How to know my media file playing has been stopped in android - android

I have a buttons called play and stop
if I press play its starting playing. after I press stop its going to home page. But I need to go home page automatically once playing is done ie. Without clicking stop button. How can I do it for android?
thanks in advance

hi shekhar you can set an oncomplete listener to the player object. The code follows:
mPlayer.setOnCompletionListener(new OnCompletionListener(){#Override public void onCompletion(MediaPlayer mp) { "your code comes here" }});
here the mPlayer is the object of the MediaPlayer which is currently running..

Related

Managing sound in android through MediaPlayer

My android application has two buttons. Each of them will play a sound of size 1.5 seconds if clicked. I have used MediaPlayer to achieve this. The problem is when i click one button and press another within less the 1.5 sec, the second sound wont play . My requirement is when i press the second button I want the first sound to be stopped and the second sound should start playing. How do i achieve this.
When you press the 2nd button and the music is still playing, you need to stop that music and then start playing the 2nd button's song.
so add this to your button
if (mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.reset();
mPlayer.release();
mPlayer = null;
}
mPlayer = MediaPlayer.create(this, YourMusicHere);
mPlayer.start();

mediaPlayer single Instance

I got a bug on my MediaPlayer.
When i click on an item of my playlist, it create a mp3Player. If i press back the music play in background (all is normal). But if i choose another song from my app, the app create a new mediaplayer:
Then i got 2 music playing !
I tried to use "singleTask", "singleInstance", and it's not working.
I tried "intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);"
None of this work!
Maybe if i could resume the previous Activity, i could destroy it and then create a nex mediaplayer no?
Can someone help please? :D
You play music in backround it means you use Service ?
Problem is you need reconnect with Service (Service play music).
How to reconnect service android
Or try. Media Playback , EG: Universal Android Music Player Sample
you can simply make your activity android:launchMode="singleInstance" in manifest.
and then in your activity overide below method
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// do what you want here
}
in above method, you can reset value of media player

play/pause button on media player

I used the media player example on this , I create a navigation drawer and showed the play/pause button there. I got that working , I got one problem, For example I clicked the play button and it will show a notif loading and after loading it will update the notif to playing and then the play button will change into pause button. If i click the service notif I want the state of the button to show like it should be showing the pause coz the media player is playing, but instead it shows the play button.
Any help is appreciated.
Thanks
edit: play/pause code:
public void onClick(View target) {
// Send the correct intent to the MusicService, according to the button that was clicked
switch (target.getId()) {
case R.id.play_button:
startService(new Intent(MusicService.ACTION_PLAY));
mPlayButton.setVisibility(ImageButton.GONE);
mPauseButton.setVisibility(ImageButton.VISIBLE);
break;
case R.id.pause_button:
startService(new Intent(MusicService.ACTION_PAUSE));
// pause music here
mPauseButton.setVisibility(ImageButton.GONE);
mPlayButton.setVisibility(ImageButton.VISIBLE);
break;
}
I suppose clicking your notification restarts your activity and recreates the "play" button. You need to add code that checks if the music is playing when activity gets started and updates your buttons accordingly.

How to play sound using service in android

I want to play sound in my android app. And maybe I have to use service for this. But from this question I see on home button pressed and on screen lock onPause method called. But in case of home button pressed I want to stop sound in case of screen lock i dont want to stop sound. So how to do this and how to use onPause method in this case?
Please someone help me. Thanks...
Use MediaPlayer from Android. See: Android Example
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start();

Resume music after pause Phonegap

I already have succeeded playing, pausing and stopping music in Android. The problem is that when I want to resume my music, if I press pause button and play, it will start the music from the beginning.
If you are using the android mediaplayer class.. the
mediaplayer.pause();<-- will pause the player
and
mediaplayer.start();<-- will resume the player from the point it was paused...
Here is the link to phonegap 1.5 Media API.
http://docs.phonegap.com/en/1.5.0/phonegap_media_media.md.html#media.pause
Hope this help
~K
<pre><code>
public void pauseAndResume() {
if (_mediaPlayer != null && _mediaPlayer.isPlaying()) {
_mediaPlayer.pause();
}else{
_mediaPlayer.start();
}
}
</code></pre>

Categories

Resources