Resume song in Android - java

I have created a media player . I run the application properly. But I want to resume my song when I restart my activity. Why does my audio restart when I restart my activity?
How can I do this? I don't understand. Can any one help me??
Here is my code.
public class Audio_Activity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.audio);
init();
mp=MediaPlayer.create(Audio_Activity.this,R.raw.ennamo_yadho);
Log.e("Song is playing","in Mediya Player ");
if(mp!=null) {
length=mp.getCurrentPosition();
Log.e("Current ","Position -> " + length);
if(length > 0){
mp.seekTo(length);
mp.start();
btnChapter.setEnabled(false);
}
}
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
btnChapter.setEnabled(true);
System.out.println("Music is over and Button is enable !!!!!!");
}
});
}
}

To prevent this you should use a service for playing sounds while your activity goes to background. Refer to the android's documentation about the life-cycle of an activity to see what actually happens: https://developer.android.com/training/basics/activity-lifecycle/index.html

Can you try:
mp.start();
mp.seekTo(length);
Referring to documentation of MediaPlayer:
public void start ()
Starts or resumes playback. If playback had previously been paused, playback will continue from where it was paused. If playback had been stopped, or never started before, playback will start at the beginning.

Save the mp.getCurrentPosition in a persistence of some sort, sharedprefs for instance in onPause, call pause() on the mediaplayer.
In onResume, do mp.start(), mp.seekTo(savedPosition).

Related

Adding music in android studio (java, MediaPlayer)

buttonMusic = findViewById(R.id.buttonMus);
musicSound = MediaPlayer.create(this, R.raw.music);
buttonClick();
}
private Button buttonMusic;
private MediaPlayer musicSound;
public void buttonClick() {
buttonMusic.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
soundPlay(musicSound);
}
}
);
}
public void soundPlay(MediaPlayer sound) {
if (sound.isPlaying()) {
sound.stop();
}else {
sound.start();
sound.setLooping(true);
} }
Hello.
The code launches the music, is able to stop it, but it wont play again after pressing the play button, after pausing the song that is.
you aren't pausing song, you are stop()ing it. use sound.pause() for pausing :)
after stop() MediaPlayer you have to prepare it again, use prepare() or prepareAsync(). MediaPlayer.create( makes that for you at the beginning, also if you would create MediaPlayer using its constructor you would also want to prepare()/preapreAsync() before calling start() in onClick
check out state diagram of MediaPlayer

I can't understand this MediaPlayer error: MediaPlayerNative: stop called in state 0, mPlayer(0x7efba92280)

When the app starts everything is fine. But after some time it stops playing sound. but the app does not crash. I didn't understand what wrong with it.
Here my MediaPlayer code:
holder.btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer!=null){
mediaPlayer.stop();
}
mediaPlayer =MediaPlayer.create(mContext,mChineseLanguages.get(position).getSound());
mediaPlayer.start();
}
});
In logcats the following error is being displayed
MediaPlayerNative: stop called in state 0,
mPlayer(0x7efba92280) MediaPlayerNative: error (-38, 0)
You need to implement mediaPlayer's onPreparedListener and start the player inside it. The abover error is producing because you are starting the player before it reaches the onPrepared state.
For example-
mediaPlayer.setDataSource(url);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepareAsync();
public void onPrepared(MediaPlayer player) {
player.start();
}

Media Player doesn't play for the second time

I use same button for play and pause.It can handle play and pause smoothly.But after the music file end, it can not play it again.When I press it restart the application.I use mp.reset(); mp.release();.It doesn't help me in the case
Java Code:
final MediaPlayer mp1;
mp1 = MediaPlayer.create(convertView.getContext().getApplicationContext(), convertView.getResources().getIdentifier(audiopath, "raw", convertView.getContext().getPackageName()));
mHolder.play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp1.isPlaying()) {
mp1.pause();
mHolder.play.setImageResource(R.drawable.plays);
} else {
mp1.start();
mHolder.play.setImageResource(R.drawable.pause);
mp1.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
mHolder.play.setImageResource(R.drawable.plays);
}
});
}
}
});
LogCat:
java.lang.IllegalStateException
at android.media.MediaPlayer.isPlaying(Native Method)
at com.example.package.adapter.AdapterN$3.onClick(AdapterN.java:223)
img ref
Problem
When you call release() of a MediaPlayer, it deallocates all its resources allocated with MediaPlayer.create() previously, hence no longer being accessible. This produces,
java.lang.IllegalStateException
at android.media.MediaPlayer.isPlaying(Native Method)
at com.example.package.adapter.AdapterN$3.onClick(AdapterN.java:223)
Solution
You should remove,
mp.release()
However, you can still use release() but in that case you have to again create MediaPlayer instance using,
mp1 = MediaPlayer.create(convertView.getContext().getApplicationContext(), convertView.getResources().getIdentifier(audiopath, "raw", convertView.getContext().getPackageName()));
in proper place (i.e. before accessing any start, pause, reset etc.).
Suggestions
Always create MediaPlayer instance in onCreate().
Release MediaPlayer
instance in onDestroy() Use in between.
Use start/pause/reset APIs in between create and release.
Its probably because of the "mp.release()":
As you can see in the documentation here, it state that after release(), the object is no longer available.
So, what i would suggest here is you may just remove the "mp.release()" and put it under onDestroy() of your activity.

onResume to restart playing background music is not working

this is my first app so this question/answer might be pretty basic.
I currently have onPause(); , to stop the music playing when player leaves the screen. I've tried to do a similar thing but with onResume, so that the music plays again (backgroundMusic). Unfortunately this isn't working. It does work again when I press the reset button or go back to the home page and come back to the game page. But it just doesn't load as soon as the app is back on screen, like I would like it to.
My code excerpt follows;
package com.example.android.buttongame;
...
public class MainActivity extends AppCompatActivity {
...
MediaPlayer winningSound;
MediaPlayer buttonSound;
MediaPlayer backgroundMusic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
Plays ticking background noise at the start of this activity. Set on a loop
*/
backgroundMusic = MediaPlayer.create(this, R.raw.ticking_background);
backgroundMusic.start();
backgroundMusic.setOnErrorListener(new android.media.MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer mediaplayer, int i, int j)
{
return false;
}
});
backgroundMusic.setLooping(true);
}
#Override
public void onResume(){
super.onResume();
backgroundMusic.start();
}
public void onPause() {
super.onPause();
backgroundMusic.stop();
}
...
public void reset(View v) {
/*
Plays button sound
*/
buttonSound = MediaPlayer.create(MainActivity.this, R.raw.button_sound);
buttonSound.start();
/*
* Refreshes activity
*/
this.recreate();
}
...
public void homePage (View view) {
/*
Stops background music
*/
backgroundMusic.stop();
/*
Plays button sound
*/
buttonSound = MediaPlayer.create(MainActivity.this, R.raw.button_sound);
buttonSound.start();
/*
Leads to home page
*/
Intent homePage = new Intent(this, HomePage.class);
startActivity(homePage);
}
}
At the place of backgroundMusic.stop(), you should use backgroundMusic.pause() then you will achieve what you are looking for.
Calling stop() stops playback and causes a MediaPlayer in the Started, Paused, Prepared or PlaybackCompleted state to enter the Stopped state.
Once in the Stopped state, playback cannot be started until prepare() or prepareAsync() are called to set the MediaPlayer object to the Prepared state again.
Calling stop() has no effect on a MediaPlayer object that is already in the Stopped state.
Here is the documentation of MediaPlayer that will help you to understand about its APIs.
https://developer.android.com/reference/android/media/MediaPlayer

Stopping a media player

I am implementing a media player on android phone.
This player starts based on some conditions, but I am unable to stop it (back button pressing doesn't work). Once the streamer who is sending the data stops, player stops too and crashes.
I want to stop the media player whenever I want by pressing the BACK button. can somebody tell me how to do so?
I have tried using methods like these, but they don't work!!
#Override
public void onBackPressed()
{
MediaPlayer.stop();
MediaPlayer.release();
finish();
}
#Override
protected void onStop()
{
finish();
}
Try adding
MediaPlayer.release();
tou the activities onPause() method
put super.onStop() (super call) call in your onstop method.And also i think you don't want to call finish () method twice.when you back the activity and the activity disappears and onStop called.you can remove onStop call from onBackPressed method.
It should be smth like this:
private MediaPlayer mPlayer = ...; // define it somewhere
#Override
public void onBackPressed()
{
if(mPlayer != null && mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.release();
}
}
#Override
protected void onStop()
{
super.onStop();
if(mPlayer != null && mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.release();
}
}

Categories

Resources