Android网络收音机项目(源码实例分享)

最近喜欢听广播,但是搜索了一下,苦于网上没有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=<a href="http://schemas.android.com/apk/res/android]http://schemas.android.com/apk/res/android" rel="noopener nofollow">http://schemas.android.com/apk/res/android]http://schemas.android.com/apk/res/android</a>
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]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
Java代码

 

复制代码
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() {
@Override
public 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;
@Override
protected 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();//停止播放
}
}
复制代码

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

 

原文地址:http://www.eoeandroid.com/thread-179089-1-1.html

posted on   vus520  阅读(8003)  评论(1编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!

导航

< 2012年6月 >
27 28 29 30 31 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
1 2 3 4 5 6 7
点击右上角即可分享
微信分享提示