2

Want to do a audio streaming for a URL. The same URL is playing a live radio while running on the mobile browser. But while using MediaPlayer for android app there is no output. It gives the following error.

    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

My code sample is :

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

2 回答 2

0

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

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

于 2012-09-07T11:03:39.197 回答
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;
}
}
于 2013-02-14T07:12:29.173 回答