android視頻播放簡單實(shí)現(xiàn)示例(VideoView&MediaPlayer)
如果你看過我的《android音樂播放簡單實(shí)現(xiàn)(MediaPlayer)》,那么本篇將會(huì)毫無壓力。
首先是主界面的三個(gè)按鈕和一個(gè)播放控件
<?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.cofox.myplayvideo.MainActivity"> <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 android:id="@+id/vdvwFilm" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
在 MainActivity.java 中這里需要用到的是 VideoView 作為視頻播放時(shí)的顯示位置。
private VideoView videoView;
在 onCreate 里,對界面的按鈕和顯示位置實(shí)例化,并檢查權(quán)限。
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ú)的方法 initVideoPath() 來實(shí)現(xiàn)視頻播放初始化
private void initVideoPath() {
File file = new File(Environment.getExternalStorageDirectory(), "movie2.mp4");
videoView.setVideoPath(file.getPath());//指定視頻文件路徑
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);//讓電影循環(huán)播放
}
});
}
onRequestPermissionsResult 中對權(quán)限的取得結(jié)果進(jìn)行判斷,并針對性操作。如果獲得了權(quán)限,就執(zhí)行初始化;如果沒有獲得權(quán)限,就提示用戶。
@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_LONG).show();
finish();
}
break;
default:
break;
}
}
在一個(gè) onClick 方法中,統(tǒng)一處理 Play(播放)、Pause(暫停)、Replay(重新播放)的邏輯。
@Override
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;
}
}
執(zhí)行完畢,釋放所有資源。
@Override
protected void onDestroy() {
super.onDestroy();
if(videoView != null){
videoView.suspend();
}
}
完整代碼示例:
package com.cofox.myplayvideo;
import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.os.EnvironmentCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;
import java.io.File;
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.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
}
}
private void initVideoPath() {
File file = new File(Environment.getExternalStorageDirectory(), "movie2.mp4");
videoView.setVideoPath(file.getPath());//指定視頻文件路徑
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);//讓電影循環(huán)播放
}
});
}
@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_LONG).show();
finish();
}
break;
default:
break;
}
}
@Override
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
protected void onDestroy() {
super.onDestroy();
if(videoView != null){
videoView.suspend();
}
}
}
在 AndroidManifest.xml 中配置相應(yīng)的權(quán)限。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cofox.myplayvideo"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> ...
然后我們就可以看到運(yùn)行結(jié)果了。


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用VideoView播放本地視頻和網(wǎng)絡(luò)視頻的方法
- Android提高之MediaPlayer播放網(wǎng)絡(luò)視頻的實(shí)現(xiàn)方法
- Android使用VideoView出現(xiàn)無法播放此視頻問題的解決方法
- 詳解Android App中使用VideoView來實(shí)現(xiàn)視頻播放的方法
- Android播放assets文件里視頻文件相關(guān)問題分析
- Android如何讓W(xué)ebView中的HTML5頁面實(shí)現(xiàn)視頻全屏播放
- 一個(gè)html5播放視頻的video控件只支持android的默認(rèn)格式mp4和3gp
- Android自定義SeekBar實(shí)現(xiàn)視頻播放進(jìn)度條
- android webvie指定視頻播放器播放網(wǎng)站視頻
- Android實(shí)現(xiàn)音樂視頻播放
相關(guān)文章
詳解Android中Handler的實(shí)現(xiàn)原理
這篇文章主要為大家詳細(xì)介紹了Android中Handler的實(shí)現(xiàn)原理,本文深入分析 Android 的消息處理機(jī)制,了解 Handler 的工作原理,感興趣的小伙伴們可以參考一下2016-04-04
android studio 新手入門教程(二)項(xiàng)目的導(dǎo)入教程圖解
這篇文章主要介紹了android studio 新手入門教程(二)項(xiàng)目的導(dǎo)入教程圖解,需要的朋友可以參考下2017-12-12
VS Code開發(fā)React-Native及Flutter 開啟無線局域網(wǎng)安卓真機(jī)調(diào)試問題
這篇文章主要介紹了VS Code開發(fā)React-Native,F(xiàn)lutter 開啟無線局域網(wǎng)安卓真機(jī)調(diào)試,需要的朋友可以參考下2020-04-04
Android Studio OkHttpClient使用教程詳解
這篇文章主要介紹了Android Studio OkHttpClient使用教程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
基于flutter?sound插件實(shí)現(xiàn)錄音與播放功能
這篇文章主要介紹了基于flutter?sound插件實(shí)現(xiàn)錄音與播放功能,介紹了如何錄音,如何播放本地和遠(yuǎn)程音頻文件,以及如何實(shí)現(xiàn)動(dòng)畫,在錄制完音頻文件后如何上傳,這些都是我們平常使用這個(gè)功能會(huì)遇到的問題。在使用的過程中遇到的問題也有列出,需要的朋友可以參考下2022-05-05
Android編程簡單獲取網(wǎng)絡(luò)上的圖片
這篇文章主要介紹了Android編程簡單獲取網(wǎng)絡(luò)上的圖片,結(jié)合實(shí)例形式分析了Android獲取網(wǎng)絡(luò)圖片及加載顯示的相關(guān)操作步驟與注意事項(xiàng),需要的朋友可以參考下2016-10-10
Android Studio gradle 編譯提示‘default not found’ 解決辦法
這篇文章主要介紹了Android Studio gradle 編譯提示‘default not found’ 解決辦法的相關(guān)資料,需要的朋友可以參考下2016-12-12

