I have created a media player which get data from url . Url is based on volley response. I'm creating a MediaPlayer
on item click so the onItemClick
create every time a new MediaPlayer()
. so i declared it in onCreate()
but after it's giving too much errors . below my code with MediaPlayer()
declaration in onItemClick
please help me anyone thanks
my motive:- i want to stop the playing song when another song is playing on item click
public class MusicsActivity extends AppCompatActivity{
private MediaPlayer mMediaPlayer;
}
public void OnItemClickActivity(int position,String video,String thumbnail,String thumbnails) {
String webUrl = "https://musicexample.com/";
Glide.with(this).load(webUrl + image).into(image);
text.setText(text);
Atext.setText(Atext);
mMediaPlayer = new MediaPlayer();
if (mMediaPlayer.isPlaying() && mMediaPlayer != null)
{
mMediaPlayer.stop();
mMediaPlayer.reset();;
mMediaPlayer.release();
mMediaPlayer = null;
}
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
togglePlayPause();
}
});
try {
mMediaPlayer.setDataSource(webUrl + src);
mMediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
To stop the Media Player without the risk of an Illegal State Exception, you must do
try {
mp.reset();
mp.prepare();
mp.stop();
mp.release();
mp=null;
}
catch (Exception e)
{
e.printStackTrace();
}
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.