Android:无法创建MediaPlayer

时间:2015-08-26 11:41:35

标签: java android android-mediaplayer android-logcat

我正在尝试运行一个简单的测试应用来播放来自在线shoutcast收音机的音乐,但我总是收到有关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) 方法的错误。这是日志:

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="BassDrive-http://50.7.98.106:8398"
    android:onClick="play" />

代码非常简单,从我一直在搜索它应该工作得很好..布局的xml:

 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;
}

活动类:

{{1}}

1 个答案:

答案 0 :(得分:3)

有两种可能的情况,

您是否在清单中添加了此权限?

<uses-permission android:name="android.permission.INTERNET" />

如果是,则android无法解码您的流。要测试这个,您可以尝试其他一些流。例如,尝试spotify 30秒预览流http://d318706lgtcm8e.cloudfront.net/mp3-preview/d9d8686acc79916d2db76abdaa2c18a594fed332