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=http://schemas.android.com/apk/res/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]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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM