private MediaPlayer mp = new MediaPlayer();
mp.reset();
mp.setDataSource("http://thesixteendigital.com.s3.amazonaws.com/testfiles/Hallelujah.mp3");
mp.prepare();
mp.start();
我正在关注此代码。但无法正常工作
private MediaPlayer mp = new MediaPlayer();
mp.reset();
mp.setDataSource("http://thesixteendigital.com.s3.amazonaws.com/testfiles/Hallelujah.mp3");
mp.prepare();
mp.start();
我正在关注此代码。但无法正常工作
对于您想要prepareAsync()
而不是准备的流媒体
public class MusicPlayer implements OnPreparedListener //, OnErrorListener , OnCompletionListener
{
private MediaPlayer mp;
public MusicPlayer()
{
mp = new MediaPlayer();
mp.setOnPreparedListener(this);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
public void play()
{
mp.reset();
mp.setDataSource("http://thesixteendigital.com.s3.amazonaws.com/testfiles/Hallelujah.mp3");
mp.prepareAsync();
}
@Override
public void onPrepared(MediaPlayer arg0)//
{
mp.start();
}
}
您需要 OnErrorListener , OnCompletionListener
在后期实施
在 AndoridManifest.xml 文件中设置 Internet 权限
String url = "http://thesixteendigital.com.s3.amazonaws.com/testfiles/Hallelujah.mp3"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try{
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
}catch(IOException e){
e.printStackTrace();
}catch (IllegalArgumentException e){
e.printStackTrace();
}catch(IllegalStateException e){
e.printStackTrace();
}
用这个..
String url = "http://........"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
还要在您的清单中使用INTERNET权限。
private MediaPlayer mp = new MediaPlayer();
mp.setDataSource("http://thesixteendigital.com.s3.amazonaws.com/testfiles/Hallelujah.mp3");
mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});