使用android 写了一个简单的音乐播放器,但同时多次点击播放。同一首歌会同时播放。
以下为button点击时触发的方法。publicclassButClickListerimplementsView.OnClickListener{@Overridepu...
以下为button点击时触发的方法。
public class ButClickLister implements View.OnClickListener{
@Override
public void onClick(View v) {
String fileName =fileNameText.getText().toString();
File file = new File(Environment.getExternalStorageDirectory(),fileName);
Button but=(Button)v;
try {
switch (v.getId()) {
case R.id.paly:
play(file);
but.setEnabled(false);
break;
case R.id.pause:
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
but.setText(R.string.continues);
}else{
mediaPlayer.start();
but.setText(R.string.pause);
}
break;
case R.id.repaly:
if(!mediaPlayer.isPlaying()){
play(file);
}else{
mediaPlayer.seekTo(0);
}
break;
case R.id.stop:
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
break;
default:
break;
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void play(File file) throws IOException {
mediaPlayer = new MediaPlayer();
mediaPlayer.reset();
mediaPlayer.setDataSource(file.toString());
mediaPlayer.prepare();
mediaPlayer.start();
}
} 展开
public class ButClickLister implements View.OnClickListener{
@Override
public void onClick(View v) {
String fileName =fileNameText.getText().toString();
File file = new File(Environment.getExternalStorageDirectory(),fileName);
Button but=(Button)v;
try {
switch (v.getId()) {
case R.id.paly:
play(file);
but.setEnabled(false);
break;
case R.id.pause:
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
but.setText(R.string.continues);
}else{
mediaPlayer.start();
but.setText(R.string.pause);
}
break;
case R.id.repaly:
if(!mediaPlayer.isPlaying()){
play(file);
}else{
mediaPlayer.seekTo(0);
}
break;
case R.id.stop:
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
break;
default:
break;
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void play(File file) throws IOException {
mediaPlayer = new MediaPlayer();
mediaPlayer.reset();
mediaPlayer.setDataSource(file.toString());
mediaPlayer.prepare();
mediaPlayer.start();
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |