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

分類:IT技術 時間:2017-09-26

一、概述

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

二、VideoView的使用方法

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

步驟:

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

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

下面是一個比較簡單的播放、暫停、重新播放的小demo

一、xml文件中的代碼

<"1.0" encoding="utf-8""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適用於一些遊戲的片頭動畫,或者某個應用的視頻宣傳。


Tags: quot android 播放 VideoView 視頻 layout_height

文章來源:


ads
ads

相關文章
ads

相關文章

ad