在Android中获取音频流错误(使用URL)

时间:2012-09-07 10:26:36

标签: android audio-streaming android-mediaplayer

想要为网址进行音频流式传输。在移动浏览器上运行时,相同的URL正在播放实时广播。但是在Android应用程序中使用MediaPlayer时没有输出。它给出了以下错误。

    09-07 05:16:37.539: E/MediaPlayer(1265): error (1, -2147483648)
09-07 05:16:37.539: W/System.err(1265): java.io.IOException: Prepare failed.: status=0x1

我的代码示例是:

try {
        Log.i("Audio Streaming", "start-->");
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(URL_OF_AUDIO);
        mediaPlayer.prepare();
        mediaPlayer.start();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

2 个答案:

答案 0 :(得分:0)

确保传递的URL有效并检查androidmanifest是否具有访问权限。

使用onprepared listener启动视频以避免一些不必要的异常。

答案 1 :(得分:0)

试试这个它工作正常........

public class test extends Activity implements OnErrorListener, OnPreparedListener {

private MediaPlayer player;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    player = new MediaPlayer();
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        player.setDataSource("http://www.hubharp.com/web_sound/BachGavotte.mp3");
        player.setOnErrorListener(this);
        player.setOnPreparedListener(this);
        player.prepareAsync();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }       
}
@Override
public void onDestroy() {
    super.onDestroy();
    player.release();
    player = null;
}
@Override
public void onPrepared(MediaPlayer play) {
    play.start();
}
@Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
    return false;
}
}