2013-10-21 67 views
-2
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工作MP3從網址

+0

您是否在清單中添加了Internet權限? –

+0

是<使用權限android:name =「android.permission.INTERNET」/> – Angel

+0

看看[這個問題](http://stackoverflow.com/questions/10307131/android-mediaplayer-prepareasync-method) - 問題加上接受的答案會給你你需要的東西。 –

回答

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階段

+0

我相信這不是嚴格正確的。 (1)Streaming確實可以同步(不是最好的做法); (2)在準備完成後需要開始異步操作 - 您需要在mediaplayer上使用'onPreparedListener'。 –

+0

我也嘗試此代碼,但同樣的錯誤來了,我正在測試模擬器 – Angel

+0

是的,你是對的,你只需要onPreparedListener,它會工作,不適當更新我的代碼 –

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

任何問題與網址? – Angel

+0

對不起,我沒有得到你能請你詳細說明嗎? – vinaykumar

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權限在你的清單。

+0

http://developer.android.com/guide/topics/media/mediaplayer.html – Angel

+0

yups ..按照鏈接 –

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

      } 
     });