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

我正在关注此代码。但无法正常工作 在此处输入图像描述

4

4 回答 4

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在后期实施

于 2013-10-21T12:08:07.980 回答
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();
        }
于 2013-10-21T12:10:30.680 回答
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权限。

于 2013-10-21T12:10:48.960 回答
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();

            }
        });
于 2013-10-21T12:12:48.033 回答