1 year ago

#213225

test-img

aschaefer

Can a StreamMediaInput only be played one time to the end?

I am using LibVLCSharp to play audio, and am running into an issue in a UWP app. I am playing audio via a MemoryStream loaded into the StreamMediaInput to be used as a Media for the player. When I do this, if I let the player reach the end of the audio, the audio becomes unusable. I can't play it a second time, can't seek to any other position, can't do anything with it. When I do, it locks the thread and completely ganks the app.

This is a simplified version of my code to play audio, but contains all of the important stuff:

LibVLC _libVLC = new LibVLC();
MediaPlayer _player;

public void Load(byte[] data)
{
    _player = new MediaPlayer(_libVLC);
   
    var stream = new MemoryStream(data);

    var mediaInput = new StreamMediaInput(stream);

    var media = new Media(_libVlc, mediaInput);
    player.Media = media;
    
}

public void Play()
{
   if (_player?.Media == null)
          return;
      
   _player.Play();
}

Am I missing something fundamental about the way the MediaPlayer should behave when audio ends?

UPDATE: I implemented this change to my code and it now plays just fine. It appears that when a StreamMediaInput reaches a state of "Ended", it doesn't have the ability to continue using that input. So, the solution is to reset it all manually if I need to. Here's what I changed:

public bool Load(Stream stream)
{
    _player = new MediaPlayer(_libVLC);
    _currentStream = stream;
    SetupMedia();    
    return _player?.Media != null;
}

public bool Play()
{
    if (_player?.Media == null)
        return false;

    if (_player.Media.State == VLCState.Ended)
    {
        SetupMedia();
    }

    return _player.Play();
}

private void SetupMedia()
{
    if (_currentStream.Position == _currentStream.Length && _currentStream.CanSeek)
    {
        _currentStream.Seek(0, SeekOrigin.Begin);
    }
    var mediaInput = new StreamMediaInput(_currentStream);
    var media = new Media(_libVlc, mediaInput);
    if (_player == null) return;
    var oldMedia = Player.Media;
    if (oldMedia != null)
    {
        oldMedia.DurationChanged -= MediaOnDurationChanged;
        oldMedia.Dispose();
    }
    _player.Media = media;
    Duration = media.Duration;
    media.DurationChanged += MediaOnDurationChanged;   
}

private void MediaOnDurationChanged(object sender, MediaDurationChangedEventArgs args)
{
     Duration = args.Duration / 1000.0;
}

In addition to this change to fix the issue of not being able to play an audio source twice all the way through, I realized I did, in fact, omit some code from the original post that was causing the app to hang: In a callback from an event on the player, I was calling methods on the player itself (such as .SeekTo(0)). In researching this I found in the documentation that you should definitely not do that, and removing that code fixed the freezing issue.

.net

uwp

libvlc

libvlcsharp

0 Answers

Your Answer

Accepted video resources