Here's how it's done with LibVLCSharp library:
<StackPanel
Orientation="Vertical"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBox
x:Name="MediaURI"
Text="avares://Monsalma_AvaloniaAudioTest_Resource/Assets/sample.mp3" />
<Button
Content="Play"
Click="ClickHandler" />
<TextBlock
x:Name="PlaybackStatus"
Text="-" />
</StackPanel>
ClickHandler is the piece of code we're interested in.
We use AssetLoader to get the resource stream. Then we initiate the Media object with the stream. Lastly, we assign the media to media player and start playback.
private LibVLC MainLibVLC { get; set; }
private MediaPlayer MainMediaPlayer { get; set; }
private Stream MediaStream { get; set; }
public MainView()
{
InitializeComponent();
InitMediaPlayer();
}
private void InitMediaPlayer()
{
MainLibVLC = new(enableDebugLogs: true);
MainMediaPlayer = new(MainLibVLC);
MainMediaPlayer.TimeChanged += MediaPlayer_TimeChanged;
}
public void ClickHandler(object sender, RoutedEventArgs args)
{
MediaStream?.Dispose();
MediaStream = AssetLoader.Open(new Uri(MediaURI.Text));
using var media = new Media(MainLibVLC, new StreamMediaInput(MediaStream));
MainMediaPlayer.Media = media;
MainMediaPlayer.Play();
}
private void MediaPlayer_TimeChanged(object sender, MediaPlayerTimeChangedEventArgs e)
{
Dispatcher.UIThread.Invoke(
new Action(
() =>
{
PlaybackStatus.Text = $"{MainMediaPlayer.Time / 1000.0} / {MainMediaPlayer.Length / 1000.0}";
}
)
);
}
I tested this on Windows Desktop and Android (simulator). I've uploaded the complete code to my GitHub repository.