I am developing an app in C# WPF, and I want to load image to display. But the code cannot get the image to frame. When I run my application, there will be no audio. I do not add LoadedBehavior and UnloadedBehavior and delete all code in the button click function will work well. But I want to control the music to play and stop through button click. I want to dynamically control the music. I wonder what problem I have met.
This is my main C# code, and second one is XAML code.
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace test_WPF;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private MediaElement music;
public MainWindow()
{
InitializeComponent();
music = (MediaElement) FindName("MyMusic");
music.LoadedBehavior = MediaState.Manual;
music.UnloadedBehavior = MediaState.Stop;
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
if (music == null)
{
return;
}
try
{
if (music.HasAudio)
{
if (music.CanPause)
{
music.Pause();
}
else
{
music.Play();
}
}
else if (music.HasVideo)
{
MessageBox.Show("It's Video");
}
else
{
MessageBox.Show("No Audio");
}
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
}
}
}
<Window x:Class="test_WPF.MainWindow"
xmlns=";
xmlns:x=";
xmlns:d=";
xmlns:mc=";
xmlns:local="clr-namespace:test_WPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<MediaElement
Source="res/BGM.flac"
Name="MyMusic"></MediaElement>
<Button
Width="40"
Height="20"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Click="ButtonBase_OnClick"
Margin="0 100">Click</Button>
</Grid>
</Window>
I use .Net 8.0.112.
I am developing an app in C# WPF, and I want to load image to display. But the code cannot get the image to frame. When I run my application, there will be no audio. I do not add LoadedBehavior and UnloadedBehavior and delete all code in the button click function will work well. But I want to control the music to play and stop through button click. I want to dynamically control the music. I wonder what problem I have met.
This is my main C# code, and second one is XAML code.
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace test_WPF;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private MediaElement music;
public MainWindow()
{
InitializeComponent();
music = (MediaElement) FindName("MyMusic");
music.LoadedBehavior = MediaState.Manual;
music.UnloadedBehavior = MediaState.Stop;
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
if (music == null)
{
return;
}
try
{
if (music.HasAudio)
{
if (music.CanPause)
{
music.Pause();
}
else
{
music.Play();
}
}
else if (music.HasVideo)
{
MessageBox.Show("It's Video");
}
else
{
MessageBox.Show("No Audio");
}
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
}
}
}
<Window x:Class="test_WPF.MainWindow"
xmlns="http://schemas.microsoft/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats./markup-compatibility/2006"
xmlns:local="clr-namespace:test_WPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<MediaElement
Source="res/BGM.flac"
Name="MyMusic"></MediaElement>
<Button
Width="40"
Height="20"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Click="ButtonBase_OnClick"
Margin="0 100">Click</Button>
</Grid>
</Window>
I use .Net 8.0.112.
Share Improve this question edited Jan 18 at 19:22 Akira asked Jan 18 at 12:37 AkiraAkira 112 bronze badges 6- 1) Usually you would create the MediaElement control in XAML. As you do it now, the object is created but not added to the Window. 2) Is the Url correct- perhaps try with an absolute Url first. 3) I'm not sure if the MediaElement supports FLAC - perhaps try with mp3 first. – Klaus Gütter Commented Jan 18 at 14:28
- What also confuses me is that the question title is about video but then you ask about audio. – Klaus Gütter Commented Jan 18 at 14:29
- Thank you. When I try MediaElement in XAML is it OK. FLAC format is OK. – Akira Commented Jan 18 at 18:23
- I change my code to let XAML load audio. And I want to control the music dynamically. However, it still could not load audio. HasVideo still return false. – Akira Commented Jan 18 at 18:41
- Your last comment is still confusing, do you mean audio ("could not load audio") or video ("HasVideo still return false")? – Klaus Gütter Commented Jan 19 at 7:18
1 Answer
Reset to default 0Once the media is loaded the MediaElement.CanPause tells whether the MediaElement supports being paused, it does not change when the MediaElement.Play() or MediaElement.Pause() methods are called.
To get your code working you could introduce a bool field representing the state of the music media element:
private MediaElement music;
private bool isMusicPlaying = false;
Then use it to determine whether to play or pause and set the state accordingly:
if (isMusicPlaying)
{
music.Pause();
isMusicPlaying = false;
}
else
{
music.Play();
isMusicPlaying = true;
}
Using the actual state of the media element would be a more robust solution. The System.Windows.Controls MediaElement does not expose any public property telling its current state, but you can get it using reflection as shown in this answer:
How do I determine if MediaElement is playing?