美文网首页
2018-05-23安卓双击播放与停止视频

2018-05-23安卓双击播放与停止视频

作者: lyblyblyblin | 来源:发表于2018-05-23 14:24 被阅读0次
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:keepScreenOn="true">
    
        <SurfaceView
            android:id="@+id/surfaceView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </RelativeLayout>
    
    package com.example.lybly.myapplication;
    
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.SurfaceView;
    import android.view.View;
    import android.widget.TextView;
    
    import org.videolan.libvlc.IVLCVout;
    import org.videolan.libvlc.LibVLC;
    import org.videolan.libvlc.Media;
    import org.videolan.libvlc.MediaPlayer;
    
    
    public class MainActivity extends AppCompatActivity {
    
        private IVLCVout vlcVout;
        private MediaPlayer mediaPlayer;
    
        private SurfaceView surfaceView;
        private long totalTime = 0;
        private IVLCVout.Callback callback;
        private MediaPlayer.EventListener eventListener = new MediaPlayer.EventListener() {
            @Override
            public void onEvent(MediaPlayer.Event event) {
                try {
                    if (event.getTimeChanged() == 0 || totalTime == 0 || event.getTimeChanged() > totalTime) {
                        return;
                    }
    //可以用来刷新播放进度条
    //播放结束
                    if (mediaPlayer.getPlayerState() == Media.State.Ended) {
                        mediaPlayer.setTime(0);
                        mediaPlayer.stop();
                        mediaPlayer.pause();
                    }
                } catch (Exception e) {
                    Log.d("vlc-event", e.toString());
                }
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            surfaceView = findViewById(R.id.surfaceView);
    
            LibVLC libvlc = LibVLCUtil.getLibVLC(null);
    
            mediaPlayer = new MediaPlayer(libvlc);
            vlcVout = mediaPlayer.getVLCVout();
    
    //视频信息,设置视频播放器窗口
            callback = new IVLCVout.Callback() {
                @Override
                public void onNewLayout(IVLCVout ivlcVout, int i, int i1, int i2, int i3, int i4, int i5) {
                    try {
    //获取播放时长,长宽
                        totalTime = mediaPlayer.getLength();
    //                    videoWidth = i;
    //                    videoHight = i1;
    
    
                    } catch (Exception e) {
                        Log.d("vlc-newlayout", e.toString());
                    }
                }
    
                @Override
                public void onSurfacesCreated(IVLCVout ivlcVout) {
    
                }
    
                @Override
                public void onSurfacesDestroyed(IVLCVout ivlcVout) {
    
                }
            };
            vlcVout.addCallback(callback);
            vlcVout.setVideoView(surfaceView);
            vlcVout.attachViews();
    
            Media media;
    //远程播放还是本地播放
            media = new Media(libvlc, "/sdcard/a.flv");
            mediaPlayer.setMedia(media);
    
    
    //播放中的监听器,播放时候不同时间的监听器
            mediaPlayer.setEventListener(eventListener);
    
            surfaceView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    long secondTime=System.currentTimeMillis();
    
                    if (mediaPlayer.isPlaying()) {
    
                        if((secondTime-firstStopTime)<2000){
                            mediaPlayer.pause();
                            firstPlayTime=0;
                        }else {
                            firstStopTime=secondTime;
                        }
                    } else {
                        if((secondTime-firstPlayTime)<2000){
                            mediaPlayer.play();
                            firstPlayTime=0;
                        }else {
                            firstPlayTime=secondTime;
                        }
                    }
                }
            });
    
            mediaPlayer.play();
    
        }
        //记录用户首次点击返回键的时间
        private long firstPlayTime=0;
        private long firstStopTime=0;
        private void pausePlay() {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
            }
    
            vlcVout.detachViews();
            vlcVout.removeCallback(callback);
            mediaPlayer.setEventListener(null);
        }
    
        private void resumePlay() {
            vlcVout.setVideoView(surfaceView);
            vlcVout.attachViews();
    
            vlcVout.addCallback(callback);
            mediaPlayer.setEventListener(eventListener);
        }
    
        @Override
        protected void onDestroy() {
            try {
                super.onDestroy();
    
                pausePlay();
                mediaPlayer.release();
            } catch (Exception e) {
                Log.d("vlc-destroy", e.toString());
            }
        }
    
        @Override
        protected void onStop() {
            try {
                super.onStop();
                pausePlay();
    
            } catch (Exception e) {
                Log.d("vlc-stop", e.toString());
            }
        }
    
        @Override
        protected void onPause() {
            try {
                super.onPause();
                pausePlay();
    
            } catch (Exception e) {
                Log.d("vlc-pause", e.toString());
            }
        }
    
        @Override
        protected void onResume() {
            try {
                super.onResume();
                resumePlay();
    
            } catch (Exception e) {
                Log.d("vlc-resume", e.toString());
            }
        }
    
        @Override
        protected void onNewIntent(Intent intent) {
            try {
                super.onNewIntent(intent);
            } catch (Exception e) {
                Log.d("vlc-newintent", e.toString());
            }
        }
    
        @Override
        public void onBackPressed() {
            try {
                super.onBackPressed();
    
                finish();
            } catch (Exception e) {
                Log.d("vlc-back", e.toString());
            }
        }
    }
    
    
    package com.example.lybly.myapplication;
    
    import org.videolan.libvlc.LibVLC;
    
    import java.util.ArrayList;
    
    /**
     * LibVLCUtil LibVLC 单例
     */
    public class LibVLCUtil {
        private static LibVLC libVLC = null;
    
        public synchronized static LibVLC getLibVLC(ArrayList<String> options) throws IllegalStateException {
            if (libVLC == null) {
                if (options == null) {
                    libVLC = new LibVLC();
                } else {
                    libVLC = new LibVLC(options);
                }
            }
            return libVLC;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:2018-05-23安卓双击播放与停止视频

          本文链接:https://www.haomeiwen.com/subject/cqyrjftx.html