i have build an app that mp3 playing from URL.The Problem is that the pause ,stop & resume is not working properly. I tried all the code and viewed every pertinent question. but nothing works.so please give the solution for this, thankyou
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
me
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SeekBar;
public class MainActivity extends Activity implements OnClickListener {
private SeekBar mBar;
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.playOnline).setOnClickListener(this);
findViewById(R.id.playOnline2).setOnClickListener(this);
findViewById(R.id.pause).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.stop).setOnClickListener(this);
mBar = (SeekBar) findViewById(R.id.progress);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.playOnline:
PlayOnlineUrl();
break;
case R.id.playOnline2:
PlayOnlineUrl2();
break;
case R.id.pause:
if (mediaPlayer != null) {
pause = true;
mediaPlayer.pause();
}
break;
case R.id.button3:
if (mediaPlayer != null) {
pause = false;
mediaPlayer.start();
}
break;
case R.id.stop:
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer = null;
timer.cancel();
task.cancel();
}
break;
default:
break;
}
}
@Override
public void onBackPressed() {
if( mediaPlayer.isPlaying() ) {
mediaPlayer.stop();
}
finish();
}
private void PlayOnlineUrl2() {
String url = "http://dj-videos.us/Music/XclusiveSinGleTrack/320%20Kbps/November%202013/Yo%20Yo%20Honey%20Singh%20-%20Blue%20Eyes-[DJKANG.Com].mp3"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
// You can show progress dialog here untill it prepared to play
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// Now dismis progress dialog, Media palyer will start playing
mp.start();
}
});
mediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// dissmiss progress bar here. It will come here when
// MediaPlayer
// is not able to play file. You can show error message to user
return false;
}
});
}
public void onPrepared(MediaPlayer mp) {
// Now dismis progress dialog, Media palyer will start playing
mp.start();
int duration = mp.getDuration();
int period = duration / 1000;
task = new TimerTask() {
@Override
public void run() {
mBar.post(new Runnable() {
@Override
public void run() {
if (!pause) {
progresss++;
mBar.setProgress(progresss);
}
}
});
}
};
timer = new Timer();
timer.schedule(task, 0, period * 10);
}
private boolean pause = false;
int progresss = 0;
private TimerTask task;
private Timer timer = null;
private void PlayOnlineUrl() {
String url = "http://starmirchi.com/upload_file/Mp3%20Songs/3605/Yo!%20Yo!%20Honey%20Singh%20_%20Blue%20Eyes%20(DJ%20AKS%20Remix)-(StarMirchi.com).mp3"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
// You can show progress dialog here untill it prepared to play
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// Now dismis progress dialog, Media palyer will start playing
mp.start();
}
});
mediaPlayer.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// dissmiss progress bar here. It will come here when
// MediaPlayer
// is not able to play file. You can show error message to user
return false;
}
});
}
}
THIS IS THE ERROR LOG
01-15 23:27:18.257: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
01-15 23:27:18.267: E/SoundPool(381): error loading /system/media/audio/ui/KeypressSpacebar.ogg
01-15 23:27:18.267: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
01-15 23:27:18.267: E/SoundPool(381): error loading /system/media/audio/ui/KeypressDelete.ogg
01-15 23:27:18.267: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
01-15 23:27:18.277: E/SoundPool(381): error loading /system/media/audio/ui/KeypressReturn.ogg
01-15 23:27:18.277: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
01-15 23:27:18.277: E/SoundPool(381): error loading /system/media/audio/ui/KeypressInvalid.ogg
01-15 23:27:18.277: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
01-15 23:27:18.277: W/AudioService(381): onLoadSoundEffects(), Error -1 while loading samples
01-15 23:27:18.367: E/AudioSink(52): received unknown event type: 1 inside CallbackWrapper !
01-15 23:27:20.097: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.097: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.107: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.107: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.127: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.127: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.137: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.137: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.137: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.197: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
01-15 23:27:20.197: E/SoundPool(381): error loading /system/media/audio/ui/KeypressStandard.ogg
01-15 23:27:20.207: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
01-15 23:27:20.207: E/SoundPool(381): error loading /system/media/audio/ui/KeypressSpacebar.ogg
01-15 23:27:20.207: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
01-15 23:27:20.207: E/SoundPool(381): error loading /system/media/audio/ui/KeypressDelete.ogg
01-15 23:27:20.207: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
01-15 23:27:20.217: E/SoundPool(381): error loading /system/media/audio/ui/KeypressReturn.ogg
01-15 23:27:20.217: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
01-15 23:27:20.217: E/SoundPool(381): error loading /system/media/audio/ui/KeypressInvalid.ogg
01-15 23:27:20.217: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
If i am not wrong then you just replace this:
findViewById(R.id.playOnline).setOnClickListener(this);
findViewById(R.id.playOnline2).setOnClickListener(this);
findViewById(R.id.pause).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.stop).setOnClickListener(this);
With below: globally defined all the buttons
Button Play1,Play2,Pause,button2,stop;
And add below into OnCreate()
:
Play1=(Button)findViewById(R.id.playOnline);
Play2=(Button)findViewById(R.id.playOnline2);
Pause=(Button)findViewById(R.id.pause);
button2=(Button)findViewById(R.id.button3);
stop=(Button)findViewById(R.id.stop);
Play1.setOnClickListener(this);
Play2.setOnClickListener(this);
Pause.setOnClickListener(this);
button2.setOnClickListener(this);
stop.setOnClickListener(this);
And try out this.
Update:
Try your button listener on this way:
Play1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//Here your code
}
});
and for all the buttons.
Your code issue is here. You just defined your MediaPlayer
object Globally then why r u created another New MediaPlayer
Object into your PlayOnlineUrl()
method. You Just add like below:
mediaPlayer = new MediaPlayer();