1

尝试在 xamarinForms 项目中使用 libvlcsharp.forms。
需要播放资源视频文件。

安装了 libVLCsharp.Forms、VideoLan.LibVLC.Android、VideoLan.LibVLC.IOS

将 mp4 文件复制到 iosProject/Resources;标记为 BundleResource。
将相同的文件复制到 androidProject/Resources/raw;标记为 AndroidResource

  • iOS可以从互联网播放
  • Anroid可以播放和iOS一样的在线地址
  • ios可以播放资源文件
  • android不能播放相同的资源文件

错误> 媒体文件错误:////BigBuckBunny.mp4

以为它不喜欢我的 mp4 文件格式或类似的东西。
下载了它可以从 url 播放的文件。
而是尝试了那个。
相同的结果。

帮助 !!!

虽然它(android)可以从 url > http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4播放著名的 BigBuckBuny ,但它无法播放下载并复制到 AndroidProject/Resource/raw 的相同视频

XAML 部分:

<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ParkinsonMobileApp.Pages.PhysicalActivitiesVideoDetail"
Padding="0"
Title="HomePage"
FlowDirection="LeftToRight"
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
ios:Page.UseSafeArea="False"
NavigationPage.HasNavigationBar="False"
xmlns:customViews="clr-namespace:ParkinsonMobileApp.Views"
BackgroundColor="White"
xmlns:vlc="clr-namespace:LibVLCSharp.Forms.Shared;assembly=LibVLCSharp.Forms"
Appearing="ContentPage_OnAppearing"
Disappearing="ContentPage_Disappearing">
<AbsoluteLayout
    Padding="0"
    Margin="0"
    HorizontalOptions="Fill"
    VerticalOptions="Fill">
    <Image
        x:Name="imgViewBackground"
        Source="viewBackgroundBlurred.png"
        Aspect="AspectFill"
        AbsoluteLayout.LayoutFlags="All"
        AbsoluteLayout.LayoutBounds="0,0,1,1" />
    <vlc:MediaPlayerElement
        x:Name="vlcPlayer"
        MediaPlayer="{Binding MediaPlayer}"
        LibVLC="{Binding LibVLC}"
        EnableRendererDiscovery="True"
        Padding="0"
        Margin="0"
        AbsoluteLayout.LayoutFlags="All"
        AbsoluteLayout.LayoutBounds="0,0,1,1" />
    <customViews:CustomNavigationBar
        HorizontalOptions="Fill"
        VerticalOptions="Start"
        Padding="0"
        Margin="0"
        AbsoluteLayout.LayoutFlags="All"
        AbsoluteLayout.LayoutBounds="0,0,1,1"
        Theme="White"
        ImgBackgroundIsVisible="False"
        ButBackDisplayed="True"
        ButsOnRightDisplayed="False"
        TitleText="Egzersiz 001"
        TitleIsDisplayed="True"
        BackgroundColor="Transparent"
        backClicked="CustomNavigationBar_backClicked" />
</AbsoluteLayout>

CodeBehind 数据绑定:

void ContentPage_OnAppearing(object sender, System.EventArgs e)
    {
        base.OnAppearing();
        //pageViewModel = new VieoDetailViewModel("file://android_asset/024_1.mp4");
        //pageViewModel = new VieoDetailViewModel("024_1.mp4");
        pageViewModel = new VieoDetailViewModel("BigBuckBunny.mp4");
        BindingContext = pageViewModel;
        //MessagingCenter.Send(this, "AllowLandscape");
    }

数据绑定模型:

public class VieoDetailViewModel : ViewModelBase
{
    /// <summary>
    /// Initialize LibVLC and playback when page appears
    /// </summary>
    public VieoDetailViewModel(String filePath)
    {
        Core.Initialize();

        LibVLC = new LibVLC();

        var media = new Media(LibVLC,
            //"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
            filePath,
            //FromType.FromLocation
            FromType.FromPath
            );

        MediaPlayer = new MediaPlayer(media) { EnableHardwareDecoding = true };
        //MediaPlayer = new MediaPlayer(media);
        MediaPlayer.Play(media);
    }

