How to change the state of song from stop to start? - android

I have make one application in android in which i take three buttons in xml and in java i set the onclick event,
when play button is clicked song is prepared and play,
when stop button is clicked song is stopped,
when pause button is clicked song is paused.
But my problem is that when I clicked play button song is played after i click on stop button then song is stopped after I clicked on play button, but at this time song is not played,
the code I created for this app. is given below.
package com.mydemo;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MydemoActivity extends Activity {
/** Called when the activity is first created. */
Button bPlay,bPause,bStop;
MediaPlayer mp;
int position = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.fluet);
bPlay = (Button) findViewById(R.id.bPlay);
bPause = (Button)findViewById(R.id.bPause);
bStop = (Button) findViewById(R.id.bStop);
bPause.setVisibility(View.GONE);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(position > 0)
{
mp.seekTo(position);
mp.start();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
bPlay.setVisibility(View.GONE);
bPause.setVisibility(View.VISIBLE);
}
});
bPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(mp!=null)
{
mp.pause();
position = mp.getCurrentPosition();
}
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
mp.stop();
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
}
}
see this Updated code:
package com.mydemo;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MydemoActivity extends Activity {
/** Called when the activity is first created. */
Button bPlay,bPause,bStop;
MediaPlayer mp;
int position = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.fluet);
bPlay = (Button) findViewById(R.id.bPlay);
bPause = (Button)findViewById(R.id.bPause);
bStop = (Button) findViewById(R.id.bStop);
bPause.setVisibility(View.GONE);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(mp.isPlaying() == false)
{
try {
mp.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
bPlay.setVisibility(View.GONE);
bPause.setVisibility(View.VISIBLE);
}
});
bPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(mp!=null)
{
mp.pause();
position = mp.getCurrentPosition();
}
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
mp.stop();
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
}
}

Edit ::
you need to apply condition in play event like ::
if(mp.isplaying == false)
{
//play the song
}

bStop.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0) {
mp.release();
position = 0;
bPlay.setVisibility(View.VISIBLE);
bPause.setVisibility(View.GONE);
}
});
bStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(mp.isplaying == false)
{
mp = MediaPlayer.create(this, R.raw.fluet);
}
}
});

Related

Media Player Button Is Not Restarting

I wanna make "Play/Stop" button. When button "Play" is clicked, the song must be played and its text should be converted into "Stop" and when "Stop" is clicked, the button text should be changed into "Play" again and song should start again to play from the beginning.
import android.media.MediaPlayer;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private Button btn_playStop;
private MediaPlayer mediaPlayer;
private boolean flag = false;
#Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Play Music");
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.zara_sa);
btn_playStop = (Button)findViewById(R.id.btn_play_stop);
btn_playStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying() && flag==true){
stopSong();
}
else if (flag == false){
playSong();
}
}
});
}
public void playSong(){
mediaPlayer.start();
btn_playStop.setText("Stop");
flag = true;
}
public void stopSong() {
mediaPlayer.stop();
btn_playStop.setText("Play");
flag = false;
}
}
You must call mediaPlayer.prepare(); if you called mediaPlayer.stop(); so in the first time you can just call mediaPlayer.start(); but in the next times you should call mediaPlayer.prepare(); before mediaPlayer.start();
public void playSong(){
try {
mediaPlayer.prepare();
} catch (IOException e) {
}
mediaPlayer.start();
btn_playStop.setText("Stop");
flag = true;
}

android force close on clicking on seekbar

i'm newbie to android programming. i have the following code but i have a problem. when i click on the seekbar without playing the sound it force close. i mean when i click the play button i can click and seek on the seekbar but without clicking the play button clicking on the seekbar will cause force close. what's the problem?!
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity implements Runnable, OnClickListener, OnSeekBarChangeListener{
private SeekBar seekBar;
private ImageButton startMedia;
private ImageButton pauseMedia;
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AudioControl();
}
public void AudioControl(){
seekBar = (SeekBar) findViewById(R.id.seekBar1);
startMedia = (ImageButton) findViewById(R.id.playbutton);
pauseMedia = (ImageButton) findViewById(R.id.pausebutton);
seekBar.setOnSeekBarChangeListener(this);
startMedia.setOnClickListener(this);
pauseMedia.setOnClickListener(this);
}
public void run() {
int currentPosition= 0;
int total = mp.getDuration();
while (mp!=null && currentPosition<total) {
try {
Thread.sleep(1000);
currentPosition= mp.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seekBar.setProgress(currentPosition);
}
}
public void onClick(View v) {
if (v.equals(startMedia)) {
if (mp != null && mp.isPlaying()) return;
if(seekBar.getProgress() > 0) {
mp.start();
return;
}
mp = MediaPlayer.create(MainActivity.this, R.raw.lone);
mp.start();
seekBar.setProgress(0);
seekBar.setMax(mp.getDuration());
new Thread(this).start();
}
if (v.equals(pauseMedia) && mp!=null) {
mp.pause();
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if(fromUser) mp.seekTo(progress);
}
}
and i have another question:
i wanna when the user clicks on play button the pause button become visible and when the user clicks on pause button it tuen invisble. what code should i wirte and please tell me where should i put this code.
Every method that manages the UI has to be called on the UI Thread itself:
You can not run
seekBar.setProgress(currentPosition);
on a Thread different frome the UIThread.

Mp3 has paused when screen locked

mp3 pauses when on screen lock..here is my main java code...I've searched some information but nothing helped me.And by the way After clicking Play, then stop, then back to play, the application force crashes, sometimes it requires more clicks, sometimes fewer..Can someone help me please..Thank you !
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class Main extends Activity implements OnClickListener {
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private MediaPlayer player;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);
}
public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}
private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}
buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
}
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource("http://users1.jabry.com/mine/inna.mp3");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}
#Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
}
When the screen is locked your onPause is being called, and you are explicitly stopping the player there.

