最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年09月20日 09:40:52   作者:liu_keke  
這篇文章主要為大家詳細(xì)介紹了android多媒體類VideoView使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一、概述

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

二、VideoView的使用方法

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

步驟:

1.指定視頻文件的路徑,
2.接下來(lái)調(diào)用start()方法就可以開(kāi)始播放視頻,pause()方法就會(huì)暫停播放,resume()方法就會(huì)重新播放

注:獲取視頻文件也需要運(yùn)行時(shí)權(quán)限,所有相關(guān)邏輯也需要寫(xiě)。
      最后不要忘記在AndroidManifest.xml文件中聲明用到的權(quán)限

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

三、聲明權(quán)限

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

VideoView并不是一個(gè)萬(wàn)能的視頻播放工具類,如果使用它,編寫(xiě)一個(gè)功能十分強(qiáng)大的視頻播放器是不太現(xiàn)實(shí)的。VideoView適用于一些游戲的片頭動(dòng)畫(huà),或者某個(gè)應(yīng)用的視頻宣傳。

相關(guān)文章

最新評(píng)論

曲水县| 梅河口市| 江门市| 兴文县| 永昌县| 江安县| 临朐县| 沛县| 微博| 青神县| 阿巴嘎旗| 泾源县| 孟津县| 石嘴山市| 嫩江县| 乌苏市| 芮城县| 舞阳县| 伊川县| 大连市| 丰原市| 阜宁县| 晋城| 攀枝花市| 巴南区| 滕州市| 肥乡县| 青州市| 玛曲县| 博爱县| 桑日县| 沽源县| 石台县| 西充县| 安康市| 满洲里市| 濮阳市| 常德市| 乐昌市| 宜君县| 容城县|