12.25

package com.example.myapp;

 

import android.net.Uri;

import android.os.Bundle;

import android.widget.MediaController;

import android.widget.VideoView;

 

import androidx.appcompat.app.AppCompatActivity;

 

public class MainActivity extends AppCompatActivity {

 

    private VideoView videoView;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        videoView = findViewById(R.id.videoView);

 

        // 设置视频文件的路径或URI

        String videoPath = "android.resource://" + getPackageName() + "/" + R.raw.sample_video;

        Uri uri = Uri.parse(videoPath);

        videoView.setVideoURI(uri);

 

        // 创建 MediaController 对象,用于控制视频播放

        MediaController mediaController = new MediaController(this);

        mediaController.setAnchorView(videoView);

        videoView.setMediaController(mediaController);

 

        // 开始播放视频

        videoView.start();

    }

}

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <VideoView

        android:id="@+id/videoView"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

 

</RelativeLayout>

 

posted @ 2024-12-25 19:35  赵千万  阅读(1)  评论(0编辑  收藏  举报