I am working in android. I am creating a mediaPlayer which is running audio files. i have 10 buttons. i have assigned different url to each button. So when i press button1 then song of url with respect to button 1 is playing. and then i click on 2nd button then song of button 2 is also playing with song 1. but i want to stop song of button 1 when i press button 2.
this is the code i am using for this functionality:-
public void onClick(View v)
{
MediaPlayer mediaPlayer=new MediaPlayer();
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.release();
}
int i = Integer.parseInt((v.getTag()).toString());
String str=urls[i];
try {
mediaPlayer.setDataSource(str);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e)
{
e.printStackTrace();
}
}
please check my code and let me know what is mistake done by me.
you have construct a new MediaPlayer object each time the user click you view
how could it be in the running state !!!
calling release() method on a MediaPlayer object it is in thre End state
Once the MediaPlayer object is in the End state, it can no longer be used and there is no way to bring it back to any other state.
but in case you want to reuse a MediaPlayer object you should call the
call the following method in the same order
reset()
make the mediaPlayer enter the Idle state
setDataSource()
set your data source note : the mediaplayer shoud be in the idle state
prepare()
start()
Related
I am developing a music player on android and the app has two activities (MainActivity and PlayActivity). In the MainActivity I have a listview with a list of songs and in the PlayActivity I have a button to listen and pause the music. The problem is that when go back to MainActivity and I select a new song from the listview, the first one keeps playing on background while the second one also starts playing. How can I stop the first song when I select a new one?
(I don't want to stop mediaplayer onBackPressed, I just want to stop the music when another song it's selected from listview and play a fresh song in PlayActivity)
EDIT: I'm using AsyncTask to start mediaplayer on PlayActivity: mp.prepareAsync();
You could try to stop MediaPlayer when onBackPressed()
MediaPlayer mp ; // mp is your MediaPlayer
#Override
public void onBackPressed() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
super.onBackPressed();
}
private void playMusic() {
if (mp != null) {
if (mp.isPlaying()) {
mp.stop();
mp.release();
MediaPlayer copyMp = new MediaPlayer();
try {
copyMp.setDataSource("/path");
mp = copyMp;
mp.prepare();
mp.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
From here
MediaPlayer is not thread-safe. Creation of and all access to player
instances should be on the same thread.
That means you have to maintain one media player instance throughout your whole application(As per your requirement). I don't know how you implemented the media player(Service/AsyncTask etc.). When you select a new song, you need to access that instance and replace the existing song with the selected one.
I have a list of songs i put manually inside my app and it currently have 3 functions: start, pause and stop (using ImageViews).
The problem is that i can play multiple songs at the same time that is not supposed to be like that. I really cant find the issue here and hope someone can help.
This is a demonstration on my problem
I want it to STOP currently playing song when another song is being clicked on.
Here is my code for my play button:
viewholder.playB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (currentSong == null || song != currentSong) {
currentSong = song;
mediaPlayer = MediaPlayer.create(context, song.getSong());
}
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
viewholder.playB.setImageResource(R.drawable.play_black);
} else {
mediaPlayer.start();
viewholder.playB.setImageResource(R.drawable.pause_black);
}
}
});
Instead of always creating a new instance of MediaPlayer every time the event onClick is called, you should create a single instance and reused it.
To play a new song don't forget to reset your player first. Here is an example:
mPlayer.reset();
mPlayer.setDataSource(context, song.getSong());
If i am playing song from a song list and if the other song is already playing then both songs plays simultaneously in android studio. If i just use next or previous buttons then they are working fine. The problem is through song list. I am not getting how to check if media player is playing already or not in this condion. I am doing this code in kotlin and i am new user of android studio.
you can check this way
You can take a global variable of MediaPlayer
public MediaPlayer mediaPlayer;
and then create in onCreate method:
mediaPlayer= MediaPlayer.create(this, R.raw.xxxx);
and thus your play method will be:
public void mediaPlay(View view) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
else{
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
}
I want my button to be "spammable". This means that if I tap on the button repeatedly the MediaPlayer starts all over again.
firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (firstTextView.getText().equals("Hello world!")) {
firstTextView.setText("You clicked!");
} else {
firstTextView.setText("Hello world!");
}
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.start();
}
});
When I interrupt the MediaPlayer when it is playing, it stops and never starts again. Why?
EDIT: The problem is that I called stop(). Thanks for pointing that out.
As per the documentation, you need to re-prepare the MediaPlayer (emphasis mine):
Once in the Stopped state, playback cannot be started until
prepare() or prepareAsync() are called to set the MediaPlayer object
to the Prepared state again.
Seems you are stopping the player because you are calling mediaPlayer.stop() this makes the MediaPlayer state to go in Stopped state. It will continue to play again when you call prepare() or prepareAsync() and has its preparation callback fired to start the playing media.
In my onCreate method i check to see whether a media player is playing and if it is a shut it down
if(mediaplayer.isPlaying()==false)
try {
playAudio(path);
} catch (Exception e) {
e.printStackTrace();
}else{
mediaplayer.stop();
mediaplayer.reset();
}
primarySeekBarProgressUpdater();
}
my play audio method is
private void playAudio(String url) throws Exception{
mediaplayer.setDataSource(url);
mediaplayer.prepare();
mediaplayer.start();
}
i also initialize my media player before the onCreate method. The problem is my media player won't shut down instead when a user clicks on a new song in the list view class it creates this class and plays both media players at the same time the old one continues playing.
Ok, so I assume you have a ListActivity with full of songs, and users click to any of those, it will move to a new Activity to play the song, in which the song information is passed through the Intent.
First of all, you need to read and remember Activity Lifecycle: http://developer.android.com/reference/android/app/Activity.html
Secondly, to your problem, according to the lifecycle, everytime users click to a song on the list, then it will create a new Activity to play the song, that means it creates a new MediaPlayer object as well. Hence, you can see many songs playing as much as you select the songs from the list.
What you need to do is to handle the MediaPlayer object when you close the music-playing screen to return to your song list.
MediaPlayer mPlayer = null;;
public void onCreate() {
// init mediaplayer here
mPlayer = ...
}
public void onDestroy() {
// release object
if( mPlayer != null ) {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
}