    /// <summary>
    /// Gets the <see cref="LibVLCSharp.Shared.LibVLC"/> instance.
    /// </summary>
    public LibVLC _LibVLC;
    public LibVLC LibVLC
    {
        get
        {
            return _LibVLC;
        }
        set
        {
            _LibVLC = value;
            OnPropertyChanged("LibVLC");
        }
    }

    /// <summary>
    /// Gets the <see cref="LibVLCSharp.Shared.MediaPlayer"/> instance.
    /// </summary>
    public MediaPlayer _MediaPlayer;
    public MediaPlayer MediaPlayer
    {
        get
        {
            return _MediaPlayer;
        }
        set
        {
            _MediaPlayer = value;
            OnPropertyChanged("MediaPlayer");
        }
    }
}
4

2 回答 2

0

我检查了存储在资源的 Assets 文件夹中的 MP4 的路径。它总是抛出如下异常。

VLC is unable to open the MRL

您可以将 mp4 文件放在 Assets 文件夹中,然后复制到设备上播放。

LibVLCSharp.Platforms.Android.VideoView _videoView;
    LibVLC _libVLC;
    MediaPlayer _mediaPlayer;
    private const string fileName = "BigBuckBunny.mp4";
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.layout1);
    }

    protected async override void OnResume()
    {
        base.OnResume();

        Core.Initialize();

        _libVLC = new LibVLC();
        _mediaPlayer = new MediaPlayer(_libVLC)
        {
            EnableHardwareDecoding = true
        };

        //_videoView = new LibVLCSharp.Platforms.Android.VideoView(this) { MediaPlayer = _mediaPlayer };

        _videoView = FindViewById<LibVLCSharp.Platforms.Android.VideoView>(Resource.Id.videoPlayer);
        _videoView.MediaPlayer = _mediaPlayer;

        // Android application default folder.
        var dbFile = GetDefaultFolderPath();

        // Check if the file already exists.
        if (!File.Exists(dbFile))
        {
            using (FileStream writeStream = new FileStream(dbFile, FileMode.OpenOrCreate, FileAccess.Write))
            {
                // Assets is comming from the current context.
                await Assets.Open(fileName).CopyToAsync(writeStream);
            }
        }


        //AddContentView(_videoView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent));
        var media = new Media(_libVLC, dbFile, FromType.FromPath);//"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
        if (File.Exists(dbFile))
        {
            _videoView.MediaPlayer.Play(media);
        }


    }
    private string GetDefaultFolderPath()
    {
        var appFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var dbFile = Path.Combine(appFolder, fileName);

        return dbFile;
    }

在此处输入图像描述

于 2020-03-06T07:40:32.793 回答
0

Media 构造函数有一个采用 StreamMediaInput 的重载,因此您可以将视频文件作为嵌入式资源添加到主项目,将其读入 Stream 并从中创建 StreamMediaInput。如果你这样做,它也适用于 iOS,所以你只需要主项目中的视频文件,从 Android 和 iOS 中删除它们。

using System.IO;
using System.Reflection;
...

Assembly assembly = Assembly.GetExecutingAssembly();
string assemblyName = assembly.GetName().Name;
Stream stream = assembly.GetManifestResourceStream(assemblyName + ".BigBuckBunny.mp4");
StreamMediaInput mediaStream = new StreamMediaInput(stream);
Media media = new Media(_libVLC, mediaStream, new string[] { });
MediaPlayer player = new MediaPlayer(media) { EnableHardwareDecoding = true };
player.Play();

这假设 mp4 文件位于主项目的根目录中。另请注意,我没有将 Stream 包装在 using() 中,因为您需要坚持使用它,直到您完成视频并随后处理它,因此您需要对其进行排序 - 您可以创建一个类来处理那个。

于 2020-06-27T23:31:20.520 回答