0

When I create LibVLC and MediaPlayer objects in the constructor and play the video, there is only sound but no image. When I create LibVLC and MediaPlayer objects in the'Play' function, there are sounds and images. I don't want to create a MediaPlayer object every time the play function is called. what should I do?

using System;
using System.ComponentModel;
using System.Windows.Input;
using LibVLCSharp.Shared;
using static Xamarin.Essentials.Permissions;


namespace MediaElement
{
    /// <summary>
    /// Represents the main viewmodel.
    /// </summary>
    public class MainViewModel : INotifyPropertyChanged
    {
        /// <summary>
        /// Property changed event
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;


        /// <summary>
        /// Initializes a new instance of <see cref="MainViewModel"/> class.
        /// </summary>
        public MainViewModel()
        {
            Core.Initialize();
            LibVLC = new LibVLC();
            MediaPlayer = new MediaPlayer(LibVLC) { EnableHardwareDecoding = true };
        }

        
        

        private LibVLC _libVLC;
        /// <summary>
        /// Gets the <see cref="LibVLCSharp.Shared.LibVLC"/> instance.
        /// </summary>
        public LibVLC LibVLC
        {
            get => _libVLC;
            private set => Set(nameof(LibVLC), ref _libVLC, value);
        }

        private MediaPlayer _mediaPlayer;
        /// <summary>
        /// Gets the <see cref="LibVLCSharp.Shared.MediaPlayer"/> instance.
        /// </summary>
        public MediaPlayer MediaPlayer
        {
            get => _mediaPlayer;
            private set => Set(nameof(MediaPlayer), ref _mediaPlayer, value);
        }

               

        /// <summary>
        /// Initialize LibVLC and playback when page appears
        /// </summary>
        public void Play(String path)
        {
                MediaPlayer.Play(new LibVLCSharp.Shared.Media(LibVLC, new Uri(path)));             
        }

        

        private void Set<T>(string propertyName, ref T field, T value)
        {
            if (field == null && value != null || field != null && !field.Equals(value))
            {
                field = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }
}
Bill Qu
  • 27
  • 2
  • Can you help me see how to solve this? https://stackoverflow.com/questions/69210874/libvlcsharp-will-get-stuck-playing-specific-videos-on-any-platform – bbhxwl Sep 16 '21 at 15:57

1 Answers1

0

You only sent half of your code, we don't know how you are using your VideoView control.

As a general rule of thumb:

  • How do you attach the MediaPlayer to your view?
  • Do you properly wait for the view to be visible before calling Play?
  • Have a look at the samples here and compare to see what you did wrong.
cube45
  • 3,429
  • 2
  • 24
  • 35
  • this should be a comment, not an answer – Jason Aug 18 '21 at 09:32
  • I have share all the project. please get it at [http://steelsoft.site/quPlayer.zip]. Help me solve the problem. Thank you very much. – Bill Qu Aug 18 '21 at 12:21
  • the project is at http://steelsoft.site/quPlayer.zip – Bill Qu Aug 18 '21 at 12:22
  • Sorry, I don't click links to zip files hosted on domains I don't trust. Moreover, on StackOverflow, you have to include every details in the question to avoid link rot. – cube45 Aug 18 '21 at 13:07
  • in the page, I codes below: MasterDetailPage1Detail.xaml: – Bill Qu Aug 22 '21 at 03:33
  • using LibVLCSharp.Shared; using MediaElement; namespace quPlayer { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class MasterDetailPage1Detail : ContentPage { MainViewModel mainViewModel; public MasterDetailPage1Detail() { InitializeComponent(); mainViewModel = new MainViewModel(); this.BindingContext = mainViewModel; } } } – Bill Qu Aug 22 '21 at 03:35
  • Please edit your question instead and paste your code as code blocks. Why did you enable renderer discovery? – cube45 Aug 22 '21 at 08:01