I have media player in main activity. And sound start with this activity. I want when new activity start audio stop and when I press back from next activity and back to main activity audio starts. Please help me.
Code-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.test);
mp.start();
mp.setLooping(true);
addListenerOnButton();
}
public void addListenerOnButton()
{
boy1 = (Button) findViewById(R.id.boy1);
boy1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
mp.stop();
startActivity(new Intent(Main.this, Boy1.class));
}
});
}
#Override
protected void onPause() {
super.onPause();
mp.stop();
}
#Override
public void onBackPressed() {
super.onBackPressed();
mp.stop();
finish();
}
#Override
public void onResume()
{
super.onResume();
}
#Override
protected void onStop() {
super.onStop();
mp.stop();
}
}
try this way:onPause()
#Override
protected void onPause() {
super.onPause();
try{
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}catch(Exception we){
we.printStackTrace();
}
}
And onResume()
#Override
protected void onResume() {
super.onResume();
try{
mediaPlayer.start();
}catch(Exception we){
we.printStackTrace();
}
}
You could use the pause() method available in MediaPlayer class. This would be useful:
http://developer.android.com/reference/android/media/MediaPlayer.html
You can do this using mediaPlayer.pause();
Related
When i add mediaplayer.start in onResume i was expecting to start the audio when i reopen the the app but it dindt start it
public MediaPlayer mediaPlayer;
int length = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mediaPlayer = MediaPlayer.create(this, R.raw.andhadhun);
mediaPlayer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mediaPlayer.pause();
length = mediaPlayer.getCurrentPosition();
}
#Override
protected void onResume() {
super.onResume();
mediaPlayer.setOnPreparedListener(MediaPlayer::start);
}
This will work, It pauses the MediaPlayer in onStop and resume in onResume
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(this, R.raw.audio);
mediaPlayer.start();
}
#Override
protected void onStop() {
super.onStop();
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
#Override
protected void onResume() {
super.onResume();
if (!mediaPlayer.isPlaying() && mediaPlayer != null) {
mediaPlayer.start();
}
}
}
ok try this code
private MediaPlayer mediaPlayer = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mediaPlayer = MediaPlayer.create(this,R.raw.test);
mediaPlayer.start();
}
#Override
protected void onPause() {
if(mediaPlayer!=null)
mediaPlayer.pause();
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
if(mediaPlayer!=null) {
mediaPlayer.start();
}
}
I tried all these and still the media player continues playing when I press the back button and return to the previous activity
oncreate ........ new Handler().postDelayed(new Runnable() {
#Override
public void run() {
int newInt = sharedPreferences.getInt("B" , DEFALT );
if (newInt == DEFALT) {
releaseMediaPlayer();
mediaPlayer = MediaPlayer.create(AlsamadSuraST3.this, wordsResourceArray[0][0]);
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(mCompletionListener);
editor.putInt("B" , DEFALT + 5 );
editor.apply();;
}
}
#Override
protected void onPause() {
super.onPause();
releaseMediaPlayer();
}
#Override
protected void onStop() {
super.onStop();
releaseMediaPlayer();
}
#Override
protected void onStart() {
super.onStart();
releaseMediaPlayer();
}
#Override
public void onBackPressed() {
super.onBackPressed();
releaseMediaPlayer();
}
#Override
protected void onRestart() {
super.onRestart();
releaseMediaPlayer();
}
private void releaseMediaPlayer() {
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
mediaPlayer = null;
mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener);
}
}
#Override
protected void onDestroy() {
super.onDestroy();
releaseMediaPlayer();
}
How can I stop the media player when i return to the previous activity >> there are many answers included onpause , onstop ondestroy but none of them worked
How to stop play sound by press button back or button home
Please Help me :
public class MainActivity extends AppCompatActivity {
Button play;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.button_play);
final MediaPlayer mP = MediaPlayer.create(MainActivity.this,R.raw.music);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mP.isPlaying()) {
mP.pause();
play.setBackgroundResource(R.drawable.play);
} else {
mP.start();
play.setBackgroundResource(R.drawable.pause);
}
}
}
);
}}
Pause media player in onPause(); Method
#Override
protected void onPause() {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()){
mMediaPlayer.pause();
}
super.onPause();
}
Hope It will work for you.
For more information Refer MediaPlayer LifeCycle
to stop music player
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
to stop play sound by press button back or button home
in Activity you need to stop music on onDestroy and onBackPressed
#Override
protected void onDestroy() {
super.onDestroy();
stopPlaying();
}
#Override
public void onBackPressed() {
super.onBackPressed();
stopPlaying();
}
I'm trying to stop my music when the user leaves the app or clicks the home button. My application plays music even if it is in the background. How can I stop the music if the user clicks the home button? If the user returns, the music is played again.
Here is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.vaporv2);
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mediaPlayer.start();
}
});
//toggle off and on :
MusicButton = (ToggleButton) findViewById(R.id.toggleButton);
MusicButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (MusicButton.isChecked() && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
}
});
In your Activity:
Check for two states:
on pause state: When you press home button
#Override
protected void onPause() {
super.onPause();
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
on resume state: When you come back to your screen
#Override
protected void onResume() {
super.onResume();
if(mediaPlayer!=null && !mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
Since you are in Activity you can override onResume and onStop methods
and then play/pause your app.
#Override
protected void onResume() {
super.onResume();
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
#Override
protected void onStop() {
super.onStop();
mediaPlayer.pause();
}
When i launch app and lock the screen without clicking(playing audio) button and after when i unlock the screen and press the play button i am getting Exception.
public class DemoActivity extends Activity {
MediaPlayer mp;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mp = MediaPlayer.create(DemoActivity.this,R.raw.mus);
btn = (Button) findViewById(R.id.btnOk);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (btn.getText().toString().equalsIgnoreCase("play")) {
mp.start();
btn.setText("Pause");
} else if (btn.getText().toString().equalsIgnoreCase("pause")) {
mp.pause();
btn.setText("Play");
}
}
});
#Override
public void onPause() {
super.onPause();
if (mp.isPlaying()) {
mp.start();
} else {
mp.release();
}
}
#Override
protected void onRestart() {
try {
if (mp.isPlaying() && mp!=null) {
mp.start();
} else {
mp.release();
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
super.onRestart();
}
#Override
protected void onDestroy() {
super.onDestroy();
mp.release();
}
}
Here is my code please try to give me a solution.
Thanks in advance
please try call mp.prepare() before every mp.start(). You are getting the error because the mp is not yet ready for your use, and calling start() on that will cause IllegalStateException.