3

我正在编写一个媒体播放器,它将直播和点播流。它大部分都在工作,但由于某种原因,我无法让 seekTo(int) 正常工作。我的问题是,当我调用 seekTo(int) 时,我没有得到我的 onSeekComplete 回调。我在我的 onPrepared 回调中注册了我的回调方法。

我应该注意到我也在使用 MediaController,并且我已经用 MediaController 注册了我的 MediaPlayer。当我通过点击搜索栏进行搜索时,它会通过我的 MediaController 并且在那种情况下我确实得到了我的 onSeekComplete 回调。这是我的代码。

package com.brockoli.android.hsdroid;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup.LayoutParams;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;
import android.widget.ProgressBar;

public class VideoPlayerActivity extends Activity implements MediaPlayer.OnPreparedListener, 
                                                             MediaPlayer.OnVideoSizeChangedListener, 
                                                             SurfaceHolder.Callback,
                                                             MediaController.MediaPlayerControl,
                                                             OnSeekCompleteListener {

    private MediaPlayer mp;
    private MediaController mc;
    private int width = 0;
    private int height = 0;
    private SurfaceView surface;
    private SurfaceHolder holder;
    private ProgressBar timeline = null;
    private boolean isPaused = false;
    private int mCurrentPosition = 0;

    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.video_player);

        if (getLastNonConfigurationInstance() != null) {
            mCurrentPosition = (Integer) getLastNonConfigurationInstance();         
        }

        mc = new MediaController(this);

        surface = (SurfaceView) findViewById(R.id.surface_video);
        holder = surface.getHolder();
        holder.addCallback(this);

    }

    @Override
    protected void onPause() {
        super.onPause();

        isPaused = true;
    }

    @Override
    protected void onResume() {
        super.onResume();

        width = getScreenWidth();
        height = setHeightBasedOnWidth();

        isPaused = false;

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mp != null) {
            mp.release();
            mp = null;
        }
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        int currentPosition = 0;
        if (mp != null && mp.isPlaying()) {
            currentPosition = mp.getCurrentPosition();
        }
        return currentPosition;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mc.show();
        return false;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Intent intent = this.getIntent();
        String url = intent.getStringExtra("STREAM_ID");

        playVideo(url);

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        LayoutParams lp = surface.getLayoutParams();
        lp.width = width;
        lp.height = height;
        surface.setLayoutParams(lp);
        if (width != 0 && height != 0) {
            holder.setFixedSize(width, height);
            mc.setMediaPlayer(this);
            mc.setAnchorView(surface);

            handler.post(new Runnable() {

                @Override
                public void run() {
                    mc.setEnabled(true);
                    mc.show();
                }
            });

            Log.d("MEDIAPLAYER", "onPrepared start");
            mp.seekTo(mCurrentPosition);
            //mediaPlayer.start();
        }
    }

    private void playVideo(String url) {
            try {
                if (mp == null) {
                    mp = new MediaPlayer();
                    mp.setScreenOnWhilePlaying(true);
                    mp.setOnVideoSizeChangedListener(this);
                    mp.setOnSeekCompleteListener(this);
                } else {
                    mp.stop();
                    mp.reset();
                }

                mp.setDataSource(url);
                mp.setDisplay(holder);
                mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mp.setOnPreparedListener(this);
                mp.prepareAsync();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    @Override
    public void onVideoSizeChanged(MediaPlayer mediaPlayer, int newWidth, int newHeight) {
/*      if (width != 0 && height != 0) {
            holder.setFixedSize(width, height);
            //mp.seekTo(mCurrentPosition);
            Log.d("MEDIAPLAYER", "onVideoSizeChanged start");
            //mediaPlayer.start();
        }
*/      
    }

    private Point getScreenSize() {
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        return size;
    }

    private int getScreenWidth() {
        return getScreenSize().x;
    }

    private int getScreenHeight() {
        return getScreenSize().y;
    }

    private int setHeightBasedOnWidth() {
        if (getScreenWidth() > getScreenHeight()) {
            return getScreenHeight();
        }
        return (int) (getScreenWidth()/1.6);
    }

    @Override
    public boolean canPause() {
        return true;
    }

    @Override
    public boolean canSeekBackward() {
        return true;
    }

    @Override
    public boolean canSeekForward() {
        return true;
    }

    @Override
    public int getBufferPercentage() {
        return 0;
    }

    @Override
    public int getCurrentPosition() {
        return mp.getCurrentPosition();
    }

    @Override
    public int getDuration() {
        return mp.getDuration();
    }

    @Override
    public boolean isPlaying() {
        return mp.isPlaying();
    }

    @Override
    public void pause() {
        mp.pause();
    }

    @Override
    public void seekTo(int pos) {
        mp.seekTo(pos);
    }

    @Override
    public void start() {
        Log.d("MEDIAPLAYER", "MediaController start");
        mp.start();
    }

    @Override
    public void onSeekComplete(MediaPlayer mp) {
        mp.start();
    }

}
4

1 回答 1

1

设备实现 MediaPlayer 有时会有所不同。检查您的 onInfo 侦听器以查看您是否收到反馈。

从评论中编辑:
我自己尝试过,但在我的应用程序中也对我不起作用。对我来说,解决方法是,特别是如果您的媒体在搜索事件开始时已经在播放,如果您看到 currentPosition 再次增加,那么您知道搜索已经完成。

于 2012-10-30T11:09:20.537 回答