Android 如何暂停&;停止mp3 url流
我已经建立了一个应用程序,mp3播放的网址。问题是,暂停,停止和恢复工作不正常。我尝试了所有的代码并查看了所有相关的问题。但是什么都不管用,所以请给出解决方案,谢谢Android 如何暂停&;停止mp3 url流,android,streaming,media-player,mp3,Android,Streaming,Media Player,Mp3,我已经建立了一个应用程序,mp3播放的网址。问题是,暂停,停止和恢复工作不正常。我尝试了所有的代码并查看了所有相关的问题。但是什么都不管用,所以请给出解决方案,谢谢 import java.io.IOException; import java.util.Timer; import java.util.TimerTask; me import android.app.Activity; import android.media.AudioManager; import android.med
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;
}
});
}
}
这是错误日志
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
如果我没有错,那么你只需替换这个:
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);
如下所示:全局定义所有按钮
Button Play1,Play2,Pause,button2,stop;
并将以下内容添加到OnCreate()
:
试试这个
更新:
通过以下方式尝试按钮侦听器:
Play1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//Here your code
}
});
还有所有的按钮
您的代码问题在这里。您刚刚全局定义了MediaPlayer
对象,那么为什么您要在PlayOnlineUrl()
方法中创建另一个MediaPlayer
对象呢。您只需添加如下内容:
mediaPlayer = new MediaPlayer();
您的声音是否正在播放。声音正在播放,但暂停、恢复、停止不起作用。我在您的代码中遇到问题,请检查我的新更新答案。我尝试了此方法,但再次暂停、恢复和停止不起作用。sp请给出关于暂停的建议,停止正常工作,谢谢。一个问题也出现了,我按下播放按钮,然后再次按下播放按钮,它再次启动
mediaPlayer = new MediaPlayer();