Android VideoView簡單播放視頻


Android VideoView一個文件目錄,就可以直接播放智能設備中的視頻文件,現在以播放事先用手機拍好並重命名的視頻文件test.mp4爲例。

1需要在佈局文件中寫一個ViedoView

<RelativeLayoutxmlns: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"
   tools:context="com.example.videoview.MainActivity" >
 
   <VideoView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/videoView" />
 
</RelativeLayout>

 

2)不要忘記在AndroidManifest.xml文件中添加讀寫外部存儲的權限:

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

 

3)在java代碼中給VideoView設置文件目錄,然後start播放:

public class MainActivity extends Activity{
 
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
 
       VideoView videoView = (VideoView) findViewById(R.id.videoView);
 
       // 獲得的path等於:/storage/emulated/0/DCIM
       File path = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
 
       // 拼接完整路徑
       File f = new File(path, "/Camera/test.mp4");
 
       // 此時的f.getAbsolutePath()=/storage/emulated/0/DCIM//\Camera/test.mp4
       videoView.setVideoPath(f.getAbsolutePath());
 
       // 開始播放視頻
       videoView.start();
 
       // VideiView獲焦點
       // videoView.requestFocus();
    }
}

 

以上!另外對APP進行全方位的檢測,我都會用這個:www.ineice.com


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章