android播放器,点击暂停,再点暂停按钮可以继续播放,要是点播放按钮从头播放了
public void mediaplay(View v) { switch (v.getId()) { case R.id.playbutton: String filename = filenames.toString(); File file = new File(Environment.getExternalStorageDirectory(), filename); if (file.exists()) { path = pathload; play(0); } else { path = null; Toast.makeText(this, R.string.filenoexsit, 1).show(); } break; case R.id.pausebutton: if (mediaPlayer.isPlaying()) { mediaPlayer.pause(); pause = true; } else { if (pause) { mediaPlayer.start(); pause = false; } } break; case R.id.resetbutton: if (mediaPlayer.isPlaying()) { mediaPlayer.seekTo(0); } else { if (path != null) { play(0); } } break; case R.id.stopbutton: if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); } break; } } private void play(int position) { try { mediaPlayer.reset(); mediaPlayer.setDataSource(path); mediaPlayer.setDisplay(surfaceView.getHolder()); mediaPlayer.prepare();// 缓冲 mediaPlayer.setOnPreparedListener(new PrepareListener(position)); } catch (Exception e) { e.printStackTrace(); } } private final class PrepareListener implements OnPreparedListener { private int position; public PrepareListener(int position) { this.position = position; } public void onPrepared(MediaPlayer mp) { mediaPlayer.start();// 播放视频 if (position > 0) mediaPlayer.seekTo(position); } }
case R.id.playbutton:if (pause && mediaPlayer!=null){mediaPlayer.start();pause = false;}else{String filename = filenames.toString(); File file = new File(Environment.getExternalStorageDirectory(), filename); if (file.exists()) { path = pathload; play(0); } else { path = null; Toast.makeText(this, R.string.filenoexsit, 1).show(); }}break;
[解决办法]
点击播放的时候,先判断一下当前音乐的播放状态:
如果是停止状态:从头播放即可
如果是暂停状态:直接start()就行了
如果是播放状态:应该是不让点击的
一般暂停、播放是用一个按钮控制的,只需要换不同的背景图片就可以了,
看楼主的描述,应该是将播放、暂停分开处理的吧,这样在逻辑处理上会麻烦许多