欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Android网络收音机项目

发布时间:2023/12/20 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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文件 
Java代码  
  • <?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 
    Java代码  
  • <?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 
    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 
    Java代码  
  • 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();//停止播放  
  •     }  
  •       
  • }  


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

     

    总结

    以上是生活随笔为你收集整理的Android网络收音机项目的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。