我有一个包含一些项目的ListView,每个项目都播放一定的声音。这是代码:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
try {
player.setDataSource(externalStoragePath + "/Android/data/com.whizzappseasyvoicenotepad/" + recordedFilesArray.get(arg2) + ".mp3");
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
如果我点击一个项目然后它开始播放然后在我完成播放之前单击之前点击另一个项目,MediaPlayer将同时播放这两个声音。如果我在停止播放之前点击了该项目,如何让我的MediaPlayer停止()播放上一个声音并开始播放新声音?
我尝试在尝试之前添加player.Stop(),这样每次点击一个项目时,MediaPlayer会在设置新的源并重新开始之前停止,但这只会停止前一个声音并且不会播放新声音。
我也尝试过添加IF语句,如下所示:
if (!player.isPlaying()){
//normally start the player
}
else if (player.isPlaying()){
//stop it before starting it again
}
我会复制我的实际代码,但我已经将其删除,因为它不起作用。它也刚刚停止了之前的声音并且没有播放新声音。
答案 0 :(得分:1)
停止播放歌曲后重置播放器。
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
try {
if (player!=null && player.isPlaying())
{
player.stop();
player.reset();
}
player.setDataSource(externalStoragePath + "/Android/data/com.whizzappseasyvoicenotepad/" + recordedFilesArray.get(arg2) + ".mp3");
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
答案 1 :(得分:0)
您必须在空闲状态下呼叫setDataSource()
。因此,您必须在致电reset()
setDateSource()
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
try {
player.reset();
player.setDataSource(externalStoragePath + "/Android/data/com.whizzappseasyvoicenotepad/" + recordedFilesArray.get(arg2) + ".mp3");
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
答案 2 :(得分:0)
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
try {
stopPlaying();
initializeMediaPlayer(externalStoragePath + "/Android/data/com.whizzappseasyvoicenotepad/" + recordedFilesArray.get(arg2) + ".mp3");
startPlaying();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
private void stopPlaying() {
if (player!=null && player.isPlaying()) {
player.stop();
player.release();
}
}
private void initializeMediaPlayer(String path) {
player = new MediaPlayer();
try {
player.setDataSource(path);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void startPlaying() {
try {
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if(player!=null)
player.start();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
答案 3 :(得分:0)
要重复使用MediaPlayer
,您必须在设置新reset()
之前致电dataSource
。还有一件事,你不应该在UI线程中调用prepare()
,这可能会导致ANR
错误。你宁愿打电话给prepareAsync()
。