国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android視頻播放簡單實現示例(VideoView&MediaPlayer)

2019-12-12 02:11:11
字體:
來源:轉載
供稿:網友

如果你看過我的《android音樂播放簡單實現(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 里,對界面的按鈕和顯示位置實例化,并檢查權限。

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() 來實現視頻播放初始化

 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);//讓電影循環播放   }  }); }

onRequestPermissionsResult 中對權限的取得結果進行判斷,并針對性操作。如果獲得了權限,就執行初始化;如果沒有獲得權限,就提示用戶。

 @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, "拒絕權限,無法使用程序。", Toast.LENGTH_LONG).show();     finish();    }    break;   default:    break;  } }

在一個 onClick 方法中,統一處理 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;  } }

執行完畢,釋放所有資源。

 @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);//讓電影循環播放   }  }); } @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, "拒絕權限,無法使用程序。", 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 中配置相應的權限。

<?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"/>...

然后我們就可以看到運行結果了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宿松县| 崇左市| 锡林郭勒盟| 曲松县| 慈利县| 湟中县| 遵化市| 博湖县| 宜阳县| 汉阴县| 丹阳市| 溆浦县| 陆良县| 凤庆县| 仁怀市| 双流县| 日土县| 丹东市| 墨江| 陆良县| 永安市| 贡山| 石狮市| 河南省| 白城市| 鄂尔多斯市| 若尔盖县| 渭源县| 城步| 武鸣县| 蒙阴县| 吉安县| 西安市| 泊头市| 古交市| 房产| 友谊县| 金溪县| 台前县| 辽宁省| 柳林县|