Delay in pause video when removing headphones

I am playing video and trying to pause it when headset is removed. But unfortunately there is a time delay of about 1-2 sec after which music/video gets stopped. The same piece of code works fine when I use it for a music player but not for a video player. Can anyone help?

Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
// Register headset receiver
    registerReceiver(headsetReceiver,
            new IntentFilter(Intent.ACTION_HEADSET_PLUG));}

// If headset gets unplugged, stop music and service.
private final BroadcastReceiver headsetReceiver = new BroadcastReceiver() {
    private boolean headsetConnected = false;

    @Override
    public void onReceive(Context context, Intent intent) {

        // Log.v("TAG", "ACTION_HEADSET_PLUG Intent received");
        if (intent.hasExtra("state")) {
            if (headsetConnected && intent.getIntExtra("state", 0) == 0) {
                headsetConnected = false;
                headsetSwitch = 0;

            } else if (!headsetConnected
                    && intent.getIntExtra("state", 0) == 1) {
                headsetConnected = true;
                headsetSwitch = 1;

            }

        }

        switch (headsetSwitch) {
            case (0):
                headsetDisconnected();
                break;
            case (1):
                break;
            default:
                break;
        }
    }

};

private void headsetDisconnected() {
    if(videoInterface!=null)
        videoInterface.stopVideo();

}

Fragment :

public class VideoPlayerFragment extends Fragment implements VideoInterface
{
 @Override
 public void onAttach(Activity activity) {
    super.onAttach(activity);
    ((VideoPlayer)getActivity()).videoInterface = this;
}
 @Override
 public void stopVideo() {   // interface method
    if (mediaPlayer != null && mediaPlayer.isPlaying())
       mediaPlayer.pause();
}

You can always rewind the video;

int currTime = mediaplayer.getcurrentposition();
    int newTime = currTime - 3000; // 3k milliseconds
    mediaplayer.seekTo(newTime);
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Delay in pause video when removing headphones

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录