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

步驟:
1.指定視頻文件的路徑,
2.接下來(lái)調(diào)用start()方法就可以開(kāi)始播放視頻,pause()方法就會(huì)暫停播放,resume()方法就會(huì)重新播放
注:獲取視頻文件也需要運(yùn)行時(shí)權(quán)限,所有相關(guān)邏輯也需要寫(xiě)。
最后不要忘記在AndroidManifest.xml文件中聲明用到的權(quán)限
下面是一個(gè)比較簡(jiǎ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)限將無(wú)法使用程序", 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();//開(kāi)始播放
}
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并不是一個(gè)萬(wàn)能的視頻播放工具類(lèi),如果使用它,編寫(xiě)一個(gè)功能十分強(qiáng)大的視頻播放器是不太現(xiàn)實(shí)的。VideoView適用于一些游戲的片頭動(dòng)畫(huà),或者某個(gè)應(yīng)用的視頻宣傳。
- Android UniversalVideoView實(shí)現(xiàn)視頻播放器
- Android音視頻開(kāi)發(fā)之VideoView使用指南
- Android自定義videoview仿抖音界面
- Android原生視頻播放VideoView的使用
- Android編程實(shí)現(xiàn)VideoView循環(huán)播放功能的方法
- Android多媒體之VideoView視頻播放器
- Android VideoView類(lèi)實(shí)例講解
- Android使用VideoView播放本地視頻和網(wǎng)絡(luò)視頻的方法
- android使用videoview播放視頻
- Android中VideoView音視頻開(kāi)發(fā)的實(shí)現(xiàn)
相關(guān)文章
Android自定義ActionProvider ToolBar實(shí)現(xiàn)Menu小紅點(diǎn)
這篇文章主要介紹了Android自定義ActionProvider ToolBar實(shí)現(xiàn)Menu小紅點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
android利用websocket協(xié)議與服務(wù)器通信
這篇文章主要為大家詳細(xì)介紹了android利用websocket協(xié)議與服務(wù)器通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Android 如何保證service在后臺(tái)不被kill
本文主要介紹了Android 如何保證service在后臺(tái)不被kill的方法。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02
Android HorizontalScrollView左右滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android HorizontalScrollView左右滑動(dòng)效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
MPAndroidChart自定義圖表Chart的Attribute及Render繪制邏輯
這篇文章主要為大家介紹了MPAndroidChart自定義圖表Chart的Attribute及Render繪制邏輯,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android 靜默安裝和智能安裝的實(shí)現(xiàn)方法
靜默安裝就是無(wú)聲無(wú)息的在后臺(tái)安裝apk,沒(méi)有任何界面提示。智能安裝就是有安裝界面,但全部是自動(dòng)的,不需要用戶(hù)去點(diǎn)擊。下面腳本之家小編給大家介紹下Android 靜默安裝和智能安裝的實(shí)現(xiàn)方法,感興趣的朋友一起看看吧2018-01-01
Android自定義TextView實(shí)現(xiàn)文字傾斜效果
有時(shí)候Android自帶的控件無(wú)法滿(mǎn)足我們的某些要求,這時(shí)就需要我們自定義控件來(lái)實(shí)現(xiàn)這些功能。比如在實(shí)際開(kāi)發(fā)應(yīng)用中,我們有時(shí)需要將TextView的文字傾斜一定的角度,就需要自定義TextView。下面這篇文章就給大家介紹了利用Android TextView如何實(shí)現(xiàn)文字傾斜效果。2016-11-11
Android使用setCustomTitle()方法自定義對(duì)話(huà)框標(biāo)題
Android有自帶的對(duì)話(huà)框標(biāo)題,但是不太美觀,如果要給彈出的對(duì)話(huà)框設(shè)置一個(gè)自定義的標(biāo)題,使用AlertDialog.Builder的setCustomTitle()方法非常方便,接下來(lái)通過(guò)本文給大家介紹Android使用setCustomTitle()方法自定義對(duì)話(huà)框標(biāo)題,感興趣的朋友一起學(xué)習(xí)吧2016-02-02
Android ListView滑動(dòng)刪除操作(SwipeListView)
這篇文章主要為大家詳細(xì)介紹了Android ListView滑動(dòng)刪除操作,主要是學(xué)習(xí)SwipeListView開(kāi)源框架。感興趣的小伙伴們可以參考一下2016-08-08

