我正在尝试实施Android的MediaPlayer来收听流媒体网络。要做到这一点,我确实扩展了MediaPlayer,但是当我尝试启动时,播放器抛出异常。我认为流服务器是一个icecast并与ogg一起使用。这是流的网址:http://hdmediaserver2.info:8144/live
我怎么能这样做?
public class CustomMediaPlayer extends MediaPlayer{
private static CustomMediaPlayer INSTANCE = null;
private boolean isPause = false;
public boolean isPause() {
return isPause;
}
public void setPause(boolean isPause) {
this.isPause = isPause;
}
private CustomMediaPlayer() {}
public static CustomMediaPlayer getInstance(
OnCompletionListener onCompeletionListener) {
if (null == INSTANCE) {
INSTANCE = new CustomMediaPlayer();
}
INSTANCE.setOnCompletionListener(onCompeletionListener);
return INSTANCE;
}
@Override
public void reset() {
isPause = false;
super.reset();
}
public void play() throws IllegalStateException, IOException {
if (!isPause) {
super.prepare();
}
super.start();
}
@Override
public void stop() throws IllegalStateException {
isPause = false;
super.stop();
}
@Override
public void pause() throws IllegalStateException {
isPause = true;
super.pause();
}
public void previousOrNext() throws IllegalStateException, IOException {
isPause = false;
play();
}
}
片段
public class RadioFrag extends Fragment implements View.OnClickListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener{
private final String TAG = getClass().getSimpleName() + "->";
private ProgressDialog progressDialog;
private ImageButton btPlay;
private ImageButton btStop;
private ImageButton btVolume;
//media player
private final MediaPlayer MP = CustomMediaPlayer.getInstance(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.radio_frag, container, false);
btPlay = (ImageButton)view.findViewById(R.id.btPlay);
btStop = (ImageButton)view.findViewById(R.id.btStop);
btVolume = (ImageButton)view.findViewById(R.id.btVolume);
//listeners
btPlay.setOnClickListener(this);
btStop.setOnClickListener(this);
btVolume.setOnClickListener(this);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onClick(View v) {
if(v == btPlay){
progressDialog = new CustomProgressDialog().getCustomProgress(null, getView().getContext());
progressDialog.setCancelable(false);
progressDialog.show();
try {
MP.setAudioStreamType(AudioManager.STREAM_MUSIC);
MP.setDataSource("http://hdmediaserver2.info:8144/live.m3u");
MP.setOnErrorListener(this);
MP.setOnPreparedListener(this);
MP.setVolume((float) 50 / 100, (float) 50 / 100);
MP.setLooping(false);
MP.prepareAsync();
} catch (IOException e) {
Log.v("AUDIOHTTPPLAYER", e.getMessage());
}
}
}
@Override
public void onCompletion(MediaPlayer mp) {
progressDialog.dismiss();
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
MP.start();
}
}
异常
05-07 11:43:14.335 1710-1710/br.com.williarts.radios.upacarai V/MediaPlayer-JNI﹕ setAudioStreamType: 3
05-07 11:43:14.335 1710-1710/br.com.williarts.radios.upacarai V/MediaPlayer﹕ MediaPlayer::setAudioStreamType
05-07 11:43:14.405 1710-1710/br.com.williarts.radios.upacarai V/MediaPlayer-JNI﹕ setVolume: left 0.500000 right 0.500000
05-07 11:43:14.405 1710-1710/br.com.williarts.radios.upacarai V/MediaPlayer﹕ MediaPlayer::setVolume(0.500000, 0.500000)
05-07 11:43:14.410 1710-1710/br.com.williarts.radios.upacarai V/MediaPlayer-JNI﹕ setLooping: 0
05-07 11:43:14.410 1710-1710/br.com.williarts.radios.upacarai V/MediaPlayer﹕ MediaPlayer::setLooping
05-07 11:43:14.410 1710-1710/br.com.williarts.radios.upacarai V/MediaPlayer﹕ setVideoSurfaceTexture
05-07 11:43:14.410 1710-1710/br.com.williarts.radios.upacarai V/MediaPlayer﹕ prepareAsync
05-07 11:43:14.640 1710-1710/br.com.williarts.radios.upacarai D/ProgressBar﹕ updateDrawableBounds: left = 0
05-07 11:43:14.640 1710-1710/br.com.williarts.radios.upacarai D/ProgressBar﹕ updateDrawableBounds: top = 0
05-07 11:43:14.640 1710-1710/br.com.williarts.radios.upacarai D/ProgressBar﹕ updateDrawableBounds: right = 96
05-07 11:43:14.645 1710-1710/br.com.williarts.radios.upacarai D/ProgressBar﹕ updateDrawableBounds: bottom = 96
05-07 11:43:16.000 1710-1721/br.com.williarts.radios.upacarai V/MediaPlayer﹕ message received msg=100, ext1=1, ext2=-1004
05-07 11:43:16.000 1710-1721/br.com.williarts.radios.upacarai E/MediaPlayer﹕ error (1, -1004)
05-07 11:43:16.000 1710-1721/br.com.williarts.radios.upacarai V/MediaPlayer﹕ callback application
05-07 11:43:16.010 1710-1721/br.com.williarts.radios.upacarai V/MediaPlayer﹕ back from callback
05-07 11:43:16.015 1710-1710/br.com.williarts.radios.upacarai E/MediaPlayer﹕ Error (1,-1004)