How to mantain mp.isPlaying() state of android media player? - android

Hi friends I have an issue with Android media player. It is working but when I change the screen orientation, control with media player is lost, that is initially I am able to pause and stop the song, but once the screen orientation changes the song continues but I am unable to pause or stop etc.
This is the code I tried (I put the song in raw folder)
setContentView(R.layout.activity_medial_player);
mp = new MediaPlayer();
playPause = (Button)findViewById(R.id.buttonPlay);
playPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying()) {
if(mp!=null) {
System.out.println("*******");
mp.pause();
}
} else {
if(mp!=null) {
System.out.println("#######");
mp.start();
}
}
}
});
startSong= (Button)findViewById(R.id.buttonStart);
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// mp.pause();
playSong();
}
});
stop = (Button)findViewById(R.id.buttonStop);
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.stop();
}
});
}
public void playSong() {
try {
mp.reset();
mp = MediaPlayer.create(MedialPlayer.this, resId);
mp.start();
} catch(Exception e) {
}
}
In Manifest I use:
android:configChanges="orientation|keyboard|keyboardHidden"
Any help please...

What is your API level ?
If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.
android:configChanges="orientation|screenSize|keyboard|keyboardHidden"
Source : http://developer.android.com/guide/topics/manifest/activity-element.html

public void Play(View v){
final MediaPlayer md = MediaPlayer.create(this,R.raw.test_cbr);
Button btn = (Button)findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
if(md.isPlaying())
md.pause();
else
md.start();
}
}
);
}

Related

How to combine play & pause button into a single button

Here I have 2 buttons for the audio controller, play and pause. How can I combine both buttons into one? I also need the image icon to change as well.
In onCreate, I have initially disabled Pause button.
pauseButton.setEnabled(false);
Below is the implementation for Play button.
public void play(View view){
mediaPlayer.start();
...
pauseButton.setEnabled(true);
playButton.setEnabled(false);
}
Below is the implementation for Pause button.
public void pause(View view){
mediaPlayer.pause();
pauseButton.setEnabled(false);
playButton.setEnabled(true);
}
Make a single button and take a flag as
Boolean buttonflag=true;//initially true
Suppose true means play and false will mean pause.
public void button(View view)
{
if(buttonflag)//when true play
{
mediaPlayer.play();
buttonflag=false;//set to false so that on next click else will work
}
else//when false pause
{
mediaPlayer.pause();
buttonflag=true;//set to true so that on next click if will work
}
}
You can do this by 2 ways :
1. Take a frame layout and put two buttons overlapping each other (by default set visible to play button and hide the pause button) and set visibility according to it like
public void play(View view) {
if(pauseButton.isVisible()) {
pauseButton.setVisibility(View.GONE);
}
playButton.setVisibility(View.VISIBLE);
}
public void pause(View view) {
if(playButton.isVisible()) {
playButton.setVisibility(View.GONE);
}
pauseButton.setVisibility(View.VISIBLE);
}
2. You can take only one button and change it backgroundResource (by default set background resource play) like
public void play(View view) {
Button.setBackgroundResourec(R.drawable.pause);
}
public void pause(View view) {
Button.setBackgroundResourec(R.drawable.play);
}
MediaPlayer m1 = null;
playAndStopButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
m1=MediaPlayer.create(MainActivity.this, R.raw.sound1);
m1.start();
}
});
private void stopPlaying() {
if (mp1 != null) {
m1.stop();
m1.release();
m1 = null;
}
This should work for you! Good luck.
I know it is quite late to answer this question, but i have searched for this question and google shown me this at the top of result, but as i see there is not satisfactory result is available here, So here i am posting my code so that it could help other
fabBtnPlayAudio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!playPause) {
fabBtnPlayAudio.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
if (intialStage){
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource("URL for audio file.. ");
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
else {
if (!mediaPlayer.isPlaying())
mediaPlayer.start();
}
playPause = true;
} else {
fabBtnPlayAudio.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
playPause = false;
}
}
});

How do I program on/off button for Android?

