android多媒體類VideoView使用方法詳解
一、概述
VideoView類將視頻的顯示和控制集于一身,我們可以借助它完成一個簡易的視頻播放器。VideoView和MediaPlayer也比較相似。
二、VideoView的使用方法
它主要有以下幾種常用方法

步驟:
1.指定視頻文件的路徑,
2.接下來調(diào)用start()方法就可以開始播放視頻,pause()方法就會暫停播放,resume()方法就會重新播放
注:獲取視頻文件也需要運行時權(quán)限,所有相關(guān)邏輯也需要寫。
最后不要忘記在AndroidManifest.xml文件中聲明用到的權(quán)限
下面是一個比較簡單的播放、暫停、重新播放的小demo
一、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.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.lk.playvideotest.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Play"
android:textAllCaps="false"/>
<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pause"
android:textAllCaps="false"/>
<Button
android:id="@+id/replay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RePlay"
android:textAllCaps="false"/>
</LinearLayout>
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
二、activity中的代碼
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = (VideoView) findViewById(R.id.video_view);
Button play = (Button) findViewById(R.id.play);
Button pause = (Button) findViewById(R.id.pause);
Button replay = (Button) findViewById(R.id.replay);
play.setOnClickListener(this);
pause.setOnClickListener(this);
replay.setOnClickListener(this);
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
initVideoPath();
}
}
private void initVideoPath() {
File file = new File(Environment.getExternalStorageDirectory(), "movie.mp4");
videoView.setVideoPath(file.getPath());//指定視頻文件的路徑
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initVideoPath();
} else {
Toast.makeText(this, "拒絕權(quán)限將無法使用程序", Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play:
if (!videoView.isPlaying()) {
videoView.start();//開始播放
}
break;
case R.id.pause:
if (!videoView.isPlaying()) {
videoView.pause();//暫停播放
}
break;
case R.id.replay:
if (!videoView.isPlaying()) {
videoView.resume();//重新播放
}
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (videoView != null) {
videoView.suspend();//將VideoView所占用的資源釋放掉
}
}
}
三、聲明權(quán)限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
VideoView并不是一個萬能的視頻播放工具類,如果使用它,編寫一個功能十分強(qiáng)大的視頻播放器是不太現(xiàn)實的。VideoView適用于一些游戲的片頭動畫,或者某個應(yīng)用的視頻宣傳。
- Android UniversalVideoView實現(xiàn)視頻播放器
- Android音視頻開發(fā)之VideoView使用指南
- Android自定義videoview仿抖音界面
- Android原生視頻播放VideoView的使用
- Android編程實現(xiàn)VideoView循環(huán)播放功能的方法
- Android多媒體之VideoView視頻播放器
- Android VideoView類實例講解
- Android使用VideoView播放本地視頻和網(wǎng)絡(luò)視頻的方法
- android使用videoview播放視頻
- Android中VideoView音視頻開發(fā)的實現(xiàn)
相關(guān)文章
Android自定義ActionProvider ToolBar實現(xiàn)Menu小紅點
這篇文章主要介紹了Android自定義ActionProvider ToolBar實現(xiàn)Menu小紅點,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
android利用websocket協(xié)議與服務(wù)器通信
這篇文章主要為大家詳細(xì)介紹了android利用websocket協(xié)議與服務(wù)器通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
Android HorizontalScrollView左右滑動效果
這篇文章主要為大家詳細(xì)介紹了Android HorizontalScrollView左右滑動效果的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
MPAndroidChart自定義圖表Chart的Attribute及Render繪制邏輯
這篇文章主要為大家介紹了MPAndroidChart自定義圖表Chart的Attribute及Render繪制邏輯,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android自定義TextView實現(xiàn)文字傾斜效果
有時候Android自帶的控件無法滿足我們的某些要求,這時就需要我們自定義控件來實現(xiàn)這些功能。比如在實際開發(fā)應(yīng)用中,我們有時需要將TextView的文字傾斜一定的角度,就需要自定義TextView。下面這篇文章就給大家介紹了利用Android TextView如何實現(xiàn)文字傾斜效果。2016-11-11
Android使用setCustomTitle()方法自定義對話框標(biāo)題
Android有自帶的對話框標(biāo)題,但是不太美觀,如果要給彈出的對話框設(shè)置一個自定義的標(biāo)題,使用AlertDialog.Builder的setCustomTitle()方法非常方便,接下來通過本文給大家介紹Android使用setCustomTitle()方法自定義對話框標(biāo)題,感興趣的朋友一起學(xué)習(xí)吧2016-02-02
Android ListView滑動刪除操作(SwipeListView)
這篇文章主要為大家詳細(xì)介紹了Android ListView滑動刪除操作,主要是學(xué)習(xí)SwipeListView開源框架。感興趣的小伙伴們可以參考一下2016-08-08

