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

首頁 > 系統 > Android > 正文

android多媒體類VideoView使用方法詳解

2019-10-22 18:27:29
字體:
來源:轉載
供稿:網友

一、概述

VideoView類將視頻的顯示和控制集于一身,我們可以借助它完成一個簡易的視頻播放器。VideoView和MediaPlayer也比較相似。

二、VideoView的使用方法

它主要有以下幾種常用方法

android,多媒體類,VideoView

步驟:

1.指定視頻文件的路徑,
2.接下來調用start()方法就可以開始播放視頻,pause()方法就會暫停播放,resume()方法就會重新播放

注:獲取視頻文件也需要運行時權限,所有相關邏輯也需要寫。
      最后不要忘記在AndroidManifest.xml文件中聲明用到的權限

下面是一個比較簡單的播放、暫停、重新播放的小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, "拒絕權限將無法使用程序", 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();//開始播放         }         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所占用的資源釋放掉     }   } } 

三、聲明權限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

VideoView并不是一個萬能的視頻播放工具類,如果使用它,編寫一個功能十分強大的視頻播放器是不太現實的。VideoView適用于一些游戲的片頭動畫,或者某個應用的視頻宣傳。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 成安县| 扬中市| 金堂县| 乌鲁木齐县| 南木林县| 福海县| 阿合奇县| 兴化市| 黔西| 通化市| 秭归县| 乌恰县| 延寿县| 苏州市| 彩票| 湄潭县| 民县| 根河市| 卢氏县| 彭泽县| 阳曲县| 临夏市| 扬州市| 玛沁县| 涿州市| 蒙阴县| 富平县| 海门市| 淮阳县| 木里| 疏附县| 西贡区| 当雄县| 龙胜| 晋城| 九龙县| 黑山县| 徐州市| 海晏县| 津南区| 枣强县|