本文將講解如何使用Android視頻播放器VideoView組件來(lái)播放本地視頻和網(wǎng)絡(luò)視頻,實(shí)現(xiàn)起來(lái)還是比較簡(jiǎn)單的。VideoView組件的作用與ImageView類似,只是ImageView用于顯示圖片,VideoView用于播放視頻。
(本文章的代碼參考: https://www.jb51.net/article/122051.htm)
實(shí)現(xiàn)效果:
用Android Studio做的簡(jiǎn)單的本地視頻播放器
導(dǎo)出的APK:
鏈接:https://pan.baidu.com/s/1UyqeqqEmEDlILKCP71FtJA
提取碼:9tro
一、創(chuàng)建Android Studio項(xiàng)目
1.File——>New——>New Project
2.
3.
二、在界面布局文件activity_main.xml中定義VideoView組件
activity_main.xml完整代碼:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
tools:context="com.example.simplevideo.MainActivity">
<!--主界面的三個(gè)按鈕和一個(gè)播放控件-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnPlay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Play"
android:textAllCaps="false" />
<Button
android:id="@+id/btnPause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pause"
android:textAllCaps="false" />
<Button
android:id="@+id/btnReplay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Replay"
android:textAllCaps="false" />
</LinearLayout>
<!--VideoView 作為視頻播放時(shí)的顯示位置-->
<VideoView
android:id="@+id/vdvwFilm"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
三、編輯MainActivity.java
基本思路:
- 1.對(duì)界面的按鈕和顯示位置實(shí)例化。
- 2.調(diào)用VideoView的setVideoPath(String path)方法來(lái)加載 本地path 文件所代表的視頻。
本次用到的名為big_buck_bunny.mp4的視頻下載地址為:http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4 需要提前下載到用于測(cè)試的手機(jī)的內(nèi)存根目錄下。
(若想要加載uri所對(duì)應(yīng)的視頻,需要使用setVideoURI(Uri uri)方法加載,在此不作詳細(xì)說(shuō)明。) - 3.對(duì)權(quán)限的取得結(jié)果進(jìn)行判斷,并針對(duì)性操作。
- 4.調(diào)用VideoView的start()、stop()、pause()方法來(lái)控制視頻播放。
- 5.執(zhí)行完畢,釋放所有資源。
實(shí)際上與VideoView—起結(jié)合使用的還有一個(gè)MediaController類,它的作用是提供一個(gè)友好的圖形控制界面,通過(guò)該控制界面來(lái)控制視頻的播放。
MainActivity.java完整代碼:
package com.example.simplevideo;
import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.io.File;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private VideoView videoView;
@Override
//對(duì)界面的按鈕和顯示位置實(shí)例化,并檢查權(quán)限
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = (VideoView)findViewById(R.id.vdvwFilm);
Button btnPlay = (Button)findViewById(R.id.btnPlay);
Button btnPause = (Button)findViewById(R.id.btnPause);
Button btnReplay = (Button)findViewById(R.id.btnReplay);
btnPlay.setOnClickListener(this);
btnPause.setOnClickListener(this);
btnReplay.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();//初始化MediaPlayer
}
}
//用一個(gè)單獨(dú)的方法來(lái)實(shí)現(xiàn)視頻播放初始化
private void initVideoPath() {
//本地的視頻,需要在手機(jī)內(nèi)存根目錄添加一個(gè)名為 big_buck_bunny.mp4 的視頻
File file = new File(Environment.getExternalStorageDirectory(), "big_buck_bunny.mp4");//指定視頻文件路徑
videoView.setVideoPath(file.getPath());//加載path文件代表的視頻
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);//讓視頻循環(huán)播放
}
});
}
@Override
//對(duì)權(quán)限的取得結(jié)果進(jìn)行判斷,并針對(duì)性操作。獲得權(quán)限,執(zhí)行初始化;如果沒(méi)有獲得權(quán)限,提示用戶。
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
initVideoPath();
} else {
Toast.makeText(this, "拒絕權(quán)限,無(wú)法使用程序。", Toast.LENGTH_LONG).show();
finish();
}
break;
default:
break;
}
}
@Override
//統(tǒng)一處理Play(播放)、Pause(暫停)、Replay(重新播放)的邏輯
public void onClick(View v) {
switch (v.getId()){
case R.id.btnPlay:
if(!videoView.isPlaying()){
videoView.start();//播放
}
break;
case R.id.btnPause:
if(videoView.isPlaying()){
videoView.pause();//暫停
}
break;
case R.id.btnReplay:
if(videoView.isPlaying()){
videoView.resume();//重新播放
}
break;
}
}
@Override
//執(zhí)行完畢,釋放所有資源
protected void onDestroy() {
super.onDestroy();
if(videoView != null){
videoView.suspend();
}
}
}
四、在 AndroidManifest.xml 中配置相應(yīng)的權(quán)限
只需要加上
<!--外存儲(chǔ)寫權(quán)限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
AndroidManifest.xml完整代碼:
五、導(dǎo)出apk包
(詳細(xì)參考:https://llw-study.blog.csdn.net/article/details/112288954?spm=1001.2014.3001.5502)
1.
2.
3.沒(méi)有Key的話需要Create new,有的話Choose existing
4.
5.
6.把.apk文件下載到手機(jī)上進(jìn)行安裝即可。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-785390.html
六、對(duì)遇到的坑的思考
在最初,總是遇到無(wú)法播放視頻+黑屏的問(wèn)題,在線播放視頻我也還沒(méi)有實(shí)現(xiàn),視頻最好是自己上傳的,不要用網(wǎng)上的視頻素材,網(wǎng)上的視頻素材人家是做了保護(hù)的措施。還有一種方法,就是使用本地視頻。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-785390.html
- 問(wèn)題分析參考:
https://www.jb51.net/article/143228.htm - Android使用VideoView播放本地視頻和網(wǎng)絡(luò)視頻的方法(里面播放網(wǎng)絡(luò)視頻的方法可以參考):https://www.jb51.net/article/90992.htm
到了這里,關(guān)于零基礎(chǔ)用Android Studio實(shí)現(xiàn)簡(jiǎn)單的本地視頻播放器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!