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.
Related
I made a Music Player that operates in two ways:
If started as a normal app, it continues playing what it was playing when last closed, and it should start in the background;
If an audio file is selected in a fileManager, it plays the song clicked in the foreground.
Since I want only one instance of the app running, so I added to the mainfest:
android:launchMode="singleTask"
What I did to accomplish this was to execute this onCreate:
uri = getIntent.getData();
if (uri != null) {
newsong = uri.toString()...
play(newsong);
} else {
moveTaskToBack(true);
playoldsong();
}
It works great, except for the fact that, when started as in (1) and, after a while, I select a song in a filemanager, the main activity is brought to the front (which is what I want) BUT it continues playing what it was playing before the song selection. The player ignored the new song, and I want it to switch to it.
This actually makes sense, since the only place where I call the new song is in the onCreate.
Maybe the launchMode is not the correct one? Maybe I should #Overwrite the OnNewTask(Intent) method? Which is the 'cheaper' solution?
You have to override onNewIntent() to start the song the user clicked on in the file manager.
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();
I'm creating a simple app that plays and stops music on button click. For the first time, when I click start music button, the music plays and stops when clicking it again. The problem is that when I click it again, it starts playing and doesn't stop on the next click anymore. Instead, it plays same music again in parallel (e.g. however many times I click, that many tracks are played in parallel).
The code for the button XML:
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_toRightOf="#+id/imageView1"
android:background="#7FFF00"
android:text="Start Music"
android:onClick="music" />
and MainActivity.java
public void music(View v)
{
Button bu= (Button)findViewById(R.id.button3);
String ans=bu.getText().toString();
MediaPlayer mp = MediaPlayer.create(this, R.raw.my);
switch(ans)
{
case "Stop Music":
mp.setLooping(false);
mp.stop();
bu.setText("Start Music");
break;
case "Start Music":
mp.setLooping(true);
mp.start();
bu.setText("Stop Music");
break;
default:
break;
}
}
You are creating a new MediaPlayer instance on every button click. Just create it once in your activity's onCreate() and refer to that single instance in your music() method.
Remember to call mp.release() as well, when you exit your activity.
The above Image shows various states of MediaPlayer. The Key thing you need to notice here is that After the Starting the music (Start State) when you stop the music it goes to Prepared State. Now if You want to play your song again, You need to prepare() it first.
Hence before playing the music:
mp.reset();
mp.prepare();
mp.start();
Hence When you hit the play button after the stop button! It will prepare() then play(). reset() is simply resetting the song to start from beginning.
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();
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..