2018/10/31

[Android] Play Video File

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

MainActivity.java:
public class MainActivity extends AppCompatActivity{

    private static String videoPath = "/myDoc/";
    private static String videoFile = "video.mp4";

    private Button btnPlay;
    private VideoView vVideoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnPlay = (Button)findViewById(R.id.id_btn01);
        vVideoView = (VideoView)findViewById(R.id.id_vVideo);

        vVideoView.setVideoURI(Uri.parse(Environment.getExternalStorageDirectory() + videoPath + videoFile));

        // set loop
        vVideoView.setOnPreparedListener (new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setLooping(true);
            }
        });
    }

    public void onClick(View v){
        if(vVideoView.isPlaying()){
            vVideoView.pause();
            btnPlay.setText("Play");
        }
        else {
            vVideoView.start();
            btnPlay.setText("Pause");
        }
    }
}

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.chris.myapplication.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="MainActivity"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/id_btn01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:onClick="onClick"
        android:text="Play"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <VideoView
        android:id="@+id/id_vVideo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/id_btn01" />
    
</android.support.constraint.ConstraintLayout>

最後記得加入讀取檔案的權限
AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

END

沒有留言:

張貼留言