MPV Android 视频播放器

问题描述 投票:0回答:1

有人知道如何制作MPV播放器吗 MPV播放器 我阅读了文档,但我尽力了,但无法构建本机代码

我目前使用的是最新的android studio 有人可以帮助我构建这个项目,我在本机代码中遇到问题,无法构建它 我会非常感激的

java android kotlin native mpv
1个回答
0
投票

MainActivity.java

package com.example.mpvplayer;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button playVideoButton;

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

        playVideoButton = findViewById(R.id.play_video_button);
        playVideoButton.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, VideoPlayer.class);
            intent.putExtra("VIDEO_URL", "http://path_to_your_video.mp4"); // Replace with your video URL
            startActivity(intent);
        });
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/play_video_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play Video"
        android:layout_centerInParent="true" />
</RelativeLayout>

VideoPlayer.java

package com.example.mpvplayer;

import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import org.mpv.core.MPV;

public class VideoPlayer extends AppCompatActivity {
    private SurfaceView surfaceView;
    private Button playButton;
    private MPV mpv;
    private boolean isPlaying = false;

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

        surfaceView = findViewById(R.id.surface_view);
        playButton = findViewById(R.id.play_button);
        mpv = new MPV();

        String videoUrl = getIntent().getStringExtra("VIDEO_URL");

        SurfaceHolder holder = surfaceView.getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mpv.setSurface(holder.getSurface());
                mpv.setOption("video-path", videoUrl);
                mpv.load();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                mpv.stop();
                mpv.setSurface(null);
            }
        });

        playButton.setOnClickListener(v -> {
            if (isPlaying) {
                mpv.pause();
                playButton.setText("Play");
            } else {
                mpv.play();
                playButton.setText("Pause");
            }
            isPlaying = !isPlaying;
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mpv.stop();
    }
}

player_controls.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <SurfaceView
        android:id="@+id/surface_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/play_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mpvplayer">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <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/Theme.AppCompat.Light.NoActionBar">
        
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".VideoPlayer" />
    </application>
</manifest>

构建.gradle

apply plugin: 'com.android.application'

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.mpvplayer"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'org.mpv:mpv:1.0.0' // Add the correct MPV dependency
}

注意:此代码为完整的基于 MPV 的 Android 视频播放器应用程序建立了基本结构。将“http://path_to_your_video.mp4”替换为您的实际视频网址。

© www.soinside.com 2019 - 2024. All rights reserved.