我正在制作一个简单的应用程序来播放在线广播流.我知道如何设置流.到目前为止,这是我的代码.它设置流并播放它.但如果我停下来,我就无法重新开始.我错过了一些明显的东西吗
final MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); try { mediaPlayer.setDataSource(url); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException 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(); } try { mediaPlayer.prepare(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // might take long! (for buffering,etc) streamButton = (ToggleButton) findViewById(R.id.playstopstream); streamButton.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0,final boolean isChecked) { // TODO Auto-generated method stub new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub if(isChecked) { mediaPlayer.start(); } else { mediaPlayer.stop(); mediaPlayer.reset(); } }
解决方法
当您调用mediaPlayer.reset()时,mediaPlayer将进入IDLE状态.
要进行播放,您应该在开始之前进行初始化以准备.
要进行播放,您应该在开始之前进行初始化以准备.
您可以在此处查看媒体播放器状态:
http://developer.android.com/reference/android/media/MediaPlayer.html.