最近喜欢听广播,但是搜索了一下,苦于网上没有android的网络收音机项目的例子,于是自己动手实现了Android网络收音机项目。
前言,由于很多网络广播使用的协议是mms,来自微软,但是android并不支持这种流媒体协议,我的解决办法是使用Vitamio插件+Vitamio库的方式解决。这样在安装app本身的apk同时还要安装对应你手机的Vitamio插件,这个插件是老外开发的还免费,支持很多媒体格式,安上一个也挺好的,大概3M多。有一点要注意的是这个插件跟硬件有关,所以...所以如果你可以一个一个的试,看哪个适应你的手机硬件,这个插件有4个版本:
ARMv6: for some low end devices (Market, VOV)
VFP: for some low end devices with VFP support (Market, VOV)
ARMv7: for ARMv7 devices without NEON support, such as Tegra 2 powered devices(Market, VOV)
NEON: for ARMv7 devices with NEON support (Market, VOV)
具体可以上 http://vov.io/vitamio/看一下,我的烂手机是VFP这个版本的。
开始前的准备,你最好在上面的官网上下载一份API看看:Vitamio-SDK.7z。SDK里面还有vitamio.jar这个jar文件,里面有流媒体的控制类。OK废话不多说,上代码(导jar包相信大家都了然,这里不作介绍):

AndroidManifest文件
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.netradiodemo"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="10" />    <uses-permission android:name="android.permission.INTERNET"></uses-permission>    <application android:icon="@drawable/icon" android:label="@string/app_name"     android:theme="@android:style/Theme.Black">        <activity android:name=".NetRadioDemoActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity><activity android:name=".compnents.PlayerActivity"></activity>    </application></manifest>


主页面布局文件main未修改,播放页面布局文件如下play_page
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><Button      android:id="@+id/btn_start"    android:layout_gravity="center_horizontal"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="听猫扑"    android:textSize="30sp"    android:onClick="doStart"    />    <Button      android:id="@+id/btn_stop"    android:layout_gravity="center_horizontal"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="不听猫扑"    android:textSize="30sp"    android:onClick="doStop"    /></LinearLayout>


第一个activity代码,主要负责检查插件NetRadioDemoActivity
package com.netradiodemo;import io.vov.vitamio.VitamioInstaller;import io.vov.vitamio.VitamioInstaller.VitamioNotCompatibleException;import io.vov.vitamio.VitamioInstaller.VitamioNotFoundException;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.LinearLayout.LayoutParams;import android.widget.TextView;import android.widget.Toast;import com.netradiodemo.compnents.PlayerActivity;public class NetRadioDemoActivity extends Activity {Intent intent ;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        intent = new Intent(this, PlayerActivity.class);        TextView tvCheck = new TextView(this);        tvCheck.setText("使用前请检查是否安装了Vitamio插件:");        this.addContentView(tvCheck, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));                Button btnCheck = new Button(this);        btnCheck.setText("检查");        btnCheck.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {try {String isInstallerString = VitamioInstaller.checkVitamioInstallation(NetRadioDemoActivity.this);//检查插件是否安装成功,这里是一个费时操作,应该启新线程处理,作为一个demo我就不做了Log.i("tag",isInstallerString); //插件安装成功后,Log中显示插件名称if(isInstallerString!=null){Toast.makeText(NetRadioDemoActivity.this, "已安装正确版本Vitamio!", Toast.LENGTH_LONG).show();startActivity(intent);//开启收听界面}else{Toast.makeText(NetRadioDemoActivity.this, "没有匹配的Vitamio!", Toast.LENGTH_LONG).show();    finish();//没有插件安装失败,则结束程序}} catch (VitamioNotCompatibleException e) {e.printStackTrace();} catch (VitamioNotFoundException e) {e.printStackTrace();}}});        this.addContentView(btnCheck, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));    }}


第二个activity,主要负责播放PlayerActivity
package com.netradiodemo.compnents;import io.vov.vitamio.MediaPlayer;import io.vov.vitamio.VitamioInstaller.VitamioNotCompatibleException;import io.vov.vitamio.VitamioInstaller.VitamioNotFoundException;import io.vov.vitamio.widget.MediaController;import java.io.IOException;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.LinearLayout.LayoutParams;import com.netradiodemo.R;public class PlayerActivity extends Activity {MediaPlayer mPlayer;@Overrideprotected void onCreate(Bundle savedInstanceState) {setContentView(R.layout.play_page);MediaController controller = new MediaController(this);//创建控制对象this.addContentView(controller, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));String path = "mms://ting.mop.com/mopradio";//猫扑电台地址,这里可以添加自己的喜欢的电台地址,mms协议的try {mPlayer = new MediaPlayer(this);//播放流媒体的对象mPlayer.setDataSource(path);//设置流媒体的数据源mPlayer.prepare();} catch (VitamioNotCompatibleException e) {e.printStackTrace();} catch (VitamioNotFoundException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (IllegalStateException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}super.onCreate(savedInstanceState);}public void doStart(View view){mPlayer.start();//开始播放}public void doStop(View view){mPlayer.stop();//停止播放}}


OK,完成,猫扑电台悦耳的声音从我的手机里播放出来啦。





更多相关文章

  1. Pycharm安装PyQt5的详细教程
  2. Android(二) 基于 eclipse 的 Android配置 安装SDK ADT
  3. 我的Android音乐播放器
  4. 基于Android的MediaPlayer的音乐播放器的封装
  5. webapp打包为Android的apk包的一种方法
  6. Android(安卓)播放Gif 动画
  7. android学习笔记(1)---搭建开发环境
  8. 深入理解Android插件化技术
  9. Ubuntu下android学习——(1)开发环境的搭建

随机推荐

  1. ReactNative调用Android原生方法
  2. 菜鸟带你Hook技术实战
  3. Android 获取屏幕高度,虚拟导航键检测
  4. Android智能定位手表开发-目录
  5. android获取ro._修改Android序列号(Seria
  6. Android(安卓)ZoomControls缩放控件
  7. 编写ios和android共用的c/c++库时 使用ic
  8. Android应用程序窗口(Activity)的测量(Measu
  9. Android app实现静默安装并自动打开,实现
  10. 问科技:手机厂商需不需要提供新版 Android