stop called in state 1

I could not resolve this media player error which says "stop called in state 1".my media player actually delays for few seconds before moving on to the next music.can anyone help me.
this might help u...
package com.commonsware.android.audio;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class AudioDemo extends Activity
implements MediaPlayer.OnCompletionListener {
private ImageButton play;
private ImageButton pause;
private ImageButton stop;
private MediaPlayer mp;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
play=(ImageButton)findViewById(R.id.play);
pause=(ImageButton)findViewById(R.id.pause);
stop=(ImageButton)findViewById(R.id.stop);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pause();
}
});
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
stop();
}
});
setup();
}
#Override
public void onDestroy() {
super.onDestroy();
if (stop.isEnabled()) {
stop();
}
}
public void onCompletion(MediaPlayer mp) {
stop();
}
private void play() {
mp.start();
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
}
private void stop() {
mp.stop();
pause.setEnabled(false);
stop.setEnabled(false);
try {
mp.prepare();
mp.seekTo(0);
play.setEnabled(true);
}
catch (Throwable t) {
goBlooey(t);
}
}
private void pause() {
mp.pause();
play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(true);
}
private void loadClip() {
try {
mp=MediaPlayer.create(this, R.raw.clip);
mp.setOnCompletionListener(this);
}
catch (Throwable t) {
goBlooey(t);
}
}
private void setup() {
loadClip();
play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(false);
}
private void goBlooey(Throwable t) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder
.setTitle("Exception!")
.setMessage(t.toString())
.setPositiveButton("OK", null)
.show();
}
}

error record and play audio in android

i've been trying to record and play recorded audio in android . I've used MediaRecorder and MediaPlayer class for the same.
My code contain 3 segments:
startrecording()
stoprecording()
playaudio
But there's an error in my code fragement and whenever i execute it ,the startrecording() function implements catch block.
package hare.krishna;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class Recordplay2Activity extends Activity
{
Button play1,record1,stop1;
TextView status1;
MediaRecorder m1=null;
MediaPlayer m2=null;
String path;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play1=(Button)findViewById(R.id.play);
record1=(Button)findViewById(R.id.record);
stop1=(Button)findViewById(R.id.stop);
status1=(TextView)findViewById(R.id.status);
OnClickListener recordListener = new OnClickListener()
{
public void onClick(View v)
{
startrecording();
}
};
OnClickListener stopListener = new OnClickListener()
{
public void onClick(View v)
{
stoprecording();
}
};
OnClickListener playListener = new OnClickListener()
{
public void onClick(View v)
{
playaudio();
}
};
record1.setOnClickListener(recordListener);
stop1.setOnClickListener(stopListener);
play1.setOnClickListener(playListener);
}
public void startrecording()
{
status1.setText("GET READY FOR RECORDING");
try
{
m1=new MediaRecorder();
m1.setAudioSource(MediaRecorder.AudioSource.MIC);
m1.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
m1.setOutputFile(path);
m1.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
m1.prepare();
m1.start();
}
catch(Exception e)
{
status1.setText("ERROR IN startrecording FUNCTION ");
}
}
public void stoprecording()
{
try
{
status1.setText("TIME TO STOP MEDIA");
m1.stop();
m1.reset();
m1.release();
}
catch(Exception e)
{
status1.setText("ERROR IN stoprecording FUNCTION");
}
}
public void playaudio()
{
status1.setText("PLAYING RECORDER AUDIO");
try
{
m2=new MediaPlayer();
m2.setDataSource("/sdcard/output.3gp");
m2.prepare();
m2.start();
}
catch(Exception e)
{
System.out.println("ERROR in playaudio FUNCTION");
}
}
}
I solved it. I changed AudioEncoder from:
m1.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
to:
m1.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT)

Categories

Resources