#播放在线音频问题 报错MediaPlayer error
这里对Android播放在线音频报错的情况做一下简单经验总结。
有两种可能:
1、这个网址是不正确的
2、这个音频文件的编码格式,手机不支持
3、还有可能电脑端浏览器能访问,但是都是手机端访问返回403,这个我就遇到了。
上面的情况,一般播放在线音频文件的预加载模式时间会比较久,有时候20多秒.才显示播放失败!
第一种情况和第三种情况其实都是差不多,都是访问没有返回200的访问码。
就是Media状态不对的情况下播放
比如:
1、MediaPlayerNative 没有回收
需要在播放完成或者停止的时候把对象释放。
mediaPlayer.reset();
mediaPlayer.release();
2、设置错误的播放流类型
比如设置成播放闹铃类型,这个可能要特殊权限或者其他设置,反正会报错。
MediaPlayer.setAudioStreamType(AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
这个熟悉一般不设置或者设置成MUSIC类型就可以啦。
在线音频播放前最好,判断一下是否能接收数据,然后再播放。
如下判断连接的url地址返回的状态的代码:
/**
* 判断连接的url地址返回的状态,必须在子线程运行否则报错null
*
* @param address
* @return
*/
public int getUrlConnectionState(String address) {
int status = HttpURLConnection.HTTP_NOT_FOUND;
try {
URL urlObj = new URL(address);
HttpURLConnection oc = (HttpURLConnection) urlObj.openConnection();
oc.setUseCaches(false);
oc.setConnectTimeout(3000); // 设置超时时间
oc.connect();
status = oc.getResponseCode();// 请求状态
if (HttpURLConnection.HTTP_OK == status) {
return status;
}
} catch (Exception e) {
Log.e(TAG, " Media getUrlConnectionState error=" + e.getMessage());
}
return status;
}
如果返回的值为HttpURLConnection.HTTP_OK(200),说明能正常连通。
完整的控制在线音频文件的示例代码:
private staticTimer timer = new Timer();
private static MediaPlayer mOnlineMediaPlayer; //背景音频文件对象
private IActionListener mOnlineMediaListener = null;
/**
* 播放在线音频文件
*
* @param url
* @param loop
* @param iActionListener
*/
public void playOnlineMedia(final String url, final float volume, final boolean loop, final IActionListener iActionListener) {
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
playInnerOnlineMedia(url, volume, loop, iActionListener);
}
};
timer.schedule(timerTask, 0);
}
public void playInnerOnlineMedia(final String url, float volume, boolean loop, IActionListener iActionListener) {
Log.i(TAG, "playOnlineMedia url=" + url + ",volume=" + volume + ",loop=" + loop + ",iActionListener=" + iActionListener);
int state = getUrlConnectionState(url);
if (state != HttpURLConnection.HTTP_OK) {
Log.e(TAG, "playOnlineMedia visit url=" + url + " ,return state:" + state);
return;
}
stopOnlineMedia();
mOnlineMediaListener = iActionListener;
mOnlineMediaPlayer = new MediaPlayer();
try {
//音频播放完毕,回调onCompletion方法
mOnlineMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.i(TAG, "playOnlineMedia onCompletion url=" + url);
if (mOnlineMediaListener != null) {
onActionListener(true, mOnlineMediaListener);
}
mOnlineMediaListener = null;
}
});
mOnlineMediaPlayer.setDataSource(url);
mOnlineMediaPlayer.setLooping(loop);
mOnlineMediaPlayer.setVolume(volume, volume);
mOnlineMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.e(TAG, "playOnlineMedia setOnErrorListener what=" + what + ",extra=" + extra);
return false;
}
});
mOnlineMediaPlayer.setAudioStreamType(AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
mOnlineMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.i(TAG, "playOnlineMedia onPrepared url=" + url);
if (mOnlineMediaPlayer != null) {
mp.start();
}
}
});
mOnlineMediaPlayer.prepareAsync();
onActionListener(false, iActionListener);
} catch (IOException e) {
Log.e(TAG, "playOnlineMedia.start error=" + e.getMessage());
e.printStackTrace();
}
}
/**
* 停止在线音频文件播放
*/
public void stopOnlineMedia() {
Log.e(TAG, "stopOnlineMedia ");
if (mOnlineMediaPlayer != null) {
try {
if (mOnlineMediaPlayer.isPlaying()) {
mOnlineMediaPlayer.stop();
}
mOnlineMediaPlayer.release();
} catch (IllegalStateException e) {
Log.e(TAG, "stopOnlineMedia error=" + e.getMessage());
}
if (mOnlineMediaListener != null) {
onActionListener(true, mOnlineMediaListener);
}
mOnlineMediaPlayer = null;
mOnlineMediaListener = null;
}
}
/**
* 暂停在线音频文件播放
*/
public void pauseOnlineMedia() {
Log.e(TAG, "pauseOnlineMedia ");
if (mOnlineMediaPlayer != null) {
try {
if (mOnlineMediaPlayer.isPlaying()) {
mOnlineMediaPlayer.pause();
}
} catch (IllegalStateException e) {
Log.e(TAG, "pauseOnlineMedia error=" + e.getMessage());
}
}
}
/**
* 恢复在线音频文件播放
*/
public void resumeOnlineMedia() {
Log.e(TAG, "resumeOnlineMedia ");
if (mOnlineMediaPlayer != null) {
try {
if (!mOnlineMediaPlayer.isPlaying()) {
mOnlineMediaPlayer.start();
}
} catch (IllegalStateException e) {
Log.e(TAG, "resumeOnlineMedia error=" + e.getMessage());
}
}
}
/**
* 判断音频文件是否在播放
*
* @return
*/
public boolean isOnlineMediaPlaying() {
try {
if (mOnlineMediaPlayer != null && mOnlineMediaPlayer.isPlaying()) {
return true;
}
} catch (IllegalStateException e) {
Log.e(TAG, "isOnlineMediaPlaying error=" + e.getMessage());
}
return false;
}
/**
* 判断连接的url地址返回的状态,必须在子线程运行否则报错null
*
* @param address
* @return
*/
public int getUrlConnectionState(String address) {
int status = HttpURLConnection.HTTP_NOT_FOUND;
try {
URL urlObj = new URL(address);
HttpURLConnection oc = (HttpURLConnection) urlObj.openConnection();
oc.setUseCaches(false);
oc.setConnectTimeout(3000); // 设置超时时间
oc.connect();
status = oc.getResponseCode();// 请求状态
if (HttpURLConnection.HTTP_OK == status) {
return status;
}
} catch (Exception e) {
Log.e(TAG, " Media getUrlConnectionState error=" + e.getMessage());
}
return status;
}
播放状态接口IActionListener
interface IActionListener {
/**
* 行为开始
*/
void onStart();
/**
*行为结束
*/
void onEnd();
}
#改变才能更加强大!