android視頻播放簡單實現(xiàn)示例(VideoView&MediaPlayer)
如果你看過我的《android音樂播放簡單實現(xiàn)(MediaPlayer)》,那么本篇將會毫無壓力。
首先是主界面的三個按鈕和一個播放控件
<?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 作為視頻播放時的顯示位置。
private VideoView videoView;
在 onCreate 里,對界面的按鈕和顯示位置實例化,并檢查權(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 }
用一個單獨的方法 initVideoPath() 來實現(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é)果進行判斷,并針對性操作。如果獲得了權(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; } }
在一個 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 中配置相應的權(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"/> ...
然后我們就可以看到運行結(jié)果了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android使用VideoView播放本地視頻和網(wǎng)絡視頻的方法
- Android提高之MediaPlayer播放網(wǎng)絡視頻的實現(xiàn)方法
- Android使用VideoView出現(xiàn)無法播放此視頻問題的解決方法
- 詳解Android App中使用VideoView來實現(xiàn)視頻播放的方法
- Android播放assets文件里視頻文件相關(guān)問題分析
- Android如何讓WebView中的HTML5頁面實現(xiàn)視頻全屏播放
- 一個html5播放視頻的video控件只支持android的默認格式mp4和3gp
- Android自定義SeekBar實現(xiàn)視頻播放進度條
- android webvie指定視頻播放器播放網(wǎng)站視頻
- Android實現(xiàn)音樂視頻播放
相關(guān)文章
Android編程中聊天頁面背景圖片、標題欄由于鍵盤引起問題的解決方法
這篇文章主要介紹了Android編程中聊天頁面背景圖片、標題欄由于鍵盤引起問題的解決方法,針對鍵盤彈出時標題欄及背景圖片異常的相關(guān)解決方法,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10Android編程處理窗口控件大小,形狀,像素等UI元素工具類
這篇文章主要介紹了Android編程處理窗口控件大小,形狀,像素等UI元素工具類,可實現(xiàn)像素與dp的轉(zhuǎn)換、窗口寬度設置、彈出窗口中l(wèi)istview高度設置等功能,需要的朋友可以參考下2017-12-12Android 添加系統(tǒng)設置屬性的實現(xiàn)及步驟
這篇文章主要介紹了Android 添加系統(tǒng)設置屬性的實現(xiàn)及步驟的相關(guān)資料,需要的朋友可以參考下2017-07-07Android中獲取網(wǎng)頁表單中的數(shù)據(jù)實現(xiàn)思路及代碼
在Android中獲取網(wǎng)頁里表單中的數(shù)據(jù)具體實現(xiàn)代碼如下,感興趣的各位可以參考過下哈,希望對大家有所幫助2013-06-06利用kotlin實現(xiàn)統(tǒng)計文件字符個數(shù)的方法示例
最近在學習kotlin,發(fā)現(xiàn)了一些不錯的小技巧,所以下面這篇文章主要給大家介紹了關(guān)于利用kotlin實現(xiàn)統(tǒng)計文件字符個數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。2017-12-12