I'm implementing Android application for creating media player.I have run the application properly for playing the music on button click . When i click the button_Start the music is start to playing and click on the same button_Start for pause and resume/start.when the button_Start is click my button_Second is disable and when button_Start click for pause then button_Second is disable is properly . But I want when sound clip is over my button_Second is enable and button_Start is disabled .I'm getting error when i run the app (IllegelStateException) . I don't know how can i do this.Can any one help me.Thanks in advanced.
Here is my code .This is y Audio_Activity class.
public class Audio_Activity extends Activity
{
private MediaPlayer mp;
Button btnStartStop ;
Button btnChapter ;
ImageView imgVw;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.audio);
init();
mp=MediaPlayer.create(this,R.raw.ennamo_yadho);
Log.e("Song is playing","in Mediya Player ");
System.out.println("B4 button Click!!!!");
System.out.println("After Button click !! ");
if(mp!=null)
{
mp.stop();
mp.release();
System.out.println("Media Player Is Stop and release");
btnChapter.setEnabled(true);
System.out.println("btnChapter is enabled when media player is
release !!!");
}
btnStartStop.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if(mp.isPlaying())
{
if(mp!=null)
{
mp.pause();
imgVw.setImageResource(R.raw.images1);
btnChapter.setEnabled(true);
}
}
else
{
// Resume song
if(mp!=null)
{
mp.start();
imgVw.setImageResource(R.raw.teddy_two);
btnChapter.setEnabled(false);
}
}}
});
}
public void init()
{
imgVw=(ImageView)findViewById(R.id.display_Images);
btnStartStop=(Button) findViewById(R.id.btnPause_Resume);
btnChapter=(Button) findViewById(R.id.btnChapter);
}
}
You can disable the button when the sound clip starts and then use the setOnCompletionListener method of the MediaPlayer class (link) to register a handler that will enable the button again. Hope that helps:)
Related
buttonMusic = findViewById(R.id.buttonMus);
musicSound = MediaPlayer.create(this, R.raw.music);
buttonClick();
}
private Button buttonMusic;
private MediaPlayer musicSound;
public void buttonClick() {
buttonMusic.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
soundPlay(musicSound);
}
}
);
}
public void soundPlay(MediaPlayer sound) {
if (sound.isPlaying()) {
sound.stop();
}else {
sound.start();
sound.setLooping(true);
} }
Hello.
The code launches the music, is able to stop it, but it wont play again after pressing the play button, after pausing the song that is.
you aren't pausing song, you are stop()ing it. use sound.pause() for pausing :)
after stop() MediaPlayer you have to prepare it again, use prepare() or prepareAsync(). MediaPlayer.create( makes that for you at the beginning, also if you would create MediaPlayer using its constructor you would also want to prepare()/preapreAsync() before calling start() in onClick
check out state diagram of MediaPlayer
Here in my MainActivity.java I have an object MediaPlayer, which plays a sound when you click playB button and pause by pressing pauseB. Everything is working fine. But if you re-open the app and click pauseB, the sound continues to play. How to fix it? How to catch the current playing MediaPlayer?
public class MainActivity extends ActionBarActivity {
Button playB;
Button pauseB;
Context c;
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button playB = (Button) findViewById(R.id.playB);
Button pauseB = (Button) findViewById(R.id.pauseB);
mp = mp.create(this, R.raw.fawaid_1);
playB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.start();
}
});
pauseB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mp.pause();
}
});
}
}
when you re-open the app, onCreate is called again, hence you have a new MediaPlayer object, and from that moment, play and pause buttons will control that new player object instead of a previous one. That's why your pause button will have no effect on the sound that started BEFORE you minimized the app. And if you press play button now, you will have two sounds playing at the same time
one way to overcome this, is to check if media player has already been initialized:
if(mp==null)
mp = MediaPlayer.create(this, R.raw.fawaid_1);
It is advisable to use Service with AIDL in developing music player.
Reasons.
You can retain the control again once you go back in your activity.
It is easy to perform an inter process communication.
Here is a simple music Player that runs on the Background, Music Player
The song I used in this music player is Owned by SnowFlakes
How can I start the song from where I pressed pause button, help me out am new to media in android and am unaware from the media properties all the buttons are working fine , but I want to start the song where from at which point I stopped it -
public class MainActivity extends Activity {
MediaPlayer mediaPlayer;
Button play, stop,pause;
Uri path;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play = (Button) findViewById(R.id.play);
stop = (Button) findViewById(R.id.stop);
pause = (Button) findViewById(R.id.pause);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
path = Uri.parse("android.resource://" + getPackageName() + "/"+ R.raw.chelseafc);
mediaPlayer = MediaPlayer.create(MainActivity.this,path);
mediaPlayer.start();
}
});
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
});
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
});
}
#Override
protected void onDestroy() {
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer= null;
}
super.onDestroy();
}
}
You have a start button, pause button and stop button. There is no Resume button. Also when you click start after pressing pause, media loads again and starts from beginning (length = 0). You have to seek it to the paused position and resume it :
Define a global variable :
int length = 0;
In pause listener:
length=mediaPlayer.getCurrentPosition();
In resume (create a resume listener):
mediaplayer.seekTo(length);
mediaPlayer.start();
UPDATE
Ideally pause and resume button would be same except you would toggle on-off with changing image resource between play and pause pngs and execute relevant code with help of a boolean variable :
In pause button listener:
if(play)
{
//paused
play =false;
pause.setImageResource(R.id.pause);
length=mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
}else {
//resumed
play = true;
pause.setImageResource(R.id.resume);
mediaplayer.seekTo(length);
mediaPlayer.start();
}
Correct wherever necessary I typed in the editor not in IDE.
Use following code -
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);
mediaPlayer.start();
mediaPlayer.pause();
mediaPlayer.reset();
I think it will help you.
i have coded for media player and it is playing song .In the front end only image button is used and hardcoded as play button
when i click again it has to change image to pause state but the button which i have used is playing the song two times simultaneously whenever i click on it which is wrong
but i need the song to be paused so whereever i used
mp.start();
function which will be start to play the music from the url i added the code as
if(!mp.isPlaying()){
mp.start();
buttonPlayPause.setBackgroundResource(R.drawable.pause);
}else {
mp.pause();
buttonPlayPause.setBackgroundResource(R.drawable.play);}
but when i click the play button it starts playing and again if i click the button again it starts playing two times simultaneously
please help me how should i start and pause the mp3 file which i have displayed it in url it is not going to else block itself should i use different function
the full code of the project is
public class MainActivity5 extends Activity implements OnClickListener,
OnPreparedListener, OnErrorListener, OnCompletionListener {
MediaPlayer mp;
ProgressDialog pd;
Button bt;
ImageButton iv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main5);
iv = (ImageButton)findViewById(R.id.play);
iv.setOnClickListener(this);}
#Override
public void onPrepared(MediaPlayer mp)
{
Log.i("StreamAudioDemo", "prepare finished");
//pd.setMessage("Playing.....");
//mp.start();
/*if(mp.isPlaying()== true)
{
mp.start();
iv.setImageResource(R.drawable.pause);
}
else
{
mp.pause();
mp.release();
iv.setImageResource(R.drawable.play);
}*/
}
#Override
public void onClick(View v) {
try
{
pd = new ProgressDialog(this);
pd.setMessage("Buffering.....");
//pd.show();
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setOnErrorListener(this);
mp.setDataSource("http://192.168.1.138/Android/music/vande.mp3");
mp.prepareAsync();
mp.setOnCompletionListener(this);
}
catch(Exception e)
{
Log.e("StreamAudioDemo", e.getMessage());
}
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
pd.dismiss();
return false;
}
#Override
public void onCompletion(MediaPlayer mp) {
pd.dismiss();
Toast.makeText(getApplicationContext(), "Completed", Toast.LENGTH_LONG).show();
}}
thanks for your help in advance
i have commented some code above i have to re correct it
you can have a try at your onclickListener,when this player is playing,if you want to pause it ,you should to call MediaPlayer.pause() ,if you want to release it data,you can call MediaPlayer.release(),then ,you can play next music.
use this
public void onPrepared(MediaPlayer mp)
{
Log.i("StreamAudioDemo", "prepare finished");
//pd.setMessage("Playing.....");
// mp.start(); - this should be removed
if(mp.isPlaying()== true)
{
mp.pause();
iv.setImageResource(R.drawable.pause);
}
else
{
mp.start();
mp.release();
iv.setImageResource(R.drawable.play);
}
}
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
MediaPlayer mp = MediaPlayer.create(main.this, R.raw.lastmohican);
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (mp.isPlaying()) {
mp.stop();
} else {
mp.start();
}
}
}
I can play this music by one click and with another click music is stopped, but when I click together music does not start. Any idea why?
I'm not sure I understand your problem, but if you are wondering why the audio will not start back up after you press the button a third time, it is because you are calling stop() on the media player when you should be calling pause(). Stop requires you to prepare the media again before you can begin playback. Have a look at the MediaPlayer Documentation. If that is not your problem you need to provide a better description.
Its solved you should change code for this type
if (mp.isPlaying()) {
mp.stop();
mp.prepareAsync();
mp.seekTo(0);
} else {
mp.start();
}