What's wrong with this audio stream playing code for Android?
I have a problem with this code for live streaming:
package cm.ex.wwd;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
public class AudioStream extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = "http://www.songblasts.c开发者_如何学编程om/songs/hindi/t/three-idiots/01-Aal_Izz_Well-(SongsBlasts.Com).mp3";
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(url);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.start();
} catch (Exception e) {
Log.i("Exception", "Exception in streaming mediaplayer e = " + e);
}
}
}
url you are giving is not a valid one.it is showing 404 NOT FOUND.so it is creating problem for you.give a valid one
Without knowing the exact problem it is difficult to answer. If you do this on your UI Thread you might also run into an application crash as the call to prepare()
might take to long. Better use prepareAsync()
and the associated listeners when using streaming.
As mentioned before, also the URL gives a 404.
i have done little changes in above code. check out this one.
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;
}
精彩评论