我想運行一個簡單的測試程序,從網上Shoutcast的電臺播放音樂,但我總是得到一個錯誤約MediaPlayer的setDataSource()
方法。這是日誌:安卓:無法創建的MediaPlayer
08-26 11:33:42.268 17698-17698/com.example.bruno.radioteste D/MainActivity﹕ Attempt to play http://50.7.98.106:8398
08-26 11:33:42.268 17698-17698/com.example.bruno.radioteste E/MediaPlayer﹕ Unable to create media player
08-26 11:33:42.268 17698-17698/com.example.bruno.radioteste W/System.err﹕ java.io.IOException: setDataSource failed.: status=0x80000000
08-26 11:33:42.278 17698-17698/com.example.bruno.radioteste W/System.err﹕ at android.media.MediaPlayer._setDataSource(Native Method)
的代碼很簡單,從我一直在尋找它應該只是罰款。該佈局的XML:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BassDrive-http://50.7.98.106:8398"
android:onClick="play" />
以及活動類:
public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener {
private static final String TAG = MainActivity.class.getSimpleName();
private MediaPlayer player;
private boolean isPlaying = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
player = new MediaPlayer();
player.setOnPreparedListener(this);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
public void play(View view) {
if(isPlaying) {
player.stop();
isPlaying = false;
} else {
try {
String stationIp = ((Button)view).getText().toString().split("-")[1];
Log.d(TAG, "Attempt to play " + stationIp);
player.setDataSource(stationIp);
player.prepareAsync();
isPlaying = true;
} catch(Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onPrepared(MediaPlayer mp) {
Log.d(TAG, "Trying to start...");
mp.start();
}
@Override
protected void onStop() {
super.onStop();
// clear media player
player.stop();
player.release();
player = null;
}
我認爲這是因爲shoutcast流不支持MediaPlayer格式。嘗試獲取直接的mp3或aac流並使用它。另外,請嘗試使用不同的流,以確保您的代碼正常。 – SteveEdson
shoutcast流支持,我已經解決了問題,shoutcast流正在流:) @SteveEdson – yat0