編輯:關於Android編程
前面講到Vitamio可以支持一些流媒體,在這裡就用Vitamio來播放網絡上的一些流媒體,如:mms、rtsp、http,參考前輩的一些文章來寫一個網絡收音機程序,對於界面的要求不高,要求實現一些實用的功能
程序目錄結構
初期界面,左邊是用開源項目SlidingMenu,不清楚的可以參考:Android開源項目SlidingMenu的學習筆記
電台信息是通過XML來保存channel.xml,這裡只添加一部分,更詳細的可以參考文章最後的鏈接,自行添加
讀取XML數據放在NetRadioApp.java,使用兩個全局的變量來保存,方便其它類的使用
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在左側菜單中使用觀察者模式監聽點擊事件ChannelTypeList = new ArrayList (); public static ArrayList> finalChanneldata = new ArrayList>(); @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); InputStream is = null; try { is = getResources().getAssets().open(channel.xml); XmlPullParserUtils.getRadioListData(is, ChannelTypeList, finalChanneldata); } catch (Exception e) { // TODO Auto-generated catch block GlobalConstants.print_i(NetRadioApp, onCreate Exception = + e.toString()); e.printStackTrace(); } } @Override public void onLowMemory() { // TODO Auto-generated method stub super.onLowMemory(); System.gc(); } }
public void setOnMenuListOnItemClickListener( OnMenuListOnItemClickListener listener) { mListener = listener; } public interface OnMenuListOnItemClickListener { public void onSelectItem(int groupPosition, int childPosition); } @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // TODO Auto-generated method stub if (mListener != null) { mListener.onSelectItem(groupPosition, childPosition); } return false; }在MainActivity.java類中設置監聽器,和設置Handler與Service通信
/** * 選擇對應的Item */ @Override public void onSelectItem(int groupPosition, int childPosition) { // TODO Auto-generated method stub RadioChannelData 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(); data.putInt(groupPosition, groupPosition); data.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); }在Service中處理播放PlayService.java
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() { } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return myBinder; } public class MyBinder extends Binder { public PlayService getService() { return PlayService.this; } } private MyBinder myBinder = new MyBinder(); @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); MainActivity.addUpdateHandler(selectItemHandler); GlobalConstants.print_i(PlayService, onCreate called); } @Override public void onDestroy() { // TODO Auto-generated method stub MainActivity.removeUpdateHandler(selectItemHandler); if (mPlayer != null) { mPlayer.release(); mPlayer = null; } super.onDestroy(); } @Override public void onPrepared(MediaPlayer mp) { // TODO Auto-generated method stub GlobalConstants.print_i(PlayService, onPrepared called); if (mPlayer != null) mPlayer.start();// 開始播放 } @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub GlobalConstants.print_i(PlayService, onCompletion called); } @Override public 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);// 播放流媒體的對象 mPlayer.setDataSource(url);// 設置流媒體的數據源 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);// 設置流媒體的數據源 } 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); private static class SelectItemHandler extends WeakHandler但只能播放一次,再點擊Item就不能播放或自動退出,還沒找到是什麼原因,在後續再解決這個問題Vitamio中的MediaPlayer與系統中的MediaPlayer非常相似,用起來也方便,比VLC要方便{ public SelectItemHandler(PlayService owner) { super(owner); // TODO Auto-generated constructor stub } @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub PlayService owner = getOwner(); if (owner == null) return; switch (msg.what) { case GlobalConstants.SELECT_ITEM: Bundle data = msg.getData(); int groupPosition = data.getInt(groupPosition); int childPosition = data.getInt(childPosition); RadioChannelData channelData = NetRadioApp.finalChanneldata .get(groupPosition).get(childPosition); owner.playSelectItem(channelData.getChannelURL()); break; default: break; } } } }
相關代碼:http://download.csdn.net/detail/deng0zhaotai/7905367
參考文檔:
1、網絡電台地址(一)
2、網絡電台地址(二)
3、http://www.cnblogs.com/mythou/p/3231184.html
一、Git 與GitHub這裡首先介紹下Git與GitHub是什麼東西,他們之間有何區別呢?Git :分布式版本控制系統,最初用在Linux上,可以和SVN、CVS等作為
本文實例為大家講解如何輕松實現Android指南針功能,分享給大家供大家參考。具體如下:(1)布局文件如下:<?xml version=1.0 encodi
關於Layout_width/height引發的寬高思考方式一: 結果不符合預期。運行結果: 方式二:
0. 介紹此文將對Github上lguipeng大神所開發的 極簡筆記 v2.0 (點我下載源碼)代碼進行分析學習。通過此文你將學到:應用源碼的研讀方法 MVP架構模式