使用 LibVLC 在 android 应用程序中播放 YouTube 视频

Playing a YouTube video in a android application using LibVLC

我只是想制作一个简单的 android 应用程序,它将播放我作为 link 提供的来自 YouTube 的视频。这是代码

package com.example.videoplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;

import org.videolan.libvlc.IVLCVout;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.MediaPlayer;

import java.util.ArrayList;
import java.util.logging.StreamHandler;

public class MainActivity extends AppCompatActivity {

private static String TAG = "MainActivity";
private VideoView vv;
private Button bt;

private Context mContext = this;

private LibVLC mLibVLC = null;
private MediaPlayer mMediaPlayer = null;

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

    vv = findViewById(R.id.videoView);
    bt = findViewById(R.id.button);

}

public void setUpMediaStream(View view) {
    final ArrayList<String> args = new ArrayList<>();
    args.add("-vvv");
    mLibVLC = new LibVLC(mContext, args);
    Log.d(TAG, "setUpMediaStream() creating media player");
    mMediaPlayer = new MediaPlayer(mLibVLC);
    Log.d(TAG, "setUpMediaStream() setting video view");
    String yt = "https://www.youtube.com/watch?v=tXHoqsnRD-U";
    Media media = new Media(mLibVLC, Uri.parse(yt));

    mMediaPlayer.setMedia(media);
    media.release();

    mMediaPlayer.setAspectRatio(null);
    mMediaPlayer.setScale(0);
    Log.d(TAG, "setUpMediaStream() playing media");
    mMediaPlayer.play();
}}

问题是当我按下播放按钮时遇到以下异常 java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip 文件“/data/app/com.example.videoplayer-JGI3RJow0fmaB6c-w2WuVQ==/base.apk”],nativeLibraryDirectories =[/data/app/com.example.videoplayer-JGI3RJow0fmaB6c-w2WuVQ==/lib/arm64, /system/lib64]]] 找不到“libc++_shared.so”

任何回复都是有帮助的。

这是我的 XML :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

<VideoView
    android:id="@+id/videoView"
    android:layout_width="370dp"
    android:layout_height="390dp"
    android:layout_marginTop="104dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.487"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="72dp"
    android:onClick="setUpMediaStream"
    android:text="@string/play"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"`enter code here`
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/videoView"
    app:layout_constraintVertical_bias="0.153" /> </androidx.constraintlayout.widget.ConstraintLayout>

我无法复制您的异常,但是...

我认为你不能只传入网页的 url,vlc 不是 WebView - 它需要是可播放的文件,例如 https://jell.yfish.us/media/jellyfish-3-mbps-hd-h264.mkv - 并且是务必在清单中添加互联网权限:

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

我还认为您需要将布局中的 VideoView 替换为 org.videolan.libvlc.util.VLCVideoLayout,并在实例化 MediaPlayer 后将其传递给 mediaPlayer.attachViews()

我可以通过这些步骤播放视频。

** 编辑 **

LibVLC 也有一个示例项目,在他们的应用级 build.gradle 文件中,他们指定了 jni 目录:

android {
...
    sourceSets {
        main {
            jni.srcDirs = [] // Prevent gradle from building native code with ndk; we have our own Makefile for it.
            jniLibs.srcDir 'jni/libs' // Where generated .so files are placed
            assets.srcDirs = ['src/main/assets', '../assets/']
        }
    }
}

https://code.videolan.org/videolan/libvlc-android-samples/-/tree/master/java_sample