在前面提到Android网络收音机--使用Vitamio解码(一)实现了播放流媒体文件,但只能播放一次,在这里可以随机点击左边的menu来播放,但Vitamio还是不是很稳定,有的uri已经过期无法播放了,我下载了一个PC版的网络收音机来对比,如果PC上能播放,在这里也是能播放的,使用了网上的一些图片,先看下修改后的界面
由于当前没有网络,所以只显示了Name
程序目录结构
关键代码选择一个流媒体播放的流程
private void playSelectItem(String url) {
if (mPlayer == null)
vplayerInit(false);
mPlayer.reset();
Uri uri = Uri.parse(url);
try {
mPlayer.setDataSource(PlayService.this, uri); // 设置流媒体的数据源
mPlayer.prepareAsync(); // 需要缓冲的不能使用prepare是阻塞的,prepareAsync是异步
} catch (Exception e) {
// TODO: handle exception
}
GlobalConstants.print_i("PlayService", "playSelectItem url = " + url);
}
private void vplayerInit(boolean isHWCodec) {
try {
mPlayer = new MediaPlayer(this.getApplicationContext(), isHWCodec);// 播放流媒体的对象
mPlayer.setOnBufferingUpdateListener(this); // 在网络视频流缓冲变化时调用
mPlayer.setOnCompletionListener(this); // 视频播放完成后调用
mPlayer.setOnPreparedListener(this); // 在视频预处理完成后调用
mPlayer.setOnErrorListener(this); // 在异步操作调用过程中发生错误时调用。例如视频打开失败
mPlayer.setOnInfoListener(this); // 在有警告或错误信息时调用。例如:开始缓冲、缓冲结束、下载速度变化
} catch (Exception e) {
// TODO: handle exception
}
}
在这里也处理了来电的广播
/**
* 处理来电广播
*
* @author Administrator
*
*/
class PhoneStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
final String action = intent.getAction();
if (action
.equalsIgnoreCase(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
final String state = intent
.getStringExtra(TelephonyManager.EXTRA_STATE);
GlobalConstants.print_i("PhoneStateReceiver",
"onReceive state = " + state);
if (state
.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)
|| state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
// 接听会收到EXTRA_STATE_OFFHOOK
pause();
} else if (state
.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)) {
// 挂断会收到EXTRA_STATE_IDLE
startPlay();
}
}
}
}
监听网络状态改变,如果没有网络,会显示一个Wifi的图标
class ConnectionChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
final String action = intent.getAction();
if (action.equalsIgnoreCase(CONNECTIVITY_CHANGE_ACTION)) {
State wifiState = null;
State mobileState = null;
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState();
mobileState = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.getState();
if (wifiState != null && mobileState != null
&& State.CONNECTED != wifiState
&& State.CONNECTED == mobileState) {
// 手机网络连接成功
ToastUtils.show(getApplicationContext(), "已连接到网络");
mIvIcon.setVisibility(View.VISIBLE);
mIvWifi.setVisibility(View.GONE);
} else if (wifiState != null && mobileState != null
&& State.CONNECTED != wifiState
&& State.CONNECTED != mobileState) {
// 手机没有任何的网络
ToastUtils.show(getApplicationContext(), "当前网络不可用");
mIvIcon.setVisibility(View.GONE);
mIvWifi.setVisibility(View.VISIBLE);
} else if (wifiState != null && State.CONNECTED == wifiState) {
// 无线网络连接成功
ToastUtils.show(getApplicationContext(), "已连接到网络");
mIvIcon.setVisibility(View.VISIBLE);
mIvWifi.setVisibility(View.GONE);
}
}
}
}
其它相关的代码,感兴趣的可以下载源码
代码下载:http://download.csdn.net/detail/deng0zhaotai/7964051
如果要在Eclipse下编译需要把这三个工程都导入,有两个是以库的形式