试图在xamarinForms项目中使用libvlcsharp.forms。需要播放资源视频文件。
已安装的libVLCsharp.Forms,VideoLan.LibVLC.Android,VideoLan.LibVLC.IOS
将mp4文件复制到iosProject / Resources;标记为BundleResource。将相同文件复制到androidProject / Resources / raw;标记为AndroidResource
Error>媒体文件错误://///BigBuckBunny.mp4
认为它不喜欢我的mp4文件格式或类似的东西。从网址下载了可以播放的文件。尝试了那个。结果相同。
帮助!!!
虽然它(android)可以通过url播放著名的BigBuckBuny> http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4,它不能播放下载并复制到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 dataBinding:
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");
}
}
}
我已经检查了存储在Resource的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;
}