Reputation: 55
I start mediaplayer like this:
if (mp != null) {
mp.stop();
mp.reset();
mp.release();
}
mp = MediaPlayer.create(this, R.raw.background);
mp.start();
How can I stop in another activity? It continues to play in another activity. How can I use onDestroy
in another activity?
Upvotes: 2
Views: 11799
Reputation: 1662
This is my Kotlin solution:
package com.programacionymas.myapp.services
import android.content.Context
import android.media.MediaPlayer
object AudioPlay {
var mediaPlayer: MediaPlayer? = null
var lastResource: Int? = null
fun playAudio(c: Context, id: Int, isLooping: Boolean = true) {
createMediaPlayer(c, id)
mediaPlayer?.let {
it.isLooping = isLooping
if (!it.isPlaying) {
it.start()
}
}
}
private fun createMediaPlayer(c: Context, id: Int) {
// in case it's already playing something
mediaPlayer?.stop()
mediaPlayer = MediaPlayer.create(c, id)
lastResource = id
}
// usually used inside the Activity's onResume method
fun continuePlaying(c: Context, specificResource: Int? = null) {
specificResource?.let {
if (lastResource != specificResource) {
createMediaPlayer(c, specificResource)
}
}
mediaPlayer?.let {
if (!it.isPlaying) {
it.start()
}
}
}
fun pauseAudio() {
mediaPlayer?.pause()
}
}
I started from itsrajesh4uguys's Java answer, and I applied these changes:
Finally I use it this way:
In the onCreate
method:
AudioPlay.playAudio(this, R.raw.background_music)
In the onResume
method:
AudioPlay.continuePlaying(this, R.raw.background_music)
In my case I had to specify the resource, because some of my Activities start playing another music sounds.
Upvotes: 1
Reputation: 2648
As you have started media player in first activity and wanted to stop in another activity, just call your second layout in first activity using layout inflater self instead of creating another activity.. and on second layout file just stop the media player by pressing a button
public class FirstAvtivity extends Activity
{
MediaPlayer mPlayer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity_layoutfile);
Button b=(Button)findViewById(R.id.button1);
//start the media player like how you were starting in your activity
// then after clicking button you will be navigated to new layout , there
// you can stop media player
mPlayer.start();
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateLayout();
}
});
}
private void newUpdateLayout() {
LayoutInflater inflater = LayoutInflater.from(this);
setContentView(inflater.inflate(R.layout.second_disapr_scr, null));
finalDismiss=(Button)findViewById(R.id.final_dismiss);
finalDismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"welcome to second
avtivity",Toast.LENGTH_SHORT).show();
mPlayer.stop();
finish();
}
});
}
}
Upvotes: 0
Reputation: 4638
Use the Separate class like below in your project.
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
public class AudioPlay {
public static MediaPlayer mediaPlayer;
private static SoundPool soundPool;
public static boolean isplayingAudio=false;
public static void playAudio(Context c,int id){
mediaPlayer = MediaPlayer.create(c,id);
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
if(!mediaPlayer.isPlaying())
{
isplayingAudio=true;
mediaPlayer.start();
}
}
public static void stopAudio(){
isplayingAudio=false;
mediaPlayer.stop();
}
}
Playing the song
`AudioPlay.playAudio(mContext, R.raw.audiofile);` // play it from your preferred activity. and you can change raw file to your path also its depends upon your requirement.
then
stop the audio using this lines AudioPlay.stopAudio();
from any activity.
hope this helps.
Upvotes: 9
Reputation: 21
you cannot call stop an activity but from the activity itself to
achive this you can send the media player in a service and bind to the service in the activities you want to access it
Upvotes: 0
Reputation: 916
In 1st activity override onPause
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mp.stop();
}
Upvotes: 1