android Multimedia实战(一)详解SurfaceView,TextureView之播放视频的四种方式

Android视频播放02—SurfaceView实现 Android视频播放02 采用SurfaceView实现 具体后面有源码、可以下载研究、在这里就不做过多解释了 代码如下: MainActivity.java import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import andro 阅读详情


http://blog.csdn.net/king1425/article/details/70766945

这个博主有些简单的demo代码可以参考

Android Multimedia实战系列将聚焦于android Multimedia模块,结合Android5.X到Android7.X或者到后来8.x 9.x最新Multimedia相关的API等,实现录制音视频,播放音视频,截图,录屏,编解码等实用功能!

下面不废话直接入正文: 
github完整demo,欢迎star:https://github.com/WangShuo1143368701/VideoView 
在Android中,我们有四种方式来实现视频的播放: 
1、使用其自带的播放器。指定Action为ACTION_VIEW,Data为Uri,Type为其MIME类型。

2、使用VideoView来播放。在布局文件中使用VideoView结合MediaController来实现对其控制。

3、使用MediaPlayer类和SurfaceView来实现,这种方式很灵活。

4、使用MediaPlayer类和TextureView来实现,这种方式更灵活。

下面看代码示例: 
1、调用其自带的播放器:

Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/Test_Movie.m4v");     
//调用系统自带的播放器    
    Intent intent = new Intent(Intent.ACTION_VIEW);    
    Log.v("URI:::::::::", uri.toString());    
    intent.setDataAndType(uri, "video/mp4");    
    startActivity(intent); 
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2、使用VideoView来实现:

Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/Test_Movie.m4v");    
VideoView videoView = (VideoView)this.findViewById(R.id.video_view);    
videoView.setMediaController(new MediaController(this));    
videoView.setVideoURI(uri);    
videoView.start();    
videoView.requestFocus();
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在介绍三四中方式之前我们先比较一下SurfaceView及TextureView

SurfaceView优点及缺点: 
优点:可以在一个独立的线程中进行绘制,不会影响主线程 
使用双缓冲机制,播放视频时画面更流畅

缺点:Surface不在View hierachy中,它的显示也不受View的属性控制,所以不能进行平移,缩放等变换,也不能放在其它ViewGroup中。SurfaceView 不能嵌套使用

TextureView优点及缺点: 
优点:支持移动、旋转、缩放等动画,支持截图

缺点:必须在硬件加速的窗口中使用,占用内存比SurfaceView高,在5.0以前在主线程渲染,5.0以后有单独的渲染线程。

小结: 
从性能和安全性角度出发,使用播放器优先选SurfaceView。

1、在android 7.0上系统surfaceview的性能比TextureView更有优势,支持对象的内容位置和包含的应用内容同步更新,平移、缩放不会产生黑边。 在7.0以下系统如果使用场景有动画效果,可以选择性使用TextureView

2、由于失效(invalidation)和缓冲的特性,TextureView增加了额外1~3帧的延迟显示画面更新

3、TextureView总是使用GL合成,而SurfaceView可以使用硬件overlay后端,可以占用更少的内存带宽,消耗更少的能量

4、TextureView的内部缓冲队列导致比SurfaceView使用更多的内存

5、SurfaceView: 内部自己持有surface,surface 创建、销毁、大小改变时系统来处理的,通过surfaceHolder 的callback回调通知。当画布创建好时,可以将surface绑定到MediaPlayer中。SurfaceView如果为用户可见的时候,创建SurfaceView的SurfaceHolder用于显示视频流解析的帧图片,如果发现SurfaceView变为用户不可见的时候,则立即销毁SurfaceView的SurfaceHolder,以达到节约系统资源的目的

3、使用MediaPlayer类和SurfaceView来实现

package com.ws.videoview.videoview.view;


import android.content.Context;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

import java.io.IOException;



/**
 * Created by Shuo.Wang on 2017/4/25.
 */

public class SurfaceVideoView extends SurfaceView implements Callback {

    /** 定时暂停 */
    private static final int HANDLER_MESSAGE_PARSE = 0;
    /** 定时循环 */
    private static final int HANDLER_MESSAGE_LOOP = 1;

    private MediaPlayer.OnCompletionListener mOnCompletionListener;
    private MediaPlayer.OnPreparedListener mOnPreparedListener;
    private MediaPlayer.OnErrorListener mOnErrorListener;
    private MediaPlayer.OnSeekCompleteListener mOnSeekCompleteListener;
    private OnInfoListener mOnInfoListener;
    private OnVideoSizeChangedListener mOnVideoSizeChangedListener;
    private OnPlayStateListener mOnPlayStateListener;
    private MediaPlayer mMediaPlayer = null;
    private SurfaceHolder mSurfaceHolder = null;

    private static final int STATE_ERROR = -1;
    private static final int STATE_IDLE = 0;
    private static final int STATE_PREPARING = 1;
    private static final int STATE_PREPARED = 2;
    private static final int STATE_PLAYING = 3;
    private static final int STATE_PAUSED = 4;
    /**
     * PlaybackCompleted状态:文件正常播放完毕,而又没有设置循环播放的话就进入该状态,
     * 并会触发OnCompletionListener的onCompletion
     * ()方法。此时可以调用start()方法重新从头播放文件,也可以stop()停止MediaPlayer,或者也可以seekTo()来重新定位播放位置。
     */
    private static final int STATE_PLAYBACK_COMPLETED = 5;
    /** Released/End状态:通过release()方法可以进入End状态 */
    private static final int STATE_RELEASED = 5;

    private int mCurrentState = STATE_IDLE;
    private int mTargetState = STATE_IDLE;

    private int mVideoWidth;
    private int mVideoHeight;
    //  private int mSurfaceWidth;
    //  private int mSurfaceHeight;

    //  private float mSystemVolumn = -1;
    private int mDuration;
    private Uri mUri;

    //  SurfaceTextureAvailable

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

    public SurfaceVideoView(Context context) {
        super(context);
        initVideoView();
    }

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

    @SuppressWarnings("deprecation")
    protected void initVideoView() {
        //      mTryCount = 0;
        mVideoWidth = 0;
        mVideoHeight = 0;

        getHolder().setFormat(PixelFormat.RGBA_8888); // PixelFormat.RGB_565
        getHolder().addCallback(this);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }

        setFocusable(true);
        setFocusableInTouchMode(true);
        requestFocus();

        mCurrentState = STATE_IDLE;
        mTargetState = STATE_IDLE;
    }

    /** 更新音量 */
    public static float getSystemVolumn(Context context) {
        if (context != null) {
            try {
                AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                int maxVolumn = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                return mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC) * 1.0F / maxVolumn;
            } catch (UnsupportedOperationException e) {

            }
        }
        return 0.5F;
    }

    public void setOnInfoListener(OnInfoListener l) {
        mOnInfoListener = l;
    }

    public void setOnVideoSizeChangedListener(OnVideoSizeChangedListener l) {
        mOnVideoSizeChangedListener = l;
    }

    public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {
        mOnPreparedListener = l;
    }

    public void setOnErrorListener(MediaPlayer.OnErrorListener l) {
        mOnErrorListener = l;
    }

    public void setOnPlayStateListener(OnPlayStateListener l) {
        mOnPlayStateListener = l;
    }

    public void setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener l) {
        mOnSeekCompleteListener = l;
    }

    public static interface OnPlayStateListener {
        public void onStateChanged(boolean isPlaying);
    }

    public void setOnCompletionListener(MediaPlayer.OnCompletionListener l) {
        mOnCompletionListener = l;
    }

    public void setVideoPath(String path) {
        // && MediaUtils.isNative(path)
        if (!TextUtils.isEmpty(path)) {
            mTargetState = STATE_PREPARED;
            openVideo(Uri.parse(path));
        }
    }

    public int getVideoWidth() {
        return mVideoWidth;
    }

    public int getVideoHeight() {
        return mVideoHeight;
    }

    public void reOpen() {
        mTargetState = STATE_PREPARED;
        openVideo(mUri);
    }

    public int getDuration() {
        return mDuration;
    }

    /** 重试 */
    private void tryAgain(Exception e) {
        mCurrentState = STATE_ERROR;
        openVideo(mUri);
    }

    public void start() {
        mTargetState = STATE_PLAYING;
        //可用状态{Prepared, Started, Paused, PlaybackCompleted}
        if (mMediaPlayer != null && (mCurrentState == STATE_PREPARED || mCurrentState == STATE_PAUSED || mCurrentState == STATE_PLAYING || mCurrentState == STATE_PLAYBACK_COMPLETED)) {
            try {
                if (!isPlaying())
                    mMediaPlayer.start();
                mCurrentState = STATE_PLAYING;
                if (mOnPlayStateListener != null)
                    mOnPlayStateListener.onStateChanged(true);
            } catch (IllegalStateException e) {
                tryAgain(e);
            } catch (Exception e) {
                tryAgain(e);
            }
        }
    }

    public void pause() {
        mTargetState = STATE_PAUSED;
        //可用状态{Started, Paused}
        if (mMediaPlayer != null && (mCurrentState == STATE_PLAYING)) {
            try {
                mMediaPlayer.pause();
                mCurrentState = STATE_PAUSED;
                if (mOnPlayStateListener != null)
                    mOnPlayStateListener.onStateChanged(false);
            } catch (IllegalStateException e) {
                tryAgain(e);
            } catch (Exception e) {
                tryAgain(e);
            }
        }
    }

    /** 处理音量键 */
    public void dispatchKeyEvent(Context context, KeyEvent event) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_VOLUME_DOWN:
            case KeyEvent.KEYCODE_VOLUME_UP:
                setVolume(getSystemVolumn(context));
                break;
        }
    }

    public void setVolume(float volume) {
        //可用状态{Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted}
        if (mMediaPlayer != null && (mCurrentState == STATE_PREPARED || mCurrentState == STATE_PLAYING || mCurrentState == STATE_PAUSED || mCurrentState == STATE_PLAYBACK_COMPLETED)) {
            try {
                mMediaPlayer.setVolume(volume, volume);
            } catch (Exception e) {

            }
        }
    }

    //  public float getSystemVolume() {
    //      return mSystemVolumn;
    //  }

    public void setLooping(boolean looping) {
        //可用状态{Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted}
        if (mMediaPlayer != null && (mCurrentState == STATE_PREPARED || mCurrentState == STATE_PLAYING || mCurrentState == STATE_PAUSED || mCurrentState == STATE_PLAYBACK_COMPLETED)) {
            try {
                mMediaPlayer.setLooping(looping);
            } catch (Exception e) {
            }
        }
    }

    public void seekTo(int msec) {
        //可用状态{Prepared, Started, Paused, PlaybackCompleted}
        if (mMediaPlayer != null && (mCurrentState == STATE_PREPARED || mCurrentState == STATE_PLAYING || mCurrentState == STATE_PAUSED || mCurrentState == STATE_PLAYBACK_COMPLETED)) {
            try {
                if (msec < 0)
                    msec = 0;
                mMediaPlayer.seekTo(msec);
            } catch (IllegalStateException e) {
            } catch (Exception e) {
            }
        }
    }

    /** 获取当前播放位置 */
    public int getCurrentPosition() {
        int position = 0;
        //可用状态{Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}
        if (mMediaPlayer != null) {
            switch (mCurrentState) {
                case STATE_PLAYBACK_COMPLETED:
                    position = getDuration();
                    break;
                case STATE_PLAYING:
                case STATE_PAUSED:
                    try {
                        position = mMediaPlayer.getCurrentPosition();
                    } catch (IllegalStateException e) {
                    } catch (Exception e) {
                    }
                    break;
            }
        }
        return position;
    }

    public boolean isPlaying() {
        //可用状态{Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}
        if (mMediaPlayer != null && mCurrentState == STATE_PLAYING) {
            try {
                return mMediaPlayer.isPlaying();
            } catch (IllegalStateException e) {
            } catch (Exception e) {
            }
        }
        return false;
    }

    /** 调用release方法以后MediaPlayer无法再恢复使用 */
    public void release() {
        mTargetState = STATE_RELEASED;
        mCurrentState = STATE_RELEASED;
        if (mMediaPlayer != null) {
            try {
                mMediaPlayer.release();
            } catch (IllegalStateException e) {
            } catch (Exception e) {
            }
            mMediaPlayer = null;
        }
    }

    public SurfaceHolder getSurfaceHolder() {
        return mSurfaceHolder;
    }

    public void openVideo(Uri uri) {
        if (uri == null || mSurfaceHolder == null || getContext() == null) {
            // not ready for playback just yet, will try again later
            if (mSurfaceHolder == null && uri != null) {
                mUri = uri;
            }
            Log.e("openVideo ws--->","openVideo return");
            return;
        }

        mUri = uri;
        mDuration = 0;

        //Idle 状态:当使用new()方法创建一个MediaPlayer对象或者调用了其reset()方法时,该MediaPlayer对象处于idle状态。
        //End 状态:通过release()方法可以进入End状态,只要MediaPlayer对象不再被使用,就应当尽快将其通过release()方法释放掉
        //Initialized 状态:这个状态比较简单,MediaPlayer调用setDataSource()方法就进入Initialized状态,表示此时要播放的文件已经设置好了。
        //Prepared 状态:初始化完成之后还需要通过调用prepare()或prepareAsync()方法,这两个方法一个是同步的一个是异步的,只有进入Prepared状态,才表明MediaPlayer到目前为止都没有错误,可以进行文件播放。

        Exception exception = null;
        try {
            if (mMediaPlayer == null) {
                mMediaPlayer = new MediaPlayer();
                mMediaPlayer.setOnPreparedListener(mPreparedListener);
                mMediaPlayer.setOnCompletionListener(mCompletionListener);
                mMediaPlayer.setOnErrorListener(mErrorListener);
                mMediaPlayer.setOnVideoSizeChangedListener(mVideoSizeChangedListener);
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mMediaPlayer.setOnSeekCompleteListener(mSeekCompleteListener);
                mMediaPlayer.setOnInfoListener(mInfoListener);
                //          mMediaPlayer.setScreenOnWhilePlaying(true);
                //              mMediaPlayer.setVolume(mSystemVolumn, mSystemVolumn);
                mMediaPlayer.setDisplay(mSurfaceHolder);
            } else {
                mMediaPlayer.reset();
            }
            mMediaPlayer.setDataSource(getContext(), uri);

            //          if (mLooping)
            //              mMediaPlayer.setLooping(true);//循环播放
            mMediaPlayer.prepareAsync();
            // we don't set the target state here either, but preserve the
            // target state that was there before.
            mCurrentState = STATE_PREPARING;
        } catch (IOException ex) {
            exception = ex;
        } catch (IllegalArgumentException ex) {
            exception = ex;
        } catch (Exception ex) {
            exception = ex;
        }
        if (exception != null) {
            mCurrentState = STATE_ERROR;
            if (mErrorListener != null)
                mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }

    private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mCurrentState = STATE_PLAYBACK_COMPLETED;
            //          mTargetState = STATE_PLAYBACK_COMPLETED;
            if (mOnCompletionListener != null)
                mOnCompletionListener.onCompletion(mp);
        }
    };

    MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            //必须是正常状态
            if (mCurrentState == STATE_PREPARING) {
                mCurrentState = STATE_PREPARED;
                try {
                    mDuration = mp.getDuration();
                } catch (IllegalStateException e) {
                }

                try {
                    mVideoWidth = mp.getVideoWidth();
                    mVideoHeight = mp.getVideoHeight();
                } catch (IllegalStateException e) {
                }

                switch (mTargetState) {
                    case STATE_PREPARED:
                        if (mOnPreparedListener != null)
                            mOnPreparedListener.onPrepared(mMediaPlayer);
                        break;
                    case STATE_PLAYING:
                        start();
                        break;
                }
            }
        }
    };

    OnVideoSizeChangedListener mVideoSizeChangedListener = new OnVideoSizeChangedListener() {

        @Override
        public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
            mVideoWidth = width;
            mVideoHeight = height;
            if (mOnVideoSizeChangedListener != null)
                mOnVideoSizeChangedListener.onVideoSizeChanged(mp, width, height);
        }

    };

    OnInfoListener mInfoListener = new OnInfoListener() {

        @Override
        public boolean onInfo(MediaPlayer mp, int what, int extra) {
            if (mOnInfoListener != null)
                mOnInfoListener.onInfo(mp, what, extra);
            return false;
        }
    };

    private MediaPlayer.OnSeekCompleteListener mSeekCompleteListener = new MediaPlayer.OnSeekCompleteListener() {

        @Override
        public void onSeekComplete(MediaPlayer mp) {
            if (mOnSeekCompleteListener != null)
                mOnSeekCompleteListener.onSeekComplete(mp);
        }
    };

    private MediaPlayer.OnErrorListener mErrorListener = new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {
            mCurrentState = STATE_ERROR;
            //          mTargetState = STATE_ERROR;
            //FIX,可以考虑出错以后重新开始
            if (mOnErrorListener != null)
                mOnErrorListener.onError(mp, framework_err, impl_err);

            return true;
        }
    };

    /** 是否可用 */
    public boolean isPrepared() {
        return mMediaPlayer != null && (mCurrentState == STATE_PREPARED);
    }

    public boolean isComplate(){
        return mMediaPlayer != null && (mCurrentState == STATE_PLAYBACK_COMPLETED);
    }

    /** 是否已经释放 */
    public boolean isRelease() {
        return mMediaPlayer == null || mCurrentState == STATE_IDLE || mCurrentState == STATE_ERROR || mCurrentState == STATE_RELEASED;
    }

    private Handler mVideoHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case HANDLER_MESSAGE_PARSE:
                    pause();
                    break;
                case HANDLER_MESSAGE_LOOP:
                    if (isPlaying()) {
                        seekTo(msg.arg1);
                        sendMessageDelayed(mVideoHandler.obtainMessage(HANDLER_MESSAGE_LOOP, msg.arg1, msg.arg2), msg.arg2);
                    }
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };

    /** 定时暂停 */
    public void pauseDelayed(int delayMillis) {
        if (mVideoHandler.hasMessages(HANDLER_MESSAGE_PARSE))
            mVideoHandler.removeMessages(HANDLER_MESSAGE_PARSE);
        mVideoHandler.sendEmptyMessageDelayed(HANDLER_MESSAGE_PARSE, delayMillis);
    }

    /** 暂停并且清除定时任务 */
    public void pauseClearDelayed() {
        pause();
        if (mVideoHandler.hasMessages(HANDLER_MESSAGE_PARSE))
            mVideoHandler.removeMessages(HANDLER_MESSAGE_PARSE);
        if (mVideoHandler.hasMessages(HANDLER_MESSAGE_LOOP))
            mVideoHandler.removeMessages(HANDLER_MESSAGE_LOOP);
    }

    /** 区域内循环播放 */
    public void loopDelayed(int startTime, int endTime) {
        if (mVideoHandler.hasMessages(HANDLER_MESSAGE_PARSE))
            mVideoHandler.removeMessages(HANDLER_MESSAGE_PARSE);
        if (mVideoHandler.hasMessages(HANDLER_MESSAGE_LOOP))
            mVideoHandler.removeMessages(HANDLER_MESSAGE_LOOP);
        int delayMillis = endTime - startTime;
        seekTo(startTime);
        if (!isPlaying())
            start();
        if (mVideoHandler.hasMessages(HANDLER_MESSAGE_LOOP))
            mVideoHandler.removeMessages(HANDLER_MESSAGE_LOOP);
        mVideoHandler.sendMessageDelayed(mVideoHandler.obtainMessage(HANDLER_MESSAGE_LOOP, getCurrentPosition(), delayMillis), delayMillis);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        boolean needReOpen = (mSurfaceHolder == null);
        mSurfaceHolder = holder;
        if (needReOpen) {
            reOpen();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        mSurfaceHolder = holder;
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mSurfaceHolder = null;
        release();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}


   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555

4、使用MediaPlayer类和TextureView来实现,这种方式更灵活。

TextureView特有API 
1 getSurfaceTexture() 
This method returns the SurfaceTexture used by this view.

2 getBitmap(int width, int height) 
This method returns Returns a Bitmap representation of the content of the associated surface texture.

3 getTransform(Matrix transform) 
This method returns the transform associated with this texture view.

4 isOpaque() 
This method indicates whether this View is opaque.

5 lockCanvas() 
This method start editing the pixels in the surface

6 setOpaque(boolean opaque) 
This method indicates whether the content of this TextureView is opaque.

7 setTransform(Matrix transform) 
This method sets the transform to associate with this texture view.

8 unlockCanvasAndPost(Canvas canvas) 
This method finish editing pixels in the surface.

9 setAlpha和setRotation

package com.ws.videoview.videoview.view;

/**
 * Created by Shuo.Wang on 2017/4/25.
 */

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.Surface;
import android.view.TextureView;

import java.io.IOException;

public class TextureVideoView extends TextureView implements TextureView.SurfaceTextureListener {

    private MediaPlayer.OnCompletionListener mOnCompletionListener;
    private MediaPlayer.OnPreparedListener mOnPreparedListener;
    private MediaPlayer.OnErrorListener mOnErrorListener;
    private MediaPlayer.OnSeekCompleteListener mOnSeekCompleteListener;
    private OnPlayStateListener mOnPlayStateListener;
    private MediaPlayer mMediaPlayer = null;
    private SurfaceTexture mSurfaceHolder = null;

    private static final int STATE_ERROR = -1;
    private static final int STATE_IDLE = 0;
    private static final int STATE_PREPARING = 1;
    private static final int STATE_PREPARED = 2;
    private static final int STATE_PLAYING = 3;
    private static final int STATE_PAUSED = 4;
    private static final int STATE_STOP = 5;
    /**
     * PlaybackCompleted状态:文件正常播放完毕,而又没有设置循环播放的话就进入该状态,
     * 并会触发OnCompletionListener的onCompletion
     * ()方法。此时可以调用start()方法重新从头播放文件,也可以stop()停止MediaPlayer,或者也可以seekTo()来重新定位播放位置。
     */
    private static final int STATE_PLAYBACK_COMPLETED = 5;
    /** Released/End状态:通过release()方法可以进入End状态 */
    private static final int STATE_RELEASED = 5;

    private int mCurrentState = STATE_IDLE;
    private int mTargetState = STATE_IDLE;

    private int mVideoWidth;
    private int mVideoHeight;
    //  private int mSurfaceWidth;
    //  private int mSurfaceHeight;

    private float mVolumn = -1;
    private int mDuration;
    private Uri mUri;

    //  SurfaceTextureAvailable

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

    public TextureVideoView(Context context) {
        super(context);
        initVideoView();
    }

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

    public MediaPlayer getMediaPlayer(){
        return mMediaPlayer;
    }

    protected void initVideoView() {
        try {
            AudioManager mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
            mVolumn = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        } catch (UnsupportedOperationException e) {

        }
        //      mTryCount = 0;
        mVideoWidth = 0;
        mVideoHeight = 0;
        setSurfaceTextureListener(this);
        //      setFocusable(true);
        //      setFocusableInTouchMode(true);
        //      requestFocus();
        mCurrentState = STATE_IDLE;
        mTargetState = STATE_IDLE;
    }

    public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) {
        mOnPreparedListener = l;
    }

    public void setOnErrorListener(MediaPlayer.OnErrorListener l) {
        mOnErrorListener = l;
    }

    public void setOnPlayStateListener(OnPlayStateListener l) {
        mOnPlayStateListener = l;
    }

    public void setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener l) {
        mOnSeekCompleteListener = l;
    }

    public static interface OnPlayStateListener {
        public void onStateChanged(boolean isPlaying);
    }

    public void setOnCompletionListener(MediaPlayer.OnCompletionListener l) {
        mOnCompletionListener = l;
    }

    public void setVideoPath(String path) {
//      if (StringUtils.isNotEmpty(path) && MediaUtils.isNative(path)) {
        mTargetState = STATE_PREPARED;
        openVideo(Uri.parse(path));
//      }
    }

    public int getVideoWidth() {
        return mVideoWidth;
    }

    public int getVideoHeight() {
        return mVideoHeight;
    }

    public void reOpen() {
        mTargetState = STATE_PREPARED;
        openVideo(mUri);
    }

    public int getDuration() {
        return mDuration;
    }

    /** 重试 */
    private void tryAgain(Exception e) {
        e.printStackTrace();
        mCurrentState = STATE_ERROR;
        openVideo(mUri);
    }

    public void start() {
        mTargetState = STATE_PLAYING;
        //可用状态{Prepared, Started, Paused, PlaybackCompleted}
        if (mMediaPlayer != null && (mCurrentState == STATE_PREPARED || mCurrentState == STATE_PAUSED || mCurrentState == STATE_PLAYING || mCurrentState == STATE_PLAYBACK_COMPLETED)) {
            try {
                if (!isPlaying())
                    mMediaPlayer.start();
                mCurrentState = STATE_PLAYING;
                if (mOnPlayStateListener != null)
                    mOnPlayStateListener.onStateChanged(true);
            } catch (IllegalStateException e) {
                tryAgain(e);
            } catch (Exception e) {
                tryAgain(e);
            }
        }
    }

    public void pause() {
        mTargetState = STATE_PAUSED;
        //可用状态{Started, Paused}
        if (mMediaPlayer != null && (mCurrentState == STATE_PLAYING || mCurrentState == STATE_PAUSED)) {
            try {
                mMediaPlayer.pause();
                mCurrentState = STATE_PAUSED;
                if (mOnPlayStateListener != null)
                    mOnPlayStateListener.onStateChanged(false);
            } catch (IllegalStateException e) {
                tryAgain(e);
            } catch (Exception e) {
                tryAgain(e);
            }
        }
    }

    public void stop(){
        mTargetState = STATE_STOP;
        if (mMediaPlayer != null && (mCurrentState == STATE_PLAYING || mCurrentState == STATE_PAUSED)) {
            try {
                mMediaPlayer.stop();
                mCurrentState = STATE_STOP;
                if (mOnPlayStateListener != null)
                    mOnPlayStateListener.onStateChanged(false);
            } catch (IllegalStateException e) {
                tryAgain(e);
            } catch (Exception e) {
                tryAgain(e);
            }
        }
    }

    public void setVolume(float volume) {
        //可用状态{Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted}
        if (mMediaPlayer != null && (mCurrentState == STATE_PREPARED || mCurrentState == STATE_PLAYING || mCurrentState == STATE_PAUSED || mCurrentState == STATE_PLAYBACK_COMPLETED)) {
            try {
                mMediaPlayer.setVolume(volume, volume);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void setLooping(boolean looping) {
        //可用状态{Idle, Initialized, Stopped, Prepared, Started, Paused, PlaybackCompleted}
        if (mMediaPlayer != null && (mCurrentState == STATE_PREPARED || mCurrentState == STATE_PLAYING || mCurrentState == STATE_PAUSED || mCurrentState == STATE_PLAYBACK_COMPLETED)) {
            try {
                mMediaPlayer.setLooping(looping);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void seekTo(int msec) {
        //可用状态{Prepared, Started, Paused, PlaybackCompleted}
        if (mMediaPlayer != null && (mCurrentState == STATE_PREPARED || mCurrentState == STATE_PLAYING || mCurrentState == STATE_PAUSED || mCurrentState == STATE_PLAYBACK_COMPLETED)) {
            try {
                if (msec < 0)
                    msec = 0;
                mMediaPlayer.seekTo(msec);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /** 获取当前播放位置 */
    public int getCurrentPosition() {
        int position = 0;
        //可用状态{Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}
        if (mMediaPlayer != null) {
            switch (mCurrentState) {
                case STATE_PLAYBACK_COMPLETED:
                    position = getDuration();
                    break;
                case STATE_PLAYING:
                case STATE_PAUSED:
                    try {
                        position = mMediaPlayer.getCurrentPosition();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
        return position;
    }

    public boolean isPlaying() {
        //可用状态{Idle, Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}
        if (mMediaPlayer != null && mCurrentState == STATE_PLAYING) {
            try {
                return mMediaPlayer.isPlaying();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /** 调用release方法以后MediaPlayer无法再恢复使用 */
    public void release() {
        mTargetState = STATE_RELEASED;
        mCurrentState = STATE_RELEASED;
        if (mMediaPlayer != null) {
            try {
                mMediaPlayer.release();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            mMediaPlayer = null;
        }
    }

    public void openVideo(Uri uri) {
        if (uri == null || mSurfaceHolder == null || getContext() == null) {
            // not ready for playback just yet, will try again later
            if (mSurfaceHolder == null && uri != null) {
                mUri = uri;
            }
            return;
        }

        mUri = uri;
        mDuration = 0;

        //Idle 状态:当使用new()方法创建一个MediaPlayer对象或者调用了其reset()方法时,该MediaPlayer对象处于idle状态。
        //End 状态:通过release()方法可以进入End状态,只要MediaPlayer对象不再被使用,就应当尽快将其通过release()方法释放掉
        //Initialized 状态:这个状态比较简单,MediaPlayer调用setDataSource()方法就进入Initialized状态,表示此时要播放的文件已经设置好了。
        //Prepared 状态:初始化完成之后还需要通过调用prepare()或prepareAsync()方法,这两个方法一个是同步的一个是异步的,只有进入Prepared状态,才表明MediaPlayer到目前为止都没有错误,可以进行文件播放。

        Exception exception = null;
        try {
            if (mMediaPlayer == null) {
                mMediaPlayer = new MediaPlayer();
                mMediaPlayer.setOnPreparedListener(mPreparedListener);
                mMediaPlayer.setOnCompletionListener(mCompletionListener);
                mMediaPlayer.setOnErrorListener(mErrorListener);
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mMediaPlayer.setOnSeekCompleteListener(mSeekCompleteListener);
                //          mMediaPlayer.setScreenOnWhilePlaying(true);
                mMediaPlayer.setVolume(mVolumn, mVolumn);
                mMediaPlayer.setSurface(new Surface(mSurfaceHolder));
            } else {
                mMediaPlayer.reset();
            }
            mMediaPlayer.setDataSource(getContext(), uri);

            //          if (mLooping)
            //              mMediaPlayer.setLooping(true);//循环播放
            mMediaPlayer.prepareAsync();
            // we don't set the target state here either, but preserve the
            // target state that was there before.
            mCurrentState = STATE_PREPARING;
        } catch (IOException ex) {
            exception = ex;
        } catch (IllegalArgumentException ex) {
            exception = ex;
        } catch (Exception ex) {
            exception = ex;
        }
        if (exception != null) {
            exception.printStackTrace();
            mCurrentState = STATE_ERROR;
            if (mErrorListener != null)
                mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        }
    }

    private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mCurrentState = STATE_PLAYBACK_COMPLETED;
            //          mTargetState = STATE_PLAYBACK_COMPLETED;
            if (mOnCompletionListener != null)
                mOnCompletionListener.onCompletion(mp);
        }
    };

    MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            //必须是正常状态
            if (mCurrentState == STATE_PREPARING) {
                mCurrentState = STATE_PREPARED;
                try {
                    mDuration = mp.getDuration();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                }

                try {
                    mVideoWidth = mp.getVideoWidth();
                    mVideoHeight = mp.getVideoHeight();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                }

                switch (mTargetState) {
                    case STATE_PREPARED:
                        if (mOnPreparedListener != null)
                            mOnPreparedListener.onPrepared(mMediaPlayer);
                        break;
                    case STATE_PLAYING:
                        start();
                        break;
                }
            }
        }
    };

    private MediaPlayer.OnSeekCompleteListener mSeekCompleteListener = new MediaPlayer.OnSeekCompleteListener() {

        @Override
        public void onSeekComplete(MediaPlayer mp) {
            if (mOnSeekCompleteListener != null)
                mOnSeekCompleteListener.onSeekComplete(mp);
        }
    };

    private MediaPlayer.OnErrorListener mErrorListener = new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {
            mCurrentState = STATE_ERROR;
            //          mTargetState = STATE_ERROR;
            //FIX,可以考虑出错以后重新开始
            if (mOnErrorListener != null)
                mOnErrorListener.onError(mp, framework_err, impl_err);

            return true;
        }
    };

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        boolean needReOpen = (mSurfaceHolder == null);
        mSurfaceHolder = surface;
        if (needReOpen) {
            reOpen();
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        //画布失效
        mSurfaceHolder = null;
        release();
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }

    /** 是否可用 */
    public boolean isPrepared() {
        //|| mCurrentState == STATE_PAUSED || mCurrentState == STATE_PLAYING
        return mMediaPlayer != null && (mCurrentState == STATE_PREPARED);
    }

    //  /** 是否能即可播放 */
    //  public boolean canStart() {
    //      return mMediaPlayer != null && (mCurrentState == STATE_PREPARED || mCurrentState == STATE_PAUSED);
    //  }

    private static final int HANDLER_MESSAGE_PARSE = 0;
    private static final int HANDLER_MESSAGE_LOOP = 1;

    private Handler mVideoHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case HANDLER_MESSAGE_PARSE:
                    pause();
                    break;
                case HANDLER_MESSAGE_LOOP:
                    if (isPlaying()) {
                        seekTo(msg.arg1);
                        sendMessageDelayed(mVideoHandler.obtainMessage(HANDLER_MESSAGE_LOOP, msg.arg1, msg.arg2), msg.arg2);
                    }
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };

    /** 定时暂停 */
    public void pauseDelayed(int delayMillis) {
        if (mVideoHandler.hasMessages(HANDLER_MESSAGE_PARSE))
            mVideoHandler.removeMessages(HANDLER_MESSAGE_PARSE);
        mVideoHandler.sendEmptyMessageDelayed(HANDLER_MESSAGE_PARSE, delayMillis);
    }

    /** 暂停并且清除定时任务 */
    public void pauseClearDelayed() {
        pause();
        if (mVideoHandler.hasMessages(HANDLER_MESSAGE_PARSE))
            mVideoHandler.removeMessages(HANDLER_MESSAGE_PARSE);
        if (mVideoHandler.hasMessages(HANDLER_MESSAGE_LOOP))
            mVideoHandler.removeMessages(HANDLER_MESSAGE_LOOP);
    }

    /** 区域内循环播放 */
    public void loopDelayed(int startTime, int endTime) {
        int delayMillis = endTime - startTime;
        seekTo(startTime);
        if (!isPlaying())
            start();
        if (mVideoHandler.hasMessages(HANDLER_MESSAGE_LOOP))
            mVideoHandler.removeMessages(HANDLER_MESSAGE_LOOP);
        mVideoHandler.sendMessageDelayed(mVideoHandler.obtainMessage(HANDLER_MESSAGE_LOOP, getCurrentPosition(), delayMillis), delayMillis);
    }
}


   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
顶
0
 
踩
0
 



一步步自定义视频播放器——TextureView+MediaPlayer自定义视频播放器 本篇参考封装一个视频播放器,原文已经写的非常棒了,本篇加入了个人对其内容的理解。秉承不重复造轮子的良好理念,接下来开始拆解轮子。内容非常多,我都差点放弃写,有耐心的请往下看 github上非常棒的视频相关开源项目有: 最炫的哔哩哔哩的ijkplayer JiaoziVideoPlayer 基于ijkplayer GSYVideoPlayer 基于ijkplayer 结合前三篇内容,我们 阅读详情

相关推荐

Android 使用MediaPlayer和SurfaceView播放视频

先创建布局页面activity_media_player_video.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android

路宇的博客 2875

Android SurfaceView和TextureView 利用Mediaplayer播放视频

SurfaceView和View的区别简单概述。 已经讲解了SurfaceView和View的区别,SurfaceView会有自己独立的Surface,虽然它被添加到了View 结构树中,但是它其实不在view 树结构中,不能像普通的View一样可以平移,缩放,旋转等操作。同时SurfaceView不能放在类似RecyclerView或ScrollView中,一些View中的特性也无法使用。 其实...

lidongxiu0714 7445

Android使用MediaPlayer和TextureView实现视频无缝切换

需求描述 比如广告播放,每个视频15秒,视频之间切换的时候,性能不太好的机器可能会黑屏一段时间,体验不太好,接下来我们就是要解决这个黑屏问题。 解决方案 使用两个surfaceView方式,经过测试不行 使用一个MediaPlayer,在MediaPlayer上面加一层ImageView,每次播放完成后,获取视频的最后一帧的图像给ImageView,视频切换完成,ImageView隐藏,如此往复循环,可行 实践 1.获取视频流图片方式,通过MediaMetadataRetriever,测试发现,部分机器获取的Bitmap可能为空,无法解决,放弃 2.使用TextureView方式,可以获取当前

Android使用TextureView播放视频

1.引言 如果你想显示一段在线视频或者任意的数据流比如视频或者OpenGL 场景,你可以用android中的TextureView做到。 1).TextureView的兄弟SurfaceView 应用程序的视频或者opengl内容往往是显示在一个特别的UI控件中:SurfaceView。SurfaceView的工作方式是创建一个置于应用窗口之后的新窗口。这种 方...

dengshuo4753的博客 954

用Python+NumPy手把手实现分数阶傅里叶变换(FRFT),搞定线性调频信号分析

本文详细介绍了如何使用Python和NumPy实现分数阶傅里叶变换(FRFT),特别针对线性调频信号(Chirp信号)分析。通过从零开始的代码实现和可视化案例,展示了FRFT在时变信号处理中的强大能力,帮助工程师和研究人员高效分析频率随时间变化的信号。

weixin_29062823的博客 221

Springboot + vue 实现导出word

Springboot + vue 实现导出word 后端代码 使用word先创建好已知的模板 使用word工具另存为.xml格式的文件 使用freemarker ftl模板 将修改后的xml文件复制到开发工具中,将文件后缀修改为ftl文件,并且将里面的动态值使用${}包裹例如:${zyjn} word工具类 /*** * * Project Name:maventest * <p>生成word工具类<br> * * @ClassName: WordUtil *

qq_45542373的博客 2898

Android视频播放 (一)——TextureView和SurfaceView区别 + 视频播放

转载注明出处:http://blog.csdn.net/u010181592/article/category/5893483文章出自 我不只是看客的博客 TextureView+MediaPlayer

我不只是看客的博客 6709

开发者体验度量:构建DevEx评估体系的方法论与工具

在深入讨论度量方法前,我们需要明确什么是开发者体验(DevEx)。许多人将其简化为"开发工具好不好用",这是一个危险的误解。开发者体验是指开发者在完成工作过程中的整体感受和效率,它由三大核心维度组成:工具与基础设施:开发、测试、部署工具链的效能流程与协作:工作流程的顺畅度和团队协作的效率文化与成长:支持实验、学习和专业发展的环境这三个维度相互作用,共同决定了开发者能否高效且愉悦地完成工作。开发者体验不是一个目的地,而是一段持续的旅程。

2401_88760782的博客 1434

避开Android截图黑屏陷阱:从WindowManager到硬件加速的完整避坑指南

本文深入解析Android开发中截图黑屏问题的根源,从SurfaceView渲染机制到FLAG_SECURE安全策略,提供全面的解决方案。涵盖硬件加速隔离层、WindowManager权限控制及兼容性处理,帮助开发者有效避开截图黑屏陷阱,提升应用体验。

info6的博客 186

BlenderGis安装碰到问题,ImageIO、无法点击和地图不加载

BlenderGis安装碰到问题,ImageIO、无法点击和地图不加载问题

qq_45176349的博客 4975

Abaqus 导出单元刚度矩阵和全局刚度矩阵

Abaqus 导出单元刚度矩阵和全局刚度矩阵

科技芯风 1万+

surfaceview设置不显示可以获取帧_播放视频时如何在视频帧上添加水印

之前的一篇文章中我们介绍了播放视频的时候调整音频的音量,我们能否在播放视频的时候在视频画面上加上水印?有同学可能会说了,我直接用TextureView渲染视频画面,然后在TextureView上盖一层ImageView可以吗?好像显示效果上没有什么问题,但是仔细分析还是不能满足要求?1.ImageView和TextureView有明显的层级关系,如果出现View层级的问题,不太好处理2....

weixin_42363141的博客 716

基于STM32的宠物智能照护嵌入式系统设计

嵌入式系统是实现物联网终端设备本地化感知、决策与执行的核心技术载体,其关键在于多传感器融合、低功耗执行控制与轻量级云边协同。本文围绕环境监测(温湿度、烟雾、余粮)、机电执行(步进电机食盘、继电器风扇)和可靠通信(MQTT+ESP8266)三大技术主线,深入解析STM32F103在资源约束下的外设调度策略、ADC采样抗干扰设计、DHT11时序精准控制及HX711称重滤波算法等工程实践要点。系统强调本地闭环优先原则,确保离线基础功能可用,同时通过阿里云IoT平台实现远程监控与告警联动,为中小型智能硬件项目提供可

weixin_29155599的博客 297

android+视频弹幕,使用SurfaceView实现视频弹幕

本文实例为大家分享了surfaceview视频弹幕展示的具体代码,供大家参考,具体内容如下全部代码如下:package com.example.app2;import android.graphics.canvas;import android.graphics.color;import android.graphics.paint;import android.graphics.pixelfor...

weixin_34652835的博客 404

用angr找到strcpy的栈溢出漏洞

这篇文章其实是分析angr文档里给出的一个挖漏洞的小例子。 原文链接在此:https://docs.angr.io/examples#beginner-vulnerability-discovery-example-strcpy_find 这是第一个用angr在二进制里找可利用条件的教程。第一个例子是一个非常简单的程序。这个angr脚本会从程序的entry到strcpy找到一条路径。(只有我们能够控制strcpy的参数的时候) 为了找到正确的路径,angr需要去算一个密码参数,算这个密码,angr只要2s

^_^ 1224

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Android_SurfaceView视频播放器

Android_SurfaceView视频播放器 所需文件 MainActivity package com.example.a526_realsurfaceview; import android.content.ContentResolver; import android.media.AudioManager; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import andr

qq_54147492的博客 843

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Android几种视频播放方式,VideoView、SurfaceView+MediaPlayer、TextureView+MediaPlayer,以及主流视频播放器开源项目

简单的说下一Android的几种视频播放功能: 1、VideoView:最简单的视频播放 &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="...

Choi晨的博客 1723

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Android使用SurfaceView和TextureView来播放视频

最近项目中在调试一个问题:app端呈现摄像头画面的调整,很坑的一个问题。项目经理估计搞不定,就把这个问题扔给我了,有点坑啊。因为播放摄像头视频的控件是继承于surfaceview的,于是乎就从surfaceview入手,因为这个控件是继承于surfaceview,又稍微做了些封装,结果机智的我就直接写了如下代码: ViewGroup.LayoutParams params = surfaceView

小飞哥的专栏 6910

Android 实现视屏播放器、边播边缓存功能,附源码

热文导读 | 点击标题阅读[墙裂推荐]AndroidVideoCache:实现视屏播放边下边播吊炸天!74款APP完整源码!一份年薪30万的Android面试宝典,附答案来源:http://www.apkbus.com/blog-938789-76574.html效果开源播放器选择Android上最为人熟知的MediaPlayer,对,就是这货,在上两篇音频文章中频频露脸的家伙,这次又有它的身影,

Java和Android架构 3276

安卓磁链搜索下载播放和原理探索

title: 安卓磁链搜索下载播放 date: 2018-11-20 categories: 安卓 tags: 磁链 搜索 下载 播放 magnet comments: false 文章目录安卓磁链搜索下载播放效果图磁力搜索获取磁链返回JSON磁链下载开始查找下载数据下载完成播放器调用迅雷打开磁链原理分析磁力链接torrent种子文件构成成份如下:文件结构TrackerDHT(分布式哈希表...

代码一挖一麻袋 2万+

Android HTTP边下边播

本文简单地分享一下在Android平台做HTTP边下载边播放的一些经验,希望对初学者有所帮助。1. 为什么播放器在播放视频文件的时候,都知道该怎么去解码、该以怎样的时间间隔去显示每一帧呢?因为无论是mp4、avi、flv、mkv还是其他格式的音视频文件,都存在一个称为metadata的数据块,记录了当前文件的图像尺寸、编码格式、帧率、码率等信息,播放器可以通过解析metadata得到这些信息。2....

weixin_34235105的博客 344

Android多媒体功能开发(9)——使用SurfaceView控件+MediaPlayer类播放视频

使用SurfaceView控件+MediaPlayer类播放视频的主要原理是把MediaPlayer对象的显示控件设置为SurfaceView对象。播放控制都是针对MediaPlayer对象,用法与播放音频相同。

nanoage的博客 2928
上一篇: FFmpeg源码调试:解析H.264 SPS(Sequence Parameter Set)中帧的宽高
下一篇: H264码流的两种形式:
STN_LCD
博客等级 码龄15年 384粉丝 54原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址