遇到的是真机测试的问题,模拟器能正常播放。
之前有篇关于mediaplayer,这次只是一些简单的修改。
问题描述:
在进行不同视频文件切换时,mediaplayer并不能正常启动播放(时常出现)。
会提示D/MediaPlayer(29517): start() mUri is null错误
切换时的主要操作方法是
1.关闭播放并重置MediaPlayer
MediaPlayer.stop()
MediaPlayer.reset()
2.之后调用播放设置
MediaPlayer.setDataSource(file)
MediaPlayer.prepare()
MediaPlayer.start()
尝试增加了reset的间隔后,可以正常播放,一直没明白什么原因(有大侠知道原因的可以指导下^_^)
之所以想到用增加时间间隔的方法,主要是因为在进行手动的停止并再启动时,视频能正常播放,而这中间进行的操作步骤完全相同。
下面贴一下代码(和第一版些许的差别,主要是给自己加深下印象)
package com.exam.test.main;
import java.io.File;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.exam.test.R;
import com.exam.engine.util.LogX;
public class mainVideoActivity extends main implements MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener, OnBufferingUpdateListener, OnCompletionListener, OnSeekCompleteListener,
SurfaceHolder.Callback
{
/**
* TAG
*/
private static final String TAG = "mainVideoActivity ";
/**
* 视频监控器地理信息
*/
private String monitorLocationInfo;
/**
* 加载页面
*/
private LinearLayout initView;
/**
* 位置信息textview
*/
private TextView text;
/**
* 监控器页面
*/
private LinearLayout playView;
/**
* 监控控制
*/
private MediaPlayer mMediaPlayer;
/**
* 文件路径
*/
private String filePath;
/**
* surfaceHolder
*/
private SurfaceHolder surfaceHolder;
/**
* 暂停按钮
*/
private ImageButton btnPause;
private static final int PLAYING = 1;
private static final int PAUSED = 2;
private static final int STOPED = 3;
/**
* 是否正在播放
*/
private int status;
private boolean isPrepared;
/**
* runner
*/
private Runnable runner = new Runnable()
{
@Override
public void run()
{
handler.sendEmptyMessageDelayed(0, 2000);
}
};
/**
* handler
*/
private Handler handler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
if (0 == msg.what)
{
initView.setVisibility(View.GONE);
this.removeCallbacks(runner);
LogX.trace(TAG, "handleMessage& mMediaPlayer.start() : " + mMediaPlayer);
if (mMediaPlayer != null)
{
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
btnPause.setBackgroundResource(R.drawable.btn_video_pause_selector);
status = PLAYING;
isPrepared = true;
}
isLocked = false;
}
else
{
playVideo();
}
}
};
/**
* 控制按钮
*/
private View.OnClickListener btnClickListener = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
LogX.trace(TAG, "onClick& status : " + status + ", isPrepared : " + isPrepared + ", mMediaPlayer : "
+ mMediaPlayer);
if (mMediaPlayer == null || !isPrepared)
{
return;
}
if (v.getId() == R.id.btn_video_pause)
{
switch (status)
{
case PLAYING:
status = PAUSED;
mMediaPlayer.pause();
btnPause.setBackgroundResource(R.drawable.btn_video_play_selector);
break;
case PAUSED:
status = PLAYING;
mMediaPlayer.start();
btnPause.setBackgroundResource(R.drawable.btn_video_pause_selector);
break;
case STOPED:
isPrepared = false;
playVideo();
break;
default:
break;
}
}
else if (status == PLAYING || status == PAUSED)
{
status = STOPED;
mMediaPlayer.stop();
mMediaPlayer.reset();
btnPause.setBackgroundResource(R.drawable.btn_video_play_selector);
}
LogX.trace(TAG, "onClick& status : " + status);
}
};
/**
* {@inheritDoc}
*/
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
playView = (LinearLayout)inflater.inflate(R.layout.main_video_play, null);
content.setOnClickListener(new View.OnClickListener()
{
long mLastClickTime = 0;
@Override
public void onClick(View v)
{
if (System.currentTimeMillis() - mLastClickTime < 2000)
{
return;
}
mLastClickTime = System.currentTimeMillis();
if (cBottom.getVisibility() == View.VISIBLE)
{
content.setPadding(0, 0, 0, 0);
cTopSapce.setVisibility(View.GONE);
cBottomSapce.setVisibility(View.GONE);
cRight.setVisibility(View.GONE);
cBottom.setVisibility(View.GONE);
}
else
{
content.setPadding(8, 8, 8, 8);
cBottomSapce.setVisibility(View.VISIBLE);
cTopSapce.setVisibility(View.VISIBLE);
cRight.setVisibility(View.VISIBLE);
cBottom.setVisibility(View.VISIBLE);
}
}
});
loadIntentData();
setContentData();
loadVideoData();
}
/**
* {@inheritDoc}
* 连接请求
*/
@Override
protected void sendRequest(mainBean bean)
{
LogX.trace(TAG, "sendRequest& isLocked : " + isLocked);
if (!isLocked)
{
monitorLocationInfo = bean.getDetailInfo();
filePath = videoFilePath + bean.getCameraId();
mMediaPlayer.stop();
mMediaPlayer.reset();
// playVideo();
isLocked = true;
isPrepared = false;
text.setText(monitorLocationInfo);
initView.setVisibility(View.VISIBLE);
// 在连续进行切换文件时,存在不能正常开始播放的问题
// 在reset后延迟1秒钟进行设置,可以正常播放---原因不明
handler.sendEmptyMessageDelayed(1, 1000);
}
}
/**
* <加载Intent传递的数据>
* @see [类、类#方法、类#成员]
*/
private void loadIntentData()
{
//获取Intent数据
Bundle bundle = getIntent().getExtras();
if (bundle != null)
{
mainBean mbean = (mainBean)bundle.getSerializable("main");
monitorLocationInfo = mbean.getDetailInfo();
filePath = videoFilePath + mbean.getCameraId();
LogX.trace(TAG, "loadIntentData& mbean : " + mbean);
}
}
/**
* <加载视频数据>
* @see [类、类#方法、类#成员]
*/
private void loadVideoData()
{
content.addView(playView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
btnPause = (ImageButton)findViewById(R.id.btn_video_pause);
btnPause.setOnClickListener(btnClickListener);
findViewById(R.id.btn_video_stop).setOnClickListener(btnClickListener);
initView = (LinearLayout)findViewById(R.id.video_inital);
text = ((TextView)findViewById(R.id.monitor_video_info));
surfaceHolder = ((SurfaceView)findViewById(R.id.video_view)).getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.addCallback(this);
}
/**
* <play>
* @see [类、类#方法、类#成员]
*/
private void playVideo()
{
if (initView.getVisibility() == View.GONE)
{
isLocked = true;
isPrepared = false;
text.setText(monitorLocationInfo);
initView.setVisibility(View.VISIBLE);
}
try
{
if (!new File(filePath).exists())
{
throw new Exception(filePath + " is not exist");
}
mMediaPlayer.setDataSource(filePath);
mMediaPlayer.prepare();
LogX.trace(TAG, "playVideo& filePath : " + filePath + ", isLocked : " + isLocked);
}
catch (Exception e)
{
btnPause.setBackgroundResource(R.drawable.btn_video_pause_selector);
isLocked = false;
e.printStackTrace();
LogX.trace(TAG, "playVideo& Exception : " + e.toString());
}
}
/**
* {@inheritDoc}
* 搜索并更新列表数据
*/
@Override
protected void search(String keyword)
{
super.search(keyword);
super.setContentData();
}
/**
* {@inheritDoc}
* MediaPlayer.start()
*/
@Override
public void onPrepared(MediaPlayer mp)
{
// surfaceHolder.setFixedSize(mVideoWidth, mVideoHeight);
// initView.setVisibility(View.GONE);
runner.run();
LogX.trace(TAG, "onPrepared& MediaPlayer.start() " + mp.getDuration());
}
/**
* {@inheritDoc}
*/
@Override
public void onCompletion(MediaPlayer mp)
{
LogX.trace(TAG, "onCompletion&");
}
/**
* {@inheritDoc}
*/
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent)
{
LogX.trace(TAG, "onBufferingUpdate& percent : " + percent);
}
/**
* {@inheritDoc}
*/
@Override
public boolean onError(MediaPlayer mp, int what, int extra)
{
LogX.trace(TAG, "onError& what : " + what + ", extra : " + extra);
return false;
}
/**
* {@inheritDoc}
*/
@Override
public void onSeekComplete(MediaPlayer mp)
{
LogX.trace(TAG, "onSeekComplete&");
}
/**
* {@inheritDoc}
*/
@Override
public void surfaceCreated(SurfaceHolder holder)
{
LogX.trace(TAG, "surfaceCreated&");
try
{
mMediaPlayer.setDisplay(holder);
playVideo();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* {@inheritDoc}
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
LogX.trace(TAG, "surfaceChanged&");
}
/**
* {@inheritDoc}
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
if (mMediaPlayer != null)
{
handler.removeCallbacks(runner);
mMediaPlayer.release();
mMediaPlayer = null;
}
LogX.trace(TAG, "surfaceDestroyed&");
}
/**
* {@inheritDoc}
*/
@Override
protected void back()
{
startActivity(new Intent(mainVideoActivity.this, mainActivity.class));
this.finish();
}
/**
* {@inheritDoc}
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
back();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onResume()
{
LogX.trace(TAG, "onResume&");
super.onResume();
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setScreenOnWhilePlaying(true);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnSeekCompleteListener(this);
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnErrorListener(this);
}
@Override
protected void onStart()
{
LogX.trace(TAG, "onStart&");
super.onStart();
}
@Override
protected void onPause()
{
LogX.trace(TAG, "onPause&");
super.onPause();
}
/**
* {@inheritDoc}
*/
@Override
protected void onStop()
{
LogX.trace(TAG, "onStop&");
super.onStop();
}
@Override
protected void onDestroy()
{
LogX.trace(TAG, "onDestroy&");
super.onDestroy();
if (mMediaPlayer != null)
{
handler.removeCallbacks(runner);
mMediaPlayer.release();
mMediaPlayer = null;
}
}
}