save the object's state during screen rotation - android

i have two objects for mediaplayer mp and mp1.
Both objects save the state of the media player.Here is the code
public Object onRetainNonConfigurationInstance()
{
HashMap<String,Object> player = new HashMap<String,Object>();
MediaPlayer instance = mp;
player.put("mp", instance);
mp = null;
MediaPlayer instance1 = mp1;
player.put("mp1", instance1);
mp1 = null;
return player;
}
this is my prob:
State of mp1 is saved.That is when i rotate the screen,the instance of mp1 continues from where it was.
Whereas mp restarts when i rotate the screen.Help me out please

For a MediaPlayer, you should really consider handling its state in a service and not in an activity :
its lifecycle is very different from the one of your activity (for example you may want to keep playing sounds when the user leave the app) and it has nothing to do in the UI thread.
It would also save the configuration changes handling.

Related

Resume MediaPlayer after pressing Home Button

I have a MediaPlayer that streams music in the background, when I press the home button the music stops, which what I want, but when I resume back to the application, the music is not there anymore. And when the current track ends, it doesn't loop.
My code is the following:
MediaPlayer backgroundMusic;
int length = 0;
//play background music
backgroundMusic = MediaPlayer.create(MainActivity.this, R.raw.background_music);
backgroundMusic.start();
backgroundMusic.setLooping(true);
#Override
protected void onPause() {
super.onPause();
if(backgroundMusic.isPlaying()){
backgroundMusic.pause();
length = backgroundMusic.getCurrentPosition();
}else{
return;
}
}
public void onPrepared(MediaPlayer backgroundMusic){
backgroundMusic.start();
backgroundMusic.seekTo(length);
}
How can I resume the music from where it stopped, when I press the Home Button, and Also make the music loop.
Thanks in advance.
Try to implement your code with service class if you want your player to play even in background or if you want to store the position save your data in static variable in onpause and restore it onResume of your Activity.
Preserve position of playing in shared preferences and not in local variable

Android: Playing two sounds one after the other

I am trying to play two sound items, one after the other
MediaPlayer mp = null;
protected void produceErrorSound(int index) {
if (mp != null) {
mp.reset();
mp.release();
}
mp = MediaPlayer.create(this, index);
mp.start();
}
public void correctAnswerAndNext(){
produceErrorSound(R.raw.right1) ;
produceErrorSound(R.raw.right1) ;
}
but only second sound is produced.
is there any alternative approach?
I can't see any wait mechanism in your code.
You can use an onCompletionListener to receive a callback when your first MediaPlayer has finished playback. At that point you can start the second MediaPlayer.
An alternative way in Jellybean (and later versions) is to use the audio chaining functionality (i.e. setNextMediaPlayer) to automatically start another MediaPlayer as soon as the current one has finished playing. Something like this (I've omitted the calls to setDataSource etc for brevity):
mediaPlayer1.prepare();
mediaPlayer2.prepare();
mediaPlayer1.start();
mediaPlayer1.setNextMediaPlayer(mediaPlayer2);

UI not responding on screen orientation -Android

I am developing an application with mediaplayer. I have handled screen orientation with two layouts. I have used onsavedInstancestate and onrestoreinstancesstate. On screen rotation the layout changes and the mediaplayer state is retained but UI is not responding.
I have saved and retained the state of mediaplayer object
public Object onRetainNonConfigurationInstance()
{
HashMap<String,Object> player = new HashMap<String,Object>();
MediaPlayer instance = mp;
mp = null;
MediaPlayer instance1 = mp1;
mp1 = null;
player.put("mp", instance);
player.put("mp1", instance1);
return player;
}
if(getLastNonConfigurationInstance()!=null)
{
playerhandle = (HashMap<String, Object>) getLastNonConfigurationInstance();
mp = (MediaPlayer)playerhandle.get("mp");
if (mp == null)
{
mp = new MediaPlayer();
}
mp1 = (MediaPlayer)playerhandle.get("mp1");
if (mp1 == null)
{
mp1 = new MediaPlayer();
}
}
and also retained the data with onsavedinstancestate and onrestoreinstancestate .The mediaplayer retains the state after screen orientation(audio plays without interpretation on screen orientation) but the UI is not responding after orientation change. Unable to figure out the problem .....

android mediaplayer is playing multiple instances from one player

so im trying to make a sound play through android. ive created a mediaplayer like this
MediaPlayer player; (outside onCreate())
player = MediaPlayer.create(this, R.raw.theninth); (inside onCreate())
and then i reference this player throughout the Activity. .start() on shake. .pause onclick etc.. it looks like this
if(mag >= 500) {
if(!started) {
mainView.setText(R.string.waitforit);
player.start();
started = true;
if(doThisOnce) {
timer.schedule(changeText, 26000);
doThisOnce = false;
}
}
player.setVolume((float)(2*mag/3.0),(float)(2*mag/3.0));
}
and
public void stopAudio(View v) {
mainView.setText(R.string.shakeme);
player.pause();
started = false;
}
and so the problem is the player doesnt pause when i tap the screen, instead it goes to the starting text of hte app, and and its like i reloaded the entire app, and i can shake it and then another intance of the same file from the same player will start playing.. what do?

How can stop mediaPlayer which is already running ,and create new mediaPlayer?

I am working in android. I am creating a mediaPlayer which is running audio files. i have 10 buttons. i have assigned different url to each button. So when i press button1 then song of url with respect to button 1 is playing. and then i click on 2nd button then song of button 2 is also playing with song 1. but i want to stop song of button 1 when i press button 2.
this is the code i am using for this functionality:-
public void onClick(View v)
{
MediaPlayer mediaPlayer=new MediaPlayer();
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.release();
}
int i = Integer.parseInt((v.getTag()).toString());
String str=urls[i];
try {
mediaPlayer.setDataSource(str);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e)
{
e.printStackTrace();
}
}
please check my code and let me know what is mistake done by me.
you have construct a new MediaPlayer object each time the user click you view
how could it be in the running state !!!
calling release() method on a MediaPlayer object it is in thre End state
Once the MediaPlayer object is in the End state, it can no longer be used and there is no way to bring it back to any other state.
but in case you want to reuse a MediaPlayer object you should call the
call the following method in the same order
reset()
make the mediaPlayer enter the Idle state
setDataSource()
set your data source note : the mediaplayer shoud be in the idle state
prepare()
start()

Categories

Resources