Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Phát video từ thư mục assets hoặc raw trong Android

Giới thiệu

Ví dụ này hướng dẫn cách phát video từ thư mục raw (trong res) hoặc assets bằng VideoView và MediaController trên Android.

Bước 1: Tạo dự án mới

Tạo project trong Android Studio: File → New Project.

Bước 2: Layout - VideoView (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="8dp"
    tools:context=".MainActivity">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </VideoView>

</LinearLayout>

Bước 3: Thêm video vào thư mục raw

Chuột phải vào res → New → Android Resource Directory, chọn loại raw. Copy file video (ví dụ: videoplayback.mp4) vào thư mục này.

Bước 4: MainActivity - Phát video từ raw

Sử dụng URI dạng android.resource://package_name/raw/ten_file (không cần phần mở rộng).

import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        VideoView videoView = findViewById(R.id.videoView);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);

        // Phát từ thư mục raw
        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.videoplayback));
        videoView.start();
    }
}

Lưu ý: Phát từ thư mục assets

Nếu video nằm trong assets, cần copy ra cache/file tạm vì VideoView không truy cập trực tiếp assets:

// Ví dụ phát từ assets (cần copy file trước)
String fileName = "video.mp4";
File cacheFile = new File(getCacheDir(), fileName);
try (InputStream is = getAssets().open(fileName);
     FileOutputStream fos = new FileOutputStream(cacheFile)) {
    byte[] buffer = new byte[1024];
    int len;
    while ((len = is.read(buffer)) > 0) fos.write(buffer, 0, len);
}
videoView.setVideoURI(Uri.fromFile(cacheFile));
videoView.start();

Bước 5: Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Chạy ứng dụng

Chạy trên thiết bị thật, video sẽ tự động phát với bộ điều khiển MediaController (tua, tạm dừng, âm lượng).