In a situation where I have a listview that in each list item when clicking is an audio and is played when clicked on the desired item.
When you click on multiple items, the audio blends.
I would like to know how I can click on an item and play and if I want another item, when clicking it the previous audio stops and the audio of the clicaco item starts.
I tried to use the code below, but audios do not stop when I click on another item.
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mediaPlayer = MediaPlayer.create(MainActivity.this, caminhoAudio[position]);
if ( !(mediaPlayer.isPlaying()) )
{
tocarSom();
}
}
});
}
public void tocarSom() {
if (mediaPlayer != null)
{
mediaPlayer.start();
}
// LIBERAR MEMÓRIA
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
};
});
}
Thank You!!!
好吧,如果您的方法startAudio()自动执行开始播放歌曲所需的所有操作,则必须在mediaPlayer.release()之后调用startAudio()。
Do something like this.
if(mMediaPlayer!=null)
{
mMediaPlayer.release();
mMediaPlayer=null;
}
if(mMediaPlayer!=null)
{
//Fist stop the current playing raw file
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer=null;
//Then Play the selected raw file.
tocarSom();
}else{
//If nothing is playing then Play the selected raw file or audio
tocarSom();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.