android: onSeekCompleteListener 与 VideoView

发布于 2024-12-12 03:00:13 字数 236 浏览 0 评论 0原文

我正在使用 VideoView 播放视频文件。我正在使用eekTo函数来从中断的地方播放视频。但是,我想在查找操作完成后执行一些操作。为此,我需要使用 onSeekCompleteListener;但是,VideoView 不支持 onSeekCompleteListener;它可以与 MediaPlayer 一起使用。我的问题是“有什么方法可以将 onSeekCompleteListener 与 VideoView 一起使用吗?”

多谢

I am using VideoView to play video files. I am using seekTo function in order to play the video from where it has been left off. However, I wanted to do some operations when the seek operation is finished. For that I need to use onSeekCompleteListener; however, onSeekCompleteListener is not supported with VideoView; it can be used with MediaPlayer. My Question is that "is there any way by which I can use onSeekCompleteListener" with VideoView?

Thanks alot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月下伊人醉 2024-12-19 03:00:13

无需创建自定义VideoView

您可以从 VideoViewonPrepared 方法访问 MediaPlayer,然后设置 OnSeekCompleteListener,如下所示:

mVideoView.setOnPreparedListener(new OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {

        mp.setOnSeekCompleteListener(new OnSeekCompleteListener() {
            @Override
            public void onSeekComplete(MediaPlayer mp) {
                //TODO: Your code here
            }
        });

    }
});

There is no need to create a custom VideoView.

You can access the MediaPlayer from the onPrepared method of the VideoView and then set the OnSeekCompleteListener, like this :

mVideoView.setOnPreparedListener(new OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {

        mp.setOnSeekCompleteListener(new OnSeekCompleteListener() {
            @Override
            public void onSeekComplete(MediaPlayer mp) {
                //TODO: Your code here
            }
        });

    }
});
凉世弥音 2024-12-19 03:00:13
public class ObservableVideoView extends VideoView
{

private IVideoViewActionListener mVideoViewListener;
private boolean mIsOnPauseMode = false;

public interface IVideoViewActionListener
{
    void onPause();
    void onResume();
    void onTimeBarSeekChanged(int currentTime);
}

public void setVideoViewListener(IVideoViewActionListener listener)
{
    mVideoViewListener = listener;
}

@Override
public void pause()
{
    super.pause();

    if (mVideoViewListener != null)
    {
        mVideoViewListener.onPause();
    }

    mIsOnPauseMode = true;
}

@Override
public void start()
{
    super.start();

    if (mIsOnPauseMode)
    {
        if (mVideoViewListener != null)
        {
            mVideoViewListener.onResume();
        }

        mIsOnPauseMode = false;
    }
}

@Override
public void seekTo(int msec)
{
    super.seekTo(msec);

    if (mVideoViewListener != null)
    {
        mVideoViewListener.onTimeBarSeekChanged(msec);
    }
}

public ObservableVideoView(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public ObservableVideoView(Context context)
{
    super(context);
}

public ObservableVideoView(Context context, AttributeSet attrs, int defStyle) 
{
    super(context, attrs, defStyle);
}

}

你可以监听这样的事件

public class VideoPlayerActivity extends Activity
{
public static final String VIDEO_URL = "VideoUrl";
private String path = "";
private ObservableVideoView mVideoView;

@Override
public void onCreate(Bundle icicle)
{
    super.onCreate(icicle);
    setContentView(R.layout.video_player_activity_layout);
    mVideoView = (ObservableVideoView) findViewById(R.id.videoView1);
    mVideoView.setMediaController(new MediaController(this));
    mVideoView.setVideoViewListener(mVideoViewListener);


    path = getIntent().getStringExtra(VIDEO_URL);       
    if (path == "")
    {
        Toast.makeText(this, "Please edit VideoViewDemo Activity, and set path" + " variable to your media file URL/path", Toast.LENGTH_LONG)
                .show();
    }
    else
    {   
        mVideoView.setVideoPath(path);
        mVideoView.requestFocus();
        mVideoView.start();
    }
}

private IVideoViewActionListener mVideoViewListener = new IVideoViewActionListener()
{
    @Override
    public void onTimeBarSeekChanged(int currentTime)
    {
        //TODO what you want
    }

    @Override
    public void onResume()
    {
        //TODO what you want
    }

    @Override
    public void onPause()
    {
        //TODO what you want
    }
};

public class ObservableVideoView extends VideoView
{

private IVideoViewActionListener mVideoViewListener;
private boolean mIsOnPauseMode = false;

public interface IVideoViewActionListener
{
    void onPause();
    void onResume();
    void onTimeBarSeekChanged(int currentTime);
}

public void setVideoViewListener(IVideoViewActionListener listener)
{
    mVideoViewListener = listener;
}

@Override
public void pause()
{
    super.pause();

    if (mVideoViewListener != null)
    {
        mVideoViewListener.onPause();
    }

    mIsOnPauseMode = true;
}

@Override
public void start()
{
    super.start();

    if (mIsOnPauseMode)
    {
        if (mVideoViewListener != null)
        {
            mVideoViewListener.onResume();
        }

        mIsOnPauseMode = false;
    }
}

@Override
public void seekTo(int msec)
{
    super.seekTo(msec);

    if (mVideoViewListener != null)
    {
        mVideoViewListener.onTimeBarSeekChanged(msec);
    }
}

public ObservableVideoView(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public ObservableVideoView(Context context)
{
    super(context);
}

public ObservableVideoView(Context context, AttributeSet attrs, int defStyle) 
{
    super(context, attrs, defStyle);
}

}

you can listen for events like this:

public class VideoPlayerActivity extends Activity
{
public static final String VIDEO_URL = "VideoUrl";
private String path = "";
private ObservableVideoView mVideoView;

@Override
public void onCreate(Bundle icicle)
{
    super.onCreate(icicle);
    setContentView(R.layout.video_player_activity_layout);
    mVideoView = (ObservableVideoView) findViewById(R.id.videoView1);
    mVideoView.setMediaController(new MediaController(this));
    mVideoView.setVideoViewListener(mVideoViewListener);


    path = getIntent().getStringExtra(VIDEO_URL);       
    if (path == "")
    {
        Toast.makeText(this, "Please edit VideoViewDemo Activity, and set path" + " variable to your media file URL/path", Toast.LENGTH_LONG)
                .show();
    }
    else
    {   
        mVideoView.setVideoPath(path);
        mVideoView.requestFocus();
        mVideoView.start();
    }
}

private IVideoViewActionListener mVideoViewListener = new IVideoViewActionListener()
{
    @Override
    public void onTimeBarSeekChanged(int currentTime)
    {
        //TODO what you want
    }

    @Override
    public void onResume()
    {
        //TODO what you want
    }

    @Override
    public void onPause()
    {
        //TODO what you want
    }
};

}

相对绾红妆 2024-12-19 03:00:13

Farhan,你是对的,VideoView 不支持 onSeekCompleteListener。

但您可以复制并本地自定义 VideoView 类以自行添加此支持。

我在我对7990784的回答。

Farhan, you are correct, onSeekCompleteListener is not supported by VideoView.

But you can copy and locally customize the VideoView class to add this support yourself.

I show how to do this in my answer to 7990784.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文