最近喜欢听广播,但是搜索了一下,苦于网上没有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,完成,猫扑电台悦耳的声音从我的手机里播放出来啦。

Android网络收音机项目



更多相关文章

  1. 深入理解Android插件化技术
  2. Android第十八课 VS源码文件导入Android工程 中文乱码
  3. Android 主题之安装的APK主题文件
  4. android中图片的三级cache策略(内存、文件、网络)之三:文件缓存策略
  5. android中流媒体
  6. Android 4.0 Launcher2源码分析——Laucher界面元素分解(主布局文
  7. Cordova插件调用Android原生Activity
  8. 读懂Android (1):使用Android内部的DownloadProvider下载文件,并

随机推荐

  1. android 树形目录
  2. 下载Android SDK tools完成Android SDK
  3. Android小项目之 where are you 监控
  4. 一看就懂的Android APP开发入门教程
  5. 屏和竖屏切换是否调用Activity生命周期在
  6. 深入理解 Android 系统升级
  7. eclipse中开发Android出现问题解决记录和
  8. Android中调用Unity3D探索
  9. 安卓从入门到精通路线图
  10. Android实时监控项目第一篇:项目分析及AVD