Android 音影片開發 - VideoView
本篇文章主要介紹下Android 中的VideoView.
1: VideoView簡介
VideoView是一個用於播放影片的檢視元件,可以方便地在應用程式中播放本地或網路上的影片檔案。
VideoView可以直接在佈局檔案中使用,也可以在程式碼中動態建立。
它封裝了MediaPlayer和SurfaceView,提供了簡單的介面來控制影片的播放和顯示。
它提供了一系列方法來控制影片的播放、暫停、停止等操作,並且支援全屏播放和影片控制器的顯示。
VideoView播放影片非常簡單,只需要指定影片的URL或本地路徑.
2: 使用
以下是VideoView的簡單使用:
2.1 佈局
在XML佈局檔案中新增VideoView元件.
<VideoView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="@+id/videoview"
/>
2.2 設定影片源
程式碼如下:
videoView = findViewById(R.id.videoview);
videoView.setVideoPath("sdcard/test.mp4");
除了setVideoPath外,我們還可以呼叫:
- setVideoURI(Uri uri)
- setVideoURI(Uri uri, Map<String, String> headers)
當然不管是setVideoPath或者setVideoURI實際都是執行的setVideoURI(Uri uri, Map<String, String> headers).
原始碼如下:
/**
* Sets video path.
*
* @param path the path of the video.
*/
public void setVideoPath(String path) {
setVideoURI(Uri.parse(path));
}
/**
* Sets video URI.
*
* @param uri the URI of the video.
*/
public void setVideoURI(Uri uri) {
setVideoURI(uri, null);
}
2.3 播放影片
videoView.start();
我們可以看下start()的原始碼:
@Override
public void start() {
if (isInPlaybackState()) {
mMediaPlayer.start();
mCurrentState = STATE_PLAYING;
}
mTargetState = STATE_PLAYING;
}
可以看到實際上呼叫mMediaPlayer.start();另外設定了當前的狀態為STATE_PLAYING.
這裡直接呼叫了mMediaPlayer.start();那mMediaPlayer是什麼時機初始化的呢?
檢視原始碼可以看到:
private void openVideo() {
if (mUri == null || mSurfaceHolder == null) {
// not ready for playback just yet, will try again later
return;
}
// we shouldn't clear the target state, because somebody might have
// called start() previously
release(false);
if (mAudioFocusType != AudioManager.AUDIOFOCUS_NONE) {
// TODO this should have a focus listener
mAudioManager.requestAudioFocus(null, mAudioAttributes, mAudioFocusType, 0 /*flags*/);
}
try {
mMediaPlayer = new MediaPlayer();
// TODO: create SubtitleController in MediaPlayer, but we need
// a context for the subtitle renderers
final Context context = getContext();
final SubtitleController controller = new SubtitleController(
context, mMediaPlayer.getMediaTimeProvider(), mMediaPlayer);
controller.registerRenderer(new WebVttRenderer(context));
controller.registerRenderer(new TtmlRenderer(context));
controller.registerRenderer(new Cea708CaptionRenderer(context));
controller.registerRenderer(new ClosedCaptionRenderer(context));
mMediaPlayer.setSubtitleAnchor(controller, this);
if (mAudioSession != 0) {
mMediaPlayer.setAudioSessionId(mAudioSession);
} else {
mAudioSession = mMediaPlayer.getAudioSessionId();
}
mMediaPlayer.setOnPreparedListener(mPreparedListener);
mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
mMediaPlayer.setOnCompletionListener(mCompletionListener);
mMediaPlayer.setOnErrorListener(mErrorListener);
mMediaPlayer.setOnInfoListener(mInfoListener);
mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
mCurrentBufferPercentage = 0;
mMediaPlayer.setDataSource(mContext, mUri, mHeaders);
mMediaPlayer.setDisplay(mSurfaceHolder);
mMediaPlayer.setAudioAttributes(mAudioAttributes);
mMediaPlayer.setScreenOnWhilePlaying(true);
mMediaPlayer.prepareAsync();
for (Pair<InputStream, MediaFormat> pending: mPendingSubtitleTracks) {
try {
mMediaPlayer.addSubtitleSource(pending.first, pending.second);
} catch (IllegalStateException e) {
mInfoListener.onInfo(
mMediaPlayer, MediaPlayer.MEDIA_INFO_UNSUPPORTED_SUBTITLE, 0);
}
}
// we don't set the target state here either, but preserve the
// target state that was there before.
mCurrentState = STATE_PREPARING;
attachMediaController();
} catch (IOException ex) {
Log.w(TAG, "Unable to open content: " + mUri, ex);
mCurrentState = STATE_ERROR;
mTargetState = STATE_ERROR;
mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
return;
} catch (IllegalArgumentException ex) {
Log.w(TAG, "Unable to open content: " + mUri, ex);
mCurrentState = STATE_ERROR;
mTargetState = STATE_ERROR;
mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
return;
} finally {
mPendingSubtitleTracks.clear();
}
}
可以看到openVideo()
- release()方法釋放正在播放的影片.
- 初始化mMediaPlayer,傳入Uri,設定狀態 STATE_PREPARING。
- attachMediaController()繫結MediaPlayer與VideoView。
最後openVideo()則是在setVideoURI(Uri uri, Map<String, String> headers)內呼叫。
這樣其實已經可以播放指定的影片了。
下面的方法可選。
2.4 MediaController控制器
MediaController是一個用於控制媒體播放器的檢視元件。
MediaController的使用步驟如下:
- 建立一個MediaController物件:MediaController mediaController = new MediaController(context);
- 將MediaController與媒體播放器元件關聯:mediaController.setMediaPlayer(mediaPlayer);
- 將MediaController新增到佈局中:layout.addView(mediaController);
videoView.setMediaController(new MediaController(this));
videoView.start().
直接呼叫setMediaController,執行後我們可以看到與之前直接呼叫start()的區別就是多了個控制器的顯示。其中包含一組常用的媒體控制按鈕,如播放/暫停、快進/快退、前進/後退等,並且可以與MediaPlayer或VideoView等媒體播放器元件進行關聯.
我們可以看下原始碼:
public void setMediaController(MediaController controller) {
if (mMediaController != null) {
mMediaController.hide();
}
mMediaController = controller;
attachMediaController();
}
可以看到做的操作如下:
- 如果存在mMediaController,則呼叫hide方法。
- 對mMediaController賦值
- attachMediaController
本文由部落格一文多發平臺 OpenWrite 釋出!