MediaPlayer not playing after reopening the app - android

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

Related

Why does the Mediaplayer not work when the activity is resumed through the recent apps?

The issue im facing is that when the activity containing the media player is resumed using the recent apps, the media player does not play correctly when the image is clicked. The media player works perfectly when the app is closed using the back button and then reopened using the recent apps. However, if the app is closed using the home button and then reopened using the recent apps, the media player does not play even when the image is clicked.
public class MainActivity extends AppCompatActivity {
private static final String STATE_MEDIA_PLAYER = "media_player_state";
private static final String STATE_AUDIO_PLAYING = "audio_playing_state";
ImageView imageView;
GifImageView gifImageView;
MediaPlayer mediaPlayer;
boolean isAudioPlaying = false;
private AdView mAdView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
imageView = findViewById(R.id.imageRickroll);
gifImageView = findViewById(R.id.gifImage);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!isAudioPlaying) {
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.rickroll_song);
if (mediaPlayer != null) {
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
gifImageView.setVisibility(View.GONE);
mediaPlayer.stop();
mediaPlayer.release();
isAudioPlaying = false;
}
});
mediaPlayer.start();
isAudioPlaying = true;
}
}
gifImageView.setImageResource(R.drawable.deadserver);
gifImageView.setVisibility(View.VISIBLE);
}
});
}
private void stopAndReleaseMediaPlayer() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
#Override
protected void onStop() {
super.onStop();
gifImageView.setVisibility(View.GONE);
stopAndReleaseMediaPlayer();
}
#Override
protected void onPause() {
super.onPause();
if (mediaPlayer != null) {
mediaPlayer.pause();
}
}
#Override
protected void onResume() {
super.onResume();
if (mediaPlayer != null && isAudioPlaying) {
mediaPlayer.start();
}
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean(STATE_AUDIO_PLAYING, isAudioPlaying);
if (mediaPlayer != null) {
int currentPosition = mediaPlayer.getCurrentPosition();
savedInstanceState.putInt(STATE_MEDIA_PLAYER, currentPosition);
}
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
isAudioPlaying = savedInstanceState.getBoolean(STATE_AUDIO_PLAYING);
int currentPosition = savedInstanceState.getInt(STATE_MEDIA_PLAYER, 0);
if (mediaPlayer != null && isAudioPlaying) {
mediaPlayer.seekTo(currentPosition);
mediaPlayer.start();
}
}
}
if you use
mediaPlayer.release();
then can not reuse again.

When I return to the previous page MediaPlayer continues playing

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

android media player song not closing even i after closing application

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

Getting java.lang.IllegalStateException When I unlcok screen

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.

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