i'm trying to make an on and off button for my background music. music starts right away. music turns off when i press off button but when i press on button it force closes. please help
mp=MediaPlayer.create(this, R.raw.islandsong);
mp.setLooping(true);
mp.start();
onButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mp==null){
mp.start();
}
}
});
offButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mp!=null){
mp.stop();
mp.release();
mp = null;
}
}
});
You are using the wrong approach. You should check if the music player is playing, if so stop it.
try {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
} catch(Exception ex) {
ex.printStackTrace()
}
Update
You need to edit your code to following if you want to play and stop the Media Player
onButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!mp.isPlaying()){
mp.start();
}
}
});
offButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
} catch(Exception ex) {
ex.printStackTrace()
}
}
This code will ensure that Media Player play song only if it is not playing and stop only if it is playing.

Android button click to play music, click again to stop sound

I have a button, when I click it plays music, how to do it, when I click second time, to stop the music?
Button two = (Button)this.findViewById(R.id.button2);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.two);
two.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
mp2.start();
}
});
Ok, this one works:
Button one = (Button)this.findViewById(R.id.button1);
final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.n);
one.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
if (mp1.isPlaying()) {
mp2.pause();
}
else {
mp2.start();
}
;
}});
The one with Pause above it works, but If I want to stop the music, it does not work.
Following not working:
#Override
public void onClick(View v) {
if (mp1.isPlaying()) {
mp2.stop();
}
else {
mp2.start();
}
;
}});
I get error: start called in state 0
error (-38, 0)
According to http://developer.android.com/reference/android/media/MediaPlayer.html, I suppose you could do something like this:
Button two = (Button)this.findViewById(R.id.button2);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.two);
two.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// If the music is playing
if(mp2.isPlaying() == true)
// Pause the music player
mp2.pause();
// If it's not playing
else
// Resume the music player
mp2.start();
}
});
You can actually write just
if(mp2.isPlaying())
instead of
if(mp2.isPlaying() == true)
It's just for the sake of understanding what is going on.
You could have a boolean check to see if it is started (and set the boolean to false) and if so stop the music, if not start it (and set the boolean to true). Something like:
public void onClick(View v)
{
if(musicPlaying == false)
{
mp2.start();
musicPlaying = true;
}
else
{
mp2.stop();
musicPlaying = false;
}
}
if(mp2.isPlaying()) {
mp2.pause();
} else {
mp2.start();
}
This is what I used to start and stop the music, you must prepare the MediaPlayer for playback. Note: this will start the music from where it stopped.Mediaplayer prepare() methodIf you are streaming music you should use prepareAsync ()
musicButton = (Button) findViewById(R.id.btn_dj_player);
musicButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mp.isPlaying()) {//check if a song is already playing
mp.stop();
try {
mp.prepare();//get the mediaplayer reeady for playback
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
mp.start();
}
}
});
If you have only one sound and you want it to start or stop with only one button than this may help .
Button button_name = (Button)this.findViewById(R.id.Your_button_id_Here);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.YOur_audio_File_name_here);
button_name.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(mp.isPlaying())
{
mp.pause();
}
else
{
mp.start();
}
}
});
Have you tried using the seekTo() function?
Something like:
if (mp.isPlaying()) {
mp.stop();
mp.prepareAsync();
mp.seekTo(0);
} else {
mp.start();
}
}
I am just guessing here, but might be worth looking into. :-)
I accomplished this as follows:
I declared a boolean variable:
boolean isButtonClicked = false;
Then I set up an if else in a method like so:
initPlaySound()
{
if(isButtonClicked)
{
player.start();
} else{
player.stop();
}
Lastly I set up an onClick of the button within the method:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
isButtonClicked = !isButtonClicked;
initPlaySound();
}
});
Basically, when the button is first clicked the boolean is set to the opposite of its' current state (false to true) and the code within the if executes. Once the button is clicked again the boolean is set to the opposite again (true to false) and the sound stops.
I solved this problem in a manual way. If sound is playing, i wrapped sound into beginning(seekTo(0)), then paused the sound. If sound is already paused, only seekTo(0) is called.
public void onClick(View v) {
if(mp.isPlaying()){
mp.seekTo(0);
mp.pause();
}
else{
mp.seekTo(0);
}
} ;
Add a global variable boolean flag=false;
if(flag==false)
{
mp=MediaPlayer.create(this, R.raw.abc);
mp.start();
playbutton.setText("Pause");
flag=true;
}
else if(mp.isPlaying()&&flag==true)
{
mp.pause();
playbutton.setText("Play");
flag=false;
}
This code will work if you want to use the same button as PLAY/PAUSE in your app. Hope this helps. Add this code in the button onClick() function which you are using to play or pause.

How to add soundButton(on&off) on a activity in android apps?

I Have created a branch activity .Now i wanted to add two button on that branch activity.
When i click on 'sound on' button then my beep sound on start and when i clicked on 'sound off' then my beep sound off. and also they hide simultaneously.
Thank's
MY Code on Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sound_layout);
soundBttnOn =(Button) findViewById(R.id.soundBttnOn);
soundBttnOn.setOnClickListener(
new OnClickListener(){
#Override
public void onClick(View v) {
startMediaPlayer();
}
}
);
soundBttnoff =(Button) findViewById(R.id.soundBttnOff);
soundBttnoff.setOnClickListener(
new OnClickListener(){
#Override
public void onClick(View v) {
stopMediaPlayer();
}
}
);
}
private void startMediaPlayer() {
mediaPlayer = MediaPlayer.create(SoundLayout.this,R.raw.keybutton5);
mediaPlayer.start();
}
private void stopMediaPlayer() {
if( mediaPlayer != null ) {
MediaPlayer mp = null;
mp.stop();
mp.release();
}
}
It showing no problem but it is not working too..:P..I am not able to implement sound.
You can do simple google search to find tons of samples for adding button. However for playing sound file check out the MediaPlayer class.
Button startBtn, stopBtn;
//get the reference of Button
....
final MediaPlayer mp = new MediaPlayer();
startBtn.onClickListener() {
public void onClick(View v) {
try {
mp.setDataSource(path+"/"+audio.mp3);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
};
stopBtn.onClickListener() {
public void onClick(View v) {
if(mp.isPlaying()) {
mp.stop();
//hide buttons
stopBtn.setVisibiltiy(View.GONE);
startBtn.setVisibility(View.GONE);
}
}
};
PS: For only on and off, you don't need two buttons, you could do with one button.
EDIT:
For single button, just use the playing state of Media player for deciding about the action to take on button click.
singleBtn.onClickListener() {
public void onClick(View v) {
try {
if(mp.isPlaying()) mp.stop();
else {
mp.setDataSource(path+"/"+audio.mp3);
mp.prepare();
mp.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};

How to play and pause in only one button - Android

I have two buttons in my media player that streams a radio station, play and pause. I want to make it only one button that has two function. First click I want to play it and second click I want to pause it. And when I click it again I want to play it. I need help. Here is my code.
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
play.performClick();
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pause();
}
});
}
private void play() {
Uri myUri = Uri.parse("Shoutcast URL");
try {
if (mp == null) {
this.mp = new MediaPlayer();
} else {
mp.stop();
mp.reset();
}
mp.setDataSource(this, myUri); // Go to Initialized state
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setOnBufferingUpdateListener(this);
mp.setOnErrorListener(this);
mp.prepareAsync();
Log.d(TAG, "LoadClip Done");
} catch (Throwable t) {
Log.d(TAG, t.toString());
}
}
#Override
public void onPrepared(MediaPlayer mp) {
Log.d(TAG, "Stream is prepared");
mp.start();
}
private void pause() {
mp.pause();
}
#Override
public void onDestroy() {
super.onDestroy();
stop();
}
public void onCompletion(MediaPlayer mp) {
stop();
}
Create just one Button and add something like a buttonstatus. Then you can check the status in your listener.
For example:
boolean isPlaying = false;
playPause = (Button) findViewById(R.id.play);
playPause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (isPlaying) {
pause();
}else{
play();
}
isPlaying = !isPlaying;
}
});
Boolean b = false;
Button play = (Button) findViewById(R.id.play);
play..setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if(b == false)
{
//write play code here
b= true;
}
else
{
// enter pause code here
}
}
}
You can use Toggle button:
toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (toggleButton.isChecked()) {
play();
} else {
pause();
}
}
});
You can also remove the green bar from toggle button.
Toggle button Mkyong example
Android dev tutorial
You don't need to declare any boolean variable to check player state. There is a method already available in mediaplayer class named - isPlaying().
Try my code sequence for playpause using single button.
public void playpause(){
if(!mp.isPlaying()){ // here mp is object of MediaPlayer class
mp.start();
//showNotification("Player State Plying");
}
else if(mp.isPlaying()){
mp.pause();
//showNotification("Player State Pause");
}
}

Categories

Resources