2016-05-13 53 views
1

我想紛至沓來的Android的音頻文件,但每次我得到這個例外如何流音頻文件的Android

產生java.io.IOException:準備失敗:狀態=爲0x1

爲我的歌曲鏈接存儲在服務器上。例如(http://Touchstoneesol.com/listen/ML1 (Section 2).mp3);

但是相同的碼流正確鏈接(http://android.programmerguru.com/wp-content/uploads/2013/04/hosannatelugu.mp3);

現在我陷入了介於代碼中的問題還是我的歌曲鏈接已損壞。

buttonPlay = (Button) findViewById(R.id.play); 
     buttonPlay.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      mPlayer = new MediaPlayer(); 
      mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
      try { 
       mPlayer.setDataSource(url); 
      } catch (IllegalArgumentException e) { 
       Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
      } catch (SecurityException e) { 
       Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
      } catch (IllegalStateException e) { 
       Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      try { 
       mPlayer.prepare(); 
      } catch (IllegalStateException e) { 
       Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
      } catch (IOException e) { 
       Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
      } 
      mPlayer.start(); 
     } 
    }); 
+0

你可以在這裏測試:http://demo.jwplayer.com/stream-tester/如果你的流鏈接正在工作。 – WannaBeGeek

+0

謝謝,刪除了問題,請務必刪除url中的空白空間。謝謝。 –

回答

0
public class StreamAudioDemo extends Activity implements OnClickListener, 
OnPreparedListener, OnErrorListener, OnCompletionListener { 

    MediaPlayer mp; 
    ProgressDialog pd; 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Button bt = (Button)findViewById(R.id.play); 
    bt.setOnClickListener(this); 
} 

    @Override 
    public void onPrepared(MediaPlayer mp) { 
     Log.i("StreamAudioDemo", "prepare finished"); 
     pd.setMessage("Playing....."); 
     mp.start(); 
    } 

    @Override 
    public void onClick(View v) { 
     try 
     { 
      pd = new ProgressDialog(this); 
      pd.setMessage("Buffering....."); 
      pd.show(); 
      mp = new MediaPlayer(); 
      mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
      mp.setOnPreparedListener(this); 
      mp.setOnErrorListener(this); 
      mp.setDataSource("http://www.robtowns.com/music/blind_willie.mp3"); 
      mp.prepareAsync(); 
      mp.setOnCompletionListener(this); 
     } 
     catch(Exception e) 
     { 
      Log.e("StreamAudioDemo", e.getMessage()); 
     } 
    } 

    @Override 
    public boolean onError(MediaPlayer mp, int what, int extra) { 
     pd.dismiss(); 
     return false; 
    } 

    @Override 
    public void onCompletion(MediaPlayer mp) { 
     pd.dismiss();A 
     Toast.makeText(getApplicationContext(), "Completed", Toast.LENGTH_LONG).show();  
    } 
} 

這是包含該按鈕的XML。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Stream Audio and Play" 
    android:id="@+id/play" 
    /> 
</LinearLayout 
+0

謝謝Akshay的建議,事實上我找到了解決方案。問題來了,因爲我的網址空間。 –