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.
Related
I have created a list adapter which is for a list of sounds. The user presses one and it plays the sound on a loop until they press to stop it. It shouldn't let more than one sound play at a time.
MediaPlayer mp;
public void playSound(int position){
if(position!=-1){
mp = MediaPlayer.create(mContext, ints.get(position));
if(!mp.isPlaying()) {
mp.setLooping(true);
mp.start();
}else{
mp.stop();
mp.release();
}
This does the exact opposite of what I would like. It doesn't stop playing the sound and instead plays multiple instances of sounds at the same time.
I am in learning process of the Android app development, and trying to create one songs app.
Problem:
I have two activities, A:songslist & B:MediaPlayer
B plays remote song via mp.prepareAsync(); and starts player when its ready.
I want to keep running the song even though I move to other activities or if I open any other app.
But problem comes, when song is running and I go back to select new song, then oncreate activity B it starts streaming and playing new song, since old song is still running.
My Code:
public void Play() {
if(mp.isPlaying())
{
releaseMediaPlayer();
Log.d("MediaPlayer", "Player is already running release it first");
}
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
URL = BASE_URL + album_files[songIndex];
mp.setDataSource(URL);
mp.prepareAsync();
btnPlay.setImageResource(R.drawable.btn_pause);
songTitleLabel.setText("Loading track, please wait....");
}
private void releaseMediaPlayer() {
if (mp != null) {
if(mp.isPlaying()) {
mp.stop();
}
Log.d("MediaPlayer", "Player is released");
mp.release();
mp = null;
}
}
You should have a service for playing music beside playlists and mediaplayer activities. service plays the music even if you exit program and you can have a widget to control playing music.
Try this. Using the answer you can make a background service and also with out background service.
Good luck
I have an android app with a button that plays a sound. the code for playing the sound:
if (mp != null)
{
mp.release();
}
mp = MediaPlayer.create(this, R.raw.match);
mp.start();
mp is a field in the activity:
public class Game extends Activity implements OnClickListener {
/** Called when the activity is first created. */
//variables:
MediaPlayer mp;
//...
The app runs ok, but after clicking the button about 200 times on the emulator, app crashed and gave me this error https://dl.dropbox.com/u/5488790/error.txt (couldn't figure how to post it here so it will appear decently)
i am assuming this is because the MediaPlayer object is consuming up too much memory, but isn't mp.release() supposed to take care of this? What am i doing wrong here?
If you are attaching a sound effect to a button, MediaPlayer in general is far too heavyweight for this operation. You're getting unnecessary latency each time just to load up the sound data into memory. You should look at using SoundPool instead.
In either case, there is no valid reason to release and re-create the MediaPlayer each time. If you decide to use MediaPlayer, control the single instance you have with the button clicks.
MediaPlayer mp;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Other init code
//Create the player this way so it doesn't auto-prepare
mp = new MediaPlayer();
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.match);
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
}
public void onDestroy() {
super.onDestroy();
//Release it only when no longer needed
mp.release();
mp = null;
}
public void onButtonClick(View v) {
if (mp.isPlaying()) {
mp.stop();
}
//Play the sound
mp.prepare();
mp.start();
}
Hope that Helps, but again, I would highly recommend using SoundPool instead if this sound is just a short effect.
It looks like your code should work, but obviously release() isn't really releasing everything.
Maybe it's because you have to reload R.raw.match every time you want to play the sound. If R.raw.match is just a short sound effect, then you might want to consider using SoundPool instead.
If you use SoundPool you only have to load R.raw.match once which may prevent the memory issues.
This tutorial has a good example on how to use it: http://www.vogella.com/articles/AndroidMedia/article.html#tutorial_soundpool
You pretty much just make one instance of SoundPool then load the sound once and play it when you need it.
Hope this helps!
Edit
If you want to use MediaPlayer...
public class Blah extends Activity implements OnClickListener {
MediaPlayer mp;
#Override
public void onCreate(Bundle b)
{
// blah blah
mp = MediaPlayer.create(R.raw.match);
// blah blah
}
#Override
public void onClick(View v)
{
if (v.getId() == yourButtonID)
{
// play sound from beginning
mp.seekTo(0);
mp.start();
}
}
}
This way you only create one instance and whenever you want to play it, you just rewind it to the beginning then play.
Try
if (mp != null)
{
mp.release();
}
mp = MediaPlayer.create(this, R.raw.match);
mp.prepare(); // not needed
mp.start();
Good luck!!
if you hold the MediaPlayer, release it at the end of the activity
#Override
void onDestroy() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
super.onDestroy();
}
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;
}
}
It now plays on first press, stops on second press or press of another sound button. If I let a sound play through I can restart it or play another sound on a single press.
My goal is for the second press of the same button to stop it, but the press of a new button to just start the new sound instead of first press of new button stopping the old sound, second press starting the new sound.
I can see why it does what it does now but am not sure how to make it work the way I want.
public void playSound(int input){
if (mp!=null && mp.isPlaying()) {
mp.stop();
mp.reset();
} else{
if (mp!=null){
mp.reset();
mp.release();
}
mp = MediaPlayer.create(Soundboard.this, input);
mp.start();
}
}
This block of code:
if (mp!=null){
mp.reset();
mp.release();
}
will never be executed. mp can only be null at this point, as it is in the else block of an if (mp != null) test. This suggests that there is a flaw in your thinking regarding the use of this method.
If a sound has played through and you press the button, then this code will execute:
mp.release();
mp = null;
Since mp was not null, the else block doesn't execute and no new sound is played. When the button is pressed a second time, mp is now null and the else block gets executed, creating a new MediaPlayer and playing the sound.