如何从Url工作mp3

时间:2013-10-21 12:04:08

标签: android

  private MediaPlayer mp = new MediaPlayer();
  mp.reset();                       
  mp.setDataSource("http://thesixteendigital.com.s3.amazonaws.com/testfiles/Hallelujah.mp3");
  mp.prepare();
  mp.start();

我正在关注此代码。但是没有工作 enter image description here

4 个答案:

答案 0 :(得分:0)

对于流式传输,您需要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

答案 1 :(得分:0)

在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();
        }

答案 2 :(得分:0)

使用此功能

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 权限。

答案 3 :(得分:0)

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();

            }
        });