I've two activities in my app, first one (MainActivity.java) has a button to call activity two (player.java) and the second one has the audio and a stop button.
So the main problem I'm facing is..
when I click start second activity from one it goes to another activity, as i programmed it starts audio automatically (I Want It To Start Automatically) I press stop audio stops... then go back to MainActivity call second activity (player.java) again it works fine... again it starts audio correctly... everything is working fine when I press back the audio keeps playing on... (That's What i Want)
But, if press back without stopping the audio, and start the second activity (player.java) again, it starts the audio again without stopping first one... and same track plays two times...
I want to play only one track at once... if any other audio is playing, stop it, when I start that activity again.
Here is My Code...
MainActivity.java
package com.test.myapp;
import android.app.*;
import android.os.*;
import android.content.*;
import android.widget.*;
import android.view.*;
public class MainActivity extends Activity {
Button btn;
@Override
public void onCreate(Bundle savedIntanceState) {
super.onCreate(savedIntanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
public void clicked(View v){
if (v.getId() == R.id.button){
Intent i = new Intent (MainActivity.this, player.class);
startActivity(i);
}
}
}
}
player.java
package com.test.myapp;
import android.app.*;
import android.os.*;
import android.content.*;
import android.widget.*;
import android.view.*;
import android.media.*;
public class player extends Activity {
Button stop;
MediaPlayer audio;
@Override
public void onCreate(Bundle savedIntanceState) {
super.onCreate(savedIntanceState);
setContentView(R.layout.player);
stop= (Button) findViewById(R.id.stop);
audio = MediaPlayer.create(getBaseContext(), R.raw.myAudio);
audio.start();
public void stop(View v){
if (v.getId() == R.id.stop){
audio.stop();
audio = MediaPlayer.create(getBaseContext(), R.raw.myAudio);
}
}
}
}
Make your MediaPlayer object static & check for Mediaplayer isPlaying(). Try
public class player extends Activity {
Button stop;
static MediaPlayer audio;
@Override
public void onCreate(Bundle savedIntanceState) {
super.onCreate(savedIntanceState);
setContentView(R.layout.activity_player);
stop = (Button) findViewById(R.id.stop);
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
audio.stop();
}
});
if(audio == null) {
audio = MediaPlayer.create(getBaseContext(), R.raw.myaudio);
audio.start();
} else {
if(audio.isPlaying()) {
audio.stop();
audio = MediaPlayer.create(getBaseContext(), R.raw.myaudio);
audio.start();
} else {
audio = MediaPlayer.create(getBaseContext(), R.raw.myaudio);
audio.start();
}
}
}
}
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.