2018/10/31

[Android] Play Video File

要在 Android 裡面播放影片,可以透過 VideoView 這個物件來用。
這邊簡單寫了一個可以播放/暫停的範例程式。
先在手機資料夾準備一個影片檔 (/myDoc/video.mp4)

MainActivity.java:
  1. public class MainActivity extends AppCompatActivity{
  2.  
  3. private static String videoPath = "/myDoc/";
  4. private static String videoFile = "video.mp4";
  5.  
  6. private Button btnPlay;
  7. private VideoView vVideoView;
  8.  
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13.  
  14. btnPlay = (Button)findViewById(R.id.id_btn01);
  15. vVideoView = (VideoView)findViewById(R.id.id_vVideo);
  16.  
  17. vVideoView.setVideoURI(Uri.parse(Environment.getExternalStorageDirectory() + videoPath + videoFile));
  18.  
  19. // set loop
  20. vVideoView.setOnPreparedListener (new MediaPlayer.OnPreparedListener() {
  21. @Override
  22. public void onPrepared(MediaPlayer mp) {
  23. mp.setLooping(true);
  24. }
  25. });
  26. }
  27.  
  28. public void onClick(View v){
  29. if(vVideoView.isPlaying()){
  30. vVideoView.pause();
  31. btnPlay.setText("Play");
  32. }
  33. else {
  34. vVideoView.start();
  35. btnPlay.setText("Pause");
  36. }
  37. }
  38. }

activity_main.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.example.chris.myapplication.MainActivity">
  8.  
  9. <TextView
  10. android:id="@+id/textView"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_marginEnd="8dp"
  14. android:layout_marginStart="8dp"
  15. android:layout_marginTop="8dp"
  16. android:text="MainActivity"
  17. app:layout_constraintEnd_toEndOf="parent"
  18. app:layout_constraintStart_toStartOf="parent"
  19. app:layout_constraintTop_toTopOf="parent" />
  20.  
  21. <Button
  22. android:id="@+id/id_btn01"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_marginEnd="8dp"
  26. android:layout_marginStart="8dp"
  27. android:layout_marginTop="8dp"
  28. android:onClick="onClick"
  29. android:text="Play"
  30. app:layout_constraintEnd_toEndOf="parent"
  31. app:layout_constraintStart_toStartOf="parent"
  32. app:layout_constraintTop_toBottomOf="@+id/textView" />
  33.  
  34. <VideoView
  35. android:id="@+id/id_vVideo"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_marginEnd="8dp"
  39. android:layout_marginStart="8dp"
  40. android:layout_marginTop="8dp"
  41. app:layout_constraintEnd_toEndOf="parent"
  42. app:layout_constraintStart_toStartOf="parent"
  43. app:layout_constraintTop_toBottomOf="@+id/id_btn01" />
  44. </android.support.constraint.ConstraintLayout>
  45.  
最後記得加入讀取檔案的權限
AndroidManifest.xml:
  1. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

END

沒有留言:

張貼留言