Android使用(su易做图ceview+mediaplayer)播放视频的问题.
出现的问题是,第一次点击播放,很顺利,没问题,第二次点击播放的时候,能播放,能听到声音但是需要点一下暂停再开始,才能出现画面,或者等播放很久才有画面,时间不定。知道的大侠,麻烦指点一下,谢谢!
下面上代码
public class MainActivity extends Activity implements OnClickListener,OnCompletionListener{
GestureOverlayView mDrawGestureView;
RelativeLayout rl;
Button playBtn,stopBtn,pauseBtn;
MediaPlayer mediaPlayer;
Su易做图ceView su易做图ceView;
int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rl=(RelativeLayout) findViewById(R.id.rl_main);
playBtn=new Button(this);
playBtn.setText("播放");
LayoutParams params1=new LayoutParams(100, 50);
params1.setMargins(0, 0, 0, 0);
playBtn.setLayoutParams(params1);
playBtn.setOnClickListener(this);
stopBtn=new Button(this);
stopBtn.setText("停止");
LayoutParams params2=new LayoutParams(100, 50);
params2.setMargins(120, 0, 0, 0);
stopBtn.setLayoutParams(params2);
stopBtn.setOnClickListener(this);
pauseBtn=new Button(this);
pauseBtn.setText("暂停");
LayoutParams params3=new LayoutParams(100, 50);
params3.setMargins(300, 0, 0, 0);
pauseBtn.setLayoutParams(params3);
pauseBtn.setOnClickListener(this);
rl.addView(playBtn);
rl.addView(stopBtn);
rl.addView(pauseBtn);
mediaPlayer=new MediaPlayer();
mediaPlayer.setLooping(true);
mediaPlayer.setOnCompletionListener(this);
su易做图ceView = new Su易做图ceView(this);
rl.addView(su易做图ceView);
LayoutParams params4=new LayoutParams(200, 200);
params4.setMargins(250,100, 0, 0);
su易做图ceView.setLayoutParams(params4);
su易做图ceView.getHolder().setKeepScreenOn(true);
su易做图ceView.getHolder().setType(Su易做图ceHolder.SURFACE_TYPE_PUSH_BUFFERS);
su易做图ceView.getHolder().addCallback(new myCallback());
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
if(v==playBtn)
{
play();
}
if(v==pauseBtn)
{
if(mediaPlayer.isPlaying())
{
mediaPlayer.pause();
}
else {
mediaPlayer.start();
}
}
if(v==stopBtn)
{
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
private void play() throws IOException
{
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource("/mnt/sdcard/i7.mp4");
mediaPlayer.setDisplay(su易做图ceView.getHolder());
mediaPlayer.prepare();
mediaPlayer.start();
}
class myCallback implements Su易做图ceHolder.Callback
{
@Override
public void su易做图ceChanged(Su易做图ceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void su易做图ceCreated(Su易做图ceHolder holder) {
// TODO Auto-generated method stub
if(position>0)
{
try {
play();
mediaPlayer.seekTo(position);
position=0;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
@Override
public void su易做图ceDestroyed(Su易做图ceHolder holder) {
// TODO Auto-generated method stub
}
}
@Override
protected void onPause()
{
if(mediaPlayer.isPlaying())
{
position=mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
}
super.onPause();
}
@Override
protected void onDestroy()
{
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
mediaPlayer.release();
super.onDestroy();
}
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
try
{
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource("/mnt/sdcard/i7.mp4");
mediaPlayer.setDisplay(su易做图ceView.getHolder());
mediaPlayer.prepare();
mediaPlayer.start();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
--------------------编程问答-------------------- 这个代码不能这样子写的,在已经播放的情况下,再去调用play()的方法,里面对视频的信息重新进行处理,这个肯定是需要耗费资源的,最简单的是
if (v == playBtn) {
if(!mediaPlayer.isPlaying())
{
play();
}
}
不过这样设计也不是很好,应该将play()里面的参数放在oncreate里面,然后添加setOnPreparedListener..在这个里面进行start
不过视频播放器一般将暂停和播放的按钮合成一个进行播放和暂停的控制
补充:移动开发 , Android