android media player song not closing even i after closing application - android

///////First Activity///////////
public class MainActivity extends AppCompatActivity
{
Button button;Button button3;MediaPlayer mp2;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
button=(Button)findViewById(R.id.button);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.happy);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent inent = new Intent(MainActivity.this,
Main2Activity.class);
startActivity(inent);
if(mp2.isPlaying() == true)
{
mp2.pause();}
else{
mp2.start();}
}
});
}
}
/////////Second Activity
public class Main2Activity extends AppCompatActivity
{
Button button2;MediaPlayer mp2;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}

in your onPause() try this
mp2.stop();

Per the first half of your question: you should get what you want if you call stopMediaPlayer() inside onPause() and onDestroy().
#Override
protected void onPause() {
super.onPause();
stopMediaPlayer();
}
or you can try this too,
#Override
protected void onPause() {
super.onPause();
releaseMediaPlayer();
}

Pause mediaplayer in onStop() and onDestroy() lifecycle methods.
#Override
protected void onStop() {
super.onPause();
mp2.pause();
}
#Override
protected void onDestroy() {
super.onDestroy();
mp2.pause();
}

Related

MediaPlayer not playing after reopening the app

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();
}
}

how can I stop music in one activity and start another music in next activity?

I have 3 activity, when I move from 1st act.to 2nd act. one music will start, and when I move from 2nd act. to 3rd act., 2nd activity's music should stop and 3rd activity's music should start. As per my coding, 2nd activity's music is stopping, but 3rd activity's music does not start. my code is given here-under: please help me.
//1st activity code start
public class MainActivity extends AppCompatActivity {
Button b;
static MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
final MediaPlayer mp = MediaPlayer.create(this,R.raw.bakaratwozeroone);
b = (Button) findViewById(R.id.butmove);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.start();
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
});
}
}
//1st activity code end
//2nd activity code start
public class Main2Activity extends AppCompatActivity {
Button b;
static MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
#Override
protected void onResume() {
super.onResume();
final MediaPlayer mp = MediaPlayer.create(this, R.raw.mumfive);
b = (Button) findViewById(R.id.butmove2);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.start();
startActivity(new Intent(Main2Activity.this, Main3Activity.class));
}
});
}
#Override
protected void onPause() {
super.onPause();
if(mp !=null);
mp.release();
mp = null;
}
}
//2nd activity code end
No need to keep MediaPlayer instance on every activity, Use BoundService to run media player and make communication with Service and activities.
For reference,use this link
https://github.com/SimpleMobileTools/Simple-Music-Player

How to stop audio that is playing when back button is pressed?

This is my code :
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer imp = MediaPlayer.create(this,R.raw.cat);
ImageButton btn = (ImageButton)findViewById(R.id.dog);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imp.start();
}
});
}
}
Override the activity's onBackPressed(). Something like:
#Override
public void onBackPressed() {
super.onBackPressed();
if (imp.isPlaying()) {
imp.stop();
}
}
Use the code below:
#Override
public void onBackPressed() {
super.onBackPressed();
imp.stop();
}
Try this following code to stop Audio file.
#Override
public void onBackPressed ()
{
if (mp != null)
mp.stop();
super.onBackPressed();
}
I hope this may help you.

Kill single activity Android app

I'm an Android newbie. I'm writing an app to trace the Activity lifecycle using Log statements. I want to kill my app in order to see the onDestroy() event being called. I've added a button that calls finish to do this, but I've not been able to terminate the app. I've also tried System.exit(0), but my app won't terminate. What am I doing wrong?
public class MainActivity extends Activity {
private static final String LOG_DISPLAY = "DEBUG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(LOG_DISPLAY, "onCreate called");
}
#Override
protected void onPause() {
super.onPause();
Log.d(LOG_DISPLAY, "onPause called");
}
#Override
protected void onResume() {
super.onResume();
Log.d(LOG_DISPLAY, "onResume called");
}
#Override
protected void onStop() {
super.onStop();
Log.d(LOG_DISPLAY, "onStop called");
}
#Override
protected void onStart() {
super.onStop();
Log.d(LOG_DISPLAY, "onStart called");
}
#Override
protected void onRestart() {
super.onStop();
Log.d(LOG_DISPLAY, "onRestart called");
}
public void addListenerOnButton() {
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
finish();
//System.exit(0);
}
});
}
}
You must override onDestroy in order to check if it is called, and call your addListenerOnButton method too:
Try with this:
public class MainActivity extends Activity {
private static final String LOG_DISPLAY = "DEBUG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(LOG_DISPLAY, "onCreate called");
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.d(LOG_DISPLAY, "onDestroy called");
}
#Override
protected void onPause() {
super.onPause();
Log.d(LOG_DISPLAY, "onPause called");
}
#Override
protected void onResume() {
super.onResume();
Log.d(LOG_DISPLAY, "onResume called");
}
#Override
protected void onStop() {
super.onStop();
Log.d(LOG_DISPLAY, "onStop called");
}
#Override
protected void onStart() {
super.onStop();
Log.d(LOG_DISPLAY, "onStart called");
}
#Override
protected void onRestart() {
super.onStop();
Log.d(LOG_DISPLAY, "onRestart called");
}
public void addListenerOnButton() {
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
MainActivity.this.finish();
//System.exit(0);
}
});
}
}
First add missing onDestroy implementation to show you logs :
#Override
protected void onDestroy() {
Log.d(LOG_DISPLAY, "onDestroy called");
super.onDestroy();
}
Then simply open activity and exit it with hardware back button. You will see onDestroy logs.
You will have same effect calling finish() programmatically as well, just dont forget to place call to your addListenerOnButton somewhere within onCreate

how to pause media player sound in android

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();

Categories

Resources