Android 播放視訊 VideoView 《第一行程式碼》學習筆記
阿新 • • 發佈:2019-02-10
視訊播放需要使用VideoView類來實現,以下是一些VideoView的常用方法:
方法名 | 功能描述 |
---|---|
setVideoPath() | 設定要播放的視訊檔案位置 |
start() | 開始或繼續播放視訊 |
pause() | 繼續播放視訊 |
resume() | 將視訊重頭開始播放 |
seekTo() | 從指定位置開始播放視訊 |
isPlaying() | 判斷當前是否正在播放 |
getDuration() | 採取載入視訊的時長 |
(1)修改activity_layout,加入一個視訊播放用的VideoView和3個分別控制視訊播放、暫停和重播的按鈕,為了使按鈕的點選事件寫起來簡便,這裡的按鈕我添加了onClick屬性(具體用法可參考 http://blog.csdn.net/htwhtw123/article/details/52456173):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<Button
android:id="@+id/play"
android:layout_width ="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Play"
android:onClick="playClick"/>
<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pause"
android:onClick="pauseClick"/>
<Button
android:id="@+id/replay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Replay"
android:onClick="replayClick"/>
</LinearLayout>
</LinearLayout>
(2)MainActivity程式碼如下:
import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.VideoView;
public class MainActivity extends Activity {
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = (VideoView) findViewById(R.id.video_view);
inintVideoPath();
}
private void inintVideoPath() {
File file = new File(Environment.getExternalStorageDirectory(),
"0021.mp4");//這裡兩個引數分別需要些檔案所在位置路徑和檔案全名
videoView.setVideoPath(file.getPath());
//指定視訊檔案路徑
}
public void playClick(View v){
if(!videoView.isPlaying()){
videoView.start();//開始播放
}
}
public void pauseClick(View v){
if(videoView.isPlaying()){
videoView.pause();//暫停播放
}
}
public void replayClick(View v){
if(videoView.isPlaying()){
videoView.resume();//重新播放
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(videoView!= null){
videoView.suspend();
}
}
}
(3)不知道是不是手機比較特殊,我三星需要新增讀寫sd卡許可權才能正常播放,而《第一行程式碼》上卻沒有寫走一步:在AndroidManifest.xml里加入該許可權:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
(4)執行效果:
但是將手機橫屏後就無法播放了,然後我參考了一下《Android從入門到精通》,提供的例子橫豎屏均可播放,而且添加了一個MediaController,沒有按鈕
(1)MainActivity
package com.example.videoplay;
import java.io.File;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Environment;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends Activity {
private VideoView video;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
video = (VideoView) findViewById(R.id.video);
String path = Environment.getExternalStorageDirectory()+"/0021.mp4";
File file = new File(path);
MediaController mc = new MediaController(MainActivity.this);
if(file.exists()){
video.setVideoPath(file.getAbsolutePath());
video.setMediaController(mc);
video.requestFocus();
try{
video.start();
}catch(Exception e){
e.printStackTrace();
}
video.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO 自動生成的方法存根
Toast.makeText(MainActivity.this, "視訊播放完了",
Toast.LENGTH_SHORT).show();
}
});
}else{
Toast.makeText(MainActivity.this, "視訊檔案不存在", Toast.LENGTH_SHORT).show();
}
}
}
(2)activity_layout裡就只有VideoView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<VideoView
android:id="@+id/video"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="center"/>
</LinearLayout>
(3)sd卡讀寫許可權也是我自己加的,可能確實是手機有別,書上依舊沒有寫:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
(4)執行效果
豎屏
橫屏