Android Network Radio--using Vitamio decoding (one)

Source: Internet
Author: User

Mentioned earlier Vitamio can support some streaming media, here with Vitamio to play some streaming media on the network, such as: MMS, RTSP, HTTP, refer to some of the predecessors of some articles to write a network radio program, the requirements for the interface is not high, to achieve some practical functions

Program directory Structure


Initial interface, left is open source project Slidingmenu, not clear can refer to: Android Open source project Slidingmenu study notes



Radio information is saved through XML channel.xml, here only to add a part, more detailed can refer to the last link of the article, add yourself

<?xml version= "1.0" encoding= "Utf-8"?><channel> <channeltype name= "recommended Station" id= "1" Ico N= "Fm_icon" level= "1" > <radiochannel name= "International radio Nostalgic Melody" id= "001" icon= "            Default_channel_icon "level=" 2 "url=" mms://live.cri.cn/oldies/"/> <radiochannel Name= "International radio Metropolis Popular" id= "002" icon= "Default_channel_icon" level= "2" url= "mms:/ /live.cri.cn/pop/"/> <radiochannel name=" Guangdong Radio music Voice "id=" 003 "icon=" Default_c Hannel_icon "level=" 2 "url=" mms://live.rgd.com.cn/993 "/> <radiochannel name = "Guangdong Zhujiang Economic Radio" id= "004" icon= "Default_channel_icon" level= "2" url= "MMS://LIVE.RGD . com.cn/974 "/> <radiochannel name=" Shenzhen Radio Flying Music "id=" 005 "icon=" Default_channel _icon "level="2" url= "mms://vod.szr.com.cn/szr_2"/> <radiochannel name= "Foshan Radio leap 924" id= "0 "Icon=" Default_channel_icon "level=" 2 "url=" mms://59.38.110.90/924 "/> <r            Adiochannel name= "MOP Network Radio" id= "007" icon= "Default_channel_icon" level= "2"            Url= "Mms://ting.mop.com/mopradio"/> <radiochannel name= "Xiamen Music broadcast" id= "008"        icon= "Default_channel_icon" level= "2" url= "Mms://mediasrv2.iptv.xmg.com.cn/yinyue"/> <radiochannel name= "Beijing Radio Nostalgic Melody" id= "009" icon= "Default_channel_icon" level= "2            "Url=" mms://alive.bjradio.com.cn/cfm1075 "/> <radiochannel name=" Sound of Music in Hunan Radio "        Id= "010" icon= "Default_channel_icon" level= "2" url= "mms://202.103.67.185/Music"/>       <radiochannel     Name= "Galaxy Crosstalk Skit" id= "011" icon= "Default_channel_icon" level= "2" url= "mms://m Edia.radio.cn/radio2 "/> <radiochannel name=" Shanghai story Broadcast "id=" 012 "icon=" Default_ Channel_icon "level=" 2 "url=" Mms://diantai.smgbb.cn/sgwy "/> </ChannelType> <chann ElType name= "Network Radio" id= "2" icon= "Fm_icon" level= "1" > <radiochannel nam E= "Mop Station" id= "001" icon= "Default_channel_icon" level= "2" url= "mms://ting.mop.co M/mopradio "/> <radiochannel name=" Hong Kong Airpower "id=" 002 "icon=" Default_channe L_icon "level=" 2 "url=" rtsp://live.onairpower.com:444/"/> <radiochannel nam E= "Chinese voice Synthesis" id= "003" icon= "Default_channel_icon" level= "2" url= "rtsp://60.191.2  1.139/hy960/rtx "/>      <radiochannel name= "Chinese popular selling" id= "004" icon= "Default_channel_icon" leve L= "2" url= "Mms://media.justaiwan.com/22"/> <radiochannel name= "Chinese classical Music" id= "0        "Icon=" Default_channel_icon "level=" 2 "url=" mms://media.justaiwan.com/24 "/>             <radiochannel name= "Comfortable environment Music" id= "006" icon= "Default_channel_icon" level= "2" Url= "Mms://media.justaiwan.com/19"/> <radiochannel name= "I Radio Network music station" id= "007" icon= "Default_channel_icon" level= "2" url= "http://www.iradio.com.tw/radio.asx"/&gt        ; <radiochannel name= "Galaxy Radio Chinese folk Music" id= "008" icon= "Default_channel_icon" level= "2             "Url=" Mms://media.radio.cn/radio1 "/> <radiochannel name=" CVC Chinese Radio "id=" 009 " IcoN= "Default_channel_icon" level= "2" url= "http://www2.cvc.tv/stream/CVC_Chinese_24kbps.m3u"/> <radiochannel Name= "The Voice of" QQ Comprehensive Arts "id=" 010 "icon=" Default_channel_icon "level="            2 "url=" Http://qr.fm.qq.com/qqradio?qqradio "/> <radiochannel name=" qq-Urban popularity "        Id= "011" icon= "Default_channel_icon" level= "2" url= "Rtsp://live.cri.cn/pop"/>             <radiochannel name= "Youth Music Station" id= "012" icon= "Default_channel_icon" level= "2"        Url= "rtsp://58.63.245.9/live/stream=3"/> </ChannelType> <channeltype name= "Guangdong Radio" Id= "3" icon= "Fm_icon" level= "1" > <radiochannel name= "Voice of Music of Guangdong Radio" id= "0        "Icon=" Default_channel_icon "level=" 2 "url=" mms://live.fm993.com.cn/musicfm "/>    <radiochannel        Name= "Guangdong Zhujiang Economic Station" id= "002" icon= "Default_channel_icon" level= "2" url= "MMS ://live.rgd.com.cn/974 "/> <radiochannel name=" Guangdong Southern Live Broadcast "id=" 003 "icon=" Defa            Ult_channel_icon "level=" 2 "url=" mms://live.rgd.com.cn/936 "/> <radiochannel Name= "Guangdong Yangcheng Transportation Station" id= "004" icon= "Default_channel_icon" level= "2" url= "mms://live . rgd.com.cn/1052 "/> <radiochannel name=" Shenzhen Radio Flying Music "id=" 005 "icon=" default_ch Annel_icon "level=" 2 "url=" mms://vod.szr.com.cn/szr_2 "/> <radiochannel name = "Foshan Radio leap 924" id= "006" icon= "Default_channel_icon" level= "2" url= "mms://59.38.1 10.90/924 "/> <radiochannel name=" Foshan Radio thousand Color 985 "id=" 007 "icon=" Default_channel_ Icon "Level="2 "url=" mms://59.38.110.90/985 "/> <radiochannel name=" Zhuhai Radio News "id=" 008 " icon= "Default_channel_icon" level= "2" url= "mms://www.zhtv.com/fm951"/> <radio            Channel name= "Dongguan Radio News Synthesis" id= "009" icon= "Default_channel_icon" level= "2"            Url= "mms://radio.sun0769.com/fm1008"/> <radiochannel name= "Jiangmen Radio News Synthesis" id= "010" icon= "Default_channel_icon" level= "2" url= "mms://116.254.197.80/fm1002"/> <radi            Ochannel name= "Shantou Radio music Sound" id= "011" icon= "Default_channel_icon" level= "2"            Url= "Mms://media.strtv.cn/strch3"/> <radiochannel name= "Chaozhou Radio integrated Channel" id= "012" icon= "Default_channel_icon" level= "2" url= "http://219.131.159.151/czdt939"/> </channe Ltype> <channelType name= "Central Station" id= "4" icon= "Fm_icon" level= "1" > <radiochannel name= "Central Radio China Voice" id= "001" icon= "Default_channel_icon" level= "2" url= "mms://mms.cnr.c N//cnr1? MTUJMCM "/> <radiochannel name=" Central Radio Music Sound "id=" 002 "icon=" Default_channel_ico N "level=" 2 "url=" Mms://mms.cnr.cn//cnr3? MTUJMCM "/> <radiochannel name=" international radio country ballad "id=" 003 "icon=" Default_channel_ico N "level=" 2 "url=" Mms://live.cri.cn/country "/> </ChannelType></Channel>
Read XML data in Netradioapp.java, use two global variables to save, facilitate the use of other classes

Package Com.dzt.netradio;import Java.io.inputstream;import Java.util.arraylist;import Com.dzt.netradio.constant.globalconstants;import Com.dzt.netradio.domain.radiochanneldata;import Com.dzt.netradio.utils.xmlpullparserutils;import Android.app.application;public class NetRadioApp extends Application {public static arraylist<radiochanneldata> channeltypelist = new Arraylist<radiochanneldata> ( );p ublic static arraylist<arraylist<radiochanneldata>> finalchanneldata = new arraylist<arraylist< Radiochanneldata>> (); @Overridepublic void OnCreate () {//TODO auto-generated method Stubsuper.oncreate (); Nputstream is = null;try {is = Getresources (). Getassets (). Open ("Channel.xml"); Xmlpullparserutils.getradiolistdata (is, channeltypelist,finalchanneldata);} catch (Exception e) {//TODO auto-generated catch blockglobalconstants.print_i ("Netradioapp", "onCreate Exception =" + e.t Ostring ()); E.printstacktrace ();}} @Overridepublic void Onlowmemory () {//TODO auto-generateD method Stubsuper.onlowmemory (); System.GC ();}}
Use Viewer mode to listen for click events in the left menu

public void Setonmenulistonitemclicklistener (Onmenulistonitemclicklistener listener) {Mlistener = listener;} Public interface Onmenulistonitemclicklistener {public void Onselectitem (int groupposition, int childposition);}  @Overridepublic Boolean Onchildclick (expandablelistview parent, View v,int groupposition, int childposition, long id) {// TODO auto-generated method stubif (Mlistener! = null) {Mlistener.onselectitem (groupposition, childposition);} return false;}
Set the listener in the Mainactivity.java class, and set the handler to communicate with the service

/** * Select the corresponding item */@Overridepublic void Onselectitem (int groupposition, int childposition) {//TODO auto-generated method s Tubradiochanneldata channeldata = NetRadioApp.finalChanneldata.get (groupposition). get (Childposition); Mname.settext (Channeldata.getchannelname ()); for (int i = 0; i < mupdatehandler.size (); i++) {Handler h = Mupdatehandler . get (i); Message msg = H.obtainmessage (); msg.what = Globalconstants.select_item; Bundle data = new Bundle ();d ata.putint ("Groupposition", Groupposition);d ata.putint ("Childposition", childposition); Msg.setdata (data); H.sendmessage (msg);}} public static void Addupdatehandler (Handler Handler) {mupdatehandler.add (Handler);} public static void Removeupdatehandler (Handler Handler) {mupdatehandler.remove (Handler);}
Handling playback Playservice.java in the service

Package Com.dzt.netradio.player;import Java.io.ioexception;import Io.vov.vitamio.mediaplayer;import Io.vov.vitamio.mediaplayer.onbufferingupdatelistener;import Io.vov.vitamio.MediaPlayer.OnCompletionListener; Import Io.vov.vitamio.mediaplayer.onpreparedlistener;import Android.app.service;import android.content.Intent; Import Android.os.binder;import android.os.bundle;import Android.os.handler;import Android.os.ibinder;import Android.os.message;import Com.dzt.netradio.mainactivity;import Com.dzt.netradio.netradioapp;import Com.dzt.netradio.weakhandler;import Com.dzt.netradio.constant.globalconstants;import Com.dzt.netradio.domain.radiochanneldata;public class Playservice extends Service implements Onbufferingupdatelistener,oncompletionlistener, Onpreparedlistener {private MediaPlayer mplayer;public void MyMethod () {globalconstants.print_i ("Playservice", "MyMethod called");} Public Playservice () {} @Overridepublic ibinder Onbind (Intent Intent) {//TODO auto-generated method Stubreturn MybindER;} public class Mybinder extends Binder {public Playservice getService () {return playservice.this;}} Private Mybinder Mybinder = new Mybinder (), @Overridepublic void OnCreate () {//TODO auto-generated method Stubsuper.oncrea Te (); Mainactivity.addupdatehandler (Selectitemhandler); Globalconstants.print_i ("Playservice", "onCreate called");} @Overridepublic void OnDestroy () {//TODO auto-generated method Stubmainactivity.removeupdatehandler ( Selectitemhandler); if (mPlayer! = null) {mplayer.release (); mPlayer = null;} Super.ondestroy ();} @Overridepublic void onprepared (MediaPlayer MP) {//TODO auto-generated method Stubglobalconstants.print_i (" Playservice "," onprepared called "); if (mPlayer! = null) Mplayer.start ();//start playing} @Overridepublic void Oncompletion ( MediaPlayer MP) {//TODO auto-generated method Stubglobalconstants.print_i ("Playservice", "oncompletion called");} @Overridepublic void Onbufferingupdate (MediaPlayer MP, int percent) {//TODO auto-generated method stub//globalconstants . Print_i ("PlaYservice "," onbufferingupdate called ");} private void Playselectitem (String URL) {if (MPlayer = = null) {try {mPlayer = new MediaPlayer (this);//object that plays streaming media mplayer.set DataSource (URL);//Set the data source for streaming media mplayer.prepare (); Mplayer.setonbufferingupdatelistener (this); Mplayer.setoncompletionlistener (this); Mplayer.setonpreparedlistener (this);} catch (Exception e) {globalconstants.print_i ("Playservice", "onCreate Exception =" + e.tostring ()); E.printstacktrace () ;}} else {try {mplayer.setdatasource (URL);//Set the data source for streaming media} catch (Exception e) {globalconstants.print_i ("Playservice", " OnCreate Exception = "+ e.tostring ()); E.printstacktrace ();}} Globalconstants.print_i ("Playservice", "playselectitem url =" + URL);} Private Handler Selectitemhandler = new Selectitemhandler (this);p rivate static class Selectitemhandler extends weakhandler<playservice> {public Selectitemhandler (Playservice owner) {super (owner);//TODO auto-generated constructor stub} @Overridepublic void Handlemessage (Message msg) {//TODO Auto-geneRated method Stubplayservice owner = GetOwner (); if (owner = = null) Return;switch (msg.what) {case Globalconstants.select_i Tem:bundle data = Msg.getdata (), int groupposition = Data.getint ("groupposition"); int childposition = Data.getint (" Childposition "); Radiochanneldata channeldata = NetRadioApp.finalChanneldata.get (groupposition). get (childposition); o Wner.playselectitem (Channeldata.getchannelurl ()); break;default:break;}}}
But can only play once, then click on the item can not play or automatically quit, have not found what is the reason, in the follow-up to solve the problem Vitamio in the MediaPlayer and the system MediaPlayer very similar, easy to use, more convenient than VLC

Related code: http://download.csdn.net/detail/deng0zhaotai/7905367

Reference Documentation:

1, Network Radio address (a)

2, network radio address (b)

3, http://www.cnblogs.com/mythou/p/3231184.html

Android Network Radio--using Vitamio decoding (one)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.