6

我没有多少经验,所以我从某个地方复制了代码;但现在我可以获得状态媒体播放器;我要做的就是获取媒体播放器的当前状态,然后在播放时显示暂停按钮;暂停/缓冲时播放按钮;

我在其中使用了直播;我想在最初加载(缓冲)时显示暂停按钮或加载状态;当它被加载并开始播放暂停按钮时;当由于某种原因它再次开始缓冲时,它应该在主要活动上显示播放按钮或加载状态

这是我的代码;

   public class Myradio extends AsyncTask implements OnBufferingUpdateListener {
    private static Context mContext;
    // private static MyProgressDialog pdialog;
    public static MediaPlayer mp = new MediaPlayer();
    private MediaPlayer mpLoop = new MediaPlayer();

    public Myradio(Context theContext) {
        mContext = theContext;
        mp.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {

            public void onBufferingUpdate(MediaPlayer arg0, int arg1) {
                Log.v("Buffring Update", "");
                // TODO Auto-generated method stub

            }
        });

    }

    public static void startRadio(String streamUrl) {
        mp.reset();

        mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.e(getClass().getName(), "Error in MediaPlayer: (" + what
                        + ") with extra (" + extra + ")");
                return false;
            }
        });
        try {
            mp.setDataSource(streamUrl);
            mp.prepare();
            mp.start();

        } catch (IllegalArgumentException e) {

        } catch (IllegalStateException e) {

        } catch (IOException e) {

        }
    }

    public static void stopRadio() {

        mp.stop();

    }

    public static void resumeRadio() {

        // mp.();
        // mp.setLooping(false);
        // mpLoop.stop();

    }

    @Override
    protected Object doInBackground(Object... params) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        // TODO Auto-generated method stub
        Log.v("Buffring Update", "Buffring Update");
    }

}
4

1 回答 1

9

你可以这样做(见这里):

if(MediaPlayer.isPlaying()){
  //show the pause button
}
else{
  //show the play button
}

也检查一下

于 2013-08-01T11:34:31.060 回答