4

There is a C# application which uses LibVLC via NuGet packages.

These are the packages:

With these packages it is very easy to drop some mediaplayers into your WinForms application.

All you have to do is to initialize a player and give a new Media to it:

LibVLCSharp.Shared.LibVLC libVLC = new LibVLC();

LibVLCSharp.WinForms.VideoView videoView;
videoView.MediaPlayer = new LibVLCSharp.Shared.MediaPlayer(libVLC)

videoView.MediaPlayer.Play(new Media(libVLC, "URL", FromType.FromLocation));

Now I want to feed the mediaplayer with my custom data from a buffer. It can be byte-array, or anything similar. (data shall be considered to come from a valid mp4 file chunk-by-chunk).

How can I achieve that with libVLC in C#?

4 Answers 4

8

Use this Media constructor

new Media(libVLC, new StreamMediaInput(stream));

stream can by any .NET Stream.

This sample is with a torrent stream for example: https://github.com/mfkl/lvst/blob/master/LVST/Program.cs

Sign up to request clarification or add additional context in comments.

2 Comments

whats difference b/w StreamMediaInput & _mediaInput ?
StreamMediaInput is a concrete class provided by LVS. The MediaInput is an abstract class you can implement to define your own stream mechanism
4

If you don't want to create a Stream where not needed, you could also implement your own MediaInput class, and implement the required methods

https://code.videolan.org/videolan/LibVLCSharp/-/blob/master/src/LibVLCSharp/MediaInput.cs

Then, the usage is the same as @mfkl pointed out. Be careful though, the MediaInput must be disposed!

this._mediaInput = new MyMediaInput();

mediaPlayer.Play(new Media(libVLC, this._mediaInput));

// At the end
this._mediaInput.Dispose();

2 Comments

Wow, this could be pretty new, I've even had to update the nuget packages to be able to have the MediaInput class available. Thanks
Yes it is new, I added this because I wanted to add more scenarios than just a plain Stream. Enjoy! 🙂
0

Assuming that you can route your data to a stream, and expanding on @mfkl's answer, here is a complete console app that will open and play a media file.

Program.cs: `

// See https://aka.ms/new-console-template for more information
using LibVLCSharp.Shared;
using static System.Console;

Core.Initialize();
LibVLC _libVLC = new LibVLC();
MediaPlayer _mediaPlayer = new MediaPlayer(_libVLC);

using var stream = new FileStream(@"path-to-media-file", FileMode.Open, FileAccess.Read);
using var mediaInput = new StreamMediaInput(stream); // can be any readable data stream that VLC can play (e.g. avi, mp3
using (var media = new Media(_libVLC, mediaInput)) {
    _mediaPlayer.Media = media;
    _mediaPlayer.Play(); // returns as soon as media starts playing
}
ReadKey();  // keep app open so the video can continue to play after 'mediaPlayer.Play();' returns`

SimpleLibVLCSharp.csproj:`

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="LibVLCSharp" Version="3.9.0" />
    <PackageReference Include="VideoLAN.LibVLC.Windows" Version="3.0.20" />
  </ItemGroup>

</Project>

` To run, change "path-to-media-file" to a path to any media file that VLC player can play. Note that the FileStream and MediaInput should be disposed, but not before the clip is done playing. Media can be disposed after playing begins.

Finally, some MediaPlayer methods, notably '_mediaPlayer.Stop();' should NOT be called from the GUI thread. It may deadlock the player. Use: `

await Task.Run(() => { _mediaPlayer.Stop(); });

`

Comments

0

Assuming that you can route your data to a stream and expanding on @mfkl answer, here is a complete console app. Program.cs:`

// See https://aka.ms/new-console-template for more information
using LibVLCSharp.Shared;
using static System.Console;

Core.Initialize();
LibVLC _libVLC = new LibVLC();
MediaPlayer _mediaPlayer = new MediaPlayer(_libVLC);

using var stream = new FileStream(@"path-to-media-file", FileMode.Open, FileAccess.Read);
using var mediaInput = new StreamMediaInput(stream); // can be any readable data stream that VLC can play (e.g. avi, mp3
using (var media = new Media(_libVLC, mediaInput)) {
    _mediaPlayer.Media = media;
    _mediaPlayer.Play(); // returns as soon as media playing
}
ReadKey();  // keep app open so video can continue to play after 'mediaPlayer.Play();'

SimpleLibVLCSharp.csproj:

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="LibVLCSharp" Version="3.9.0" />
    <PackageReference Include="VideoLAN.LibVLC.Windows" Version="3.0.20" />
  </ItemGroup>

</Project>

` To run, change "path-to-media-file" to a media file path that VLC media player can play.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.