The task should be simple. I have an Image with a "Tapped" even handler, and i want to play a wav file when it is clicked.
<Image x:Name="Snd_abort_1" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" Source="Assets/Tiles/A/abort_1.png" Tapped="Snd_abort_1_Tapped"></Image>
private void Snd_abort_1_Tapped(object sender, TappedRoutedEventArgs e)
{
MediaElement myMediaElement = new MediaElement();
myMediaElement.AutoPlay = false;
myMediaElement.Source = new Uri("ms-appx:///Assets/Sounds/DukeNukem/abort.wav");
myMediaElement.Play();
}
For some particular reason this does not work.
I tried to debug the click and that fires but no error, no sound.
I have tried both in emulator and on my Lumia 930 device.
What am i missing out?
You don't need to prefix your files with ms-appx:, just do
myMediaElement.Source = new Uri("Assets/Sounds/DukeNukem/abort.wav", UriKind.Relative);
myMediaElement.Play();
Unfortunately, though it is correct. This code-behind approach will still not work. Because the myMediaElement is not part of the VisualTree (because you made it dynamically).
To fix this, just add it to the root Grid/Panel
myMediaElement.Source = new Uri("Assets/Sounds/DukeNukem/abort.wav", UriKind.Relative);
this.ContentPanel.Children.Add(myMediaElement);
myMediaElement.Play();
Now it should play nicely with everything.
Related
In this particular application I am showing Video using Web Cam.When I press any button it runs Audio file using SoundPlayer.
Code to Run Audio
System.Media.SoundPlayer player = new System.Media.SoundPlayer(#"Audio\audio1.wav");
player.PlayLooping();
This code snippet freezing the UI even if I put this code in thread
still my UI is freezing
So can anyone please tell me how I can solve this problem. Thank you
Edit1:
So in above code snippet
I am declaring Sound Player object Globally
System.Media.SoundPlayer player = new System.Media.SoundPlayer(#"Audio\audio1.wav");
and calling function PlayAudio() on Button click Event
private void PlayAudio()
{
player.PlayLooping();
}
It should NOT freeze the UI, unless there is some other blocking code running. Below is a sample 'Hello World' WPF code with your code sample -
XAML -
<StackPanel>
<Button Content="Play" Height="50" Width="100" Click="Button_Click"/>
<Button Content="Test" Height="50" Width="100" Click="Button_Click_1"/>
<TextBlock x:Name="txtSample" Text="Hello"/>
</StackPanel>
xaml.cs -
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(#"Audio\audio1.wav");
player.PlayLooping();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
txtSample.Text = Guid.NewGuid().ToString();
}
Now click on the Play button to play the sound, now while the sound is playing click on Test button any number of times it should not freeze.
Also as per Microsoft documentation PlayLooping should not hang UI as it runs in a new thread by default.
I have been searching for a while and honestly I haven't solved this really simple issue. I have a flyout menu and I have attached an event to a flyout menu item. I want to change programmatically the background image of my XAML page when I select the item.
<Grid x:Name="main">
<Grid.Background>
<ImageBrush Stretch="Fill" ImageSource="Assets/bg_1.jpg"/>
</Grid.Background>
</Grid>
This is the default image I got as a background (the grid is basically the whole page).
The C# event code is here:
private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
{
main.Background = new ImageBrush { ImageSource = new BitmapImage(new Uri("ms-appx:///MTG Life Counter/Assets/bg_2.jpg")) , Stretch = Stretch.None};
}
When I select the menu item instead of setting the image as background, it makes it white.
Default background
I tested and it is not an issue of the image. I know that I am doing something like an obvious mistake but I am a bit new to UWP and I couldn't find a solution.
Here is the blank background after I select to change it
Thanks for you time and sorry for the ignorance.
there's a property called "BaseUri"... try adding it to your code.
make these changes:
private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
{
main.Background = new ImageBrush { ImageSource = new BitmapImage(new Uri(this.BaseUri, "Assets/bg_2.jpg")), Stretch = Stretch.None };
}
I am trying to play a video in my app. The video and the xaml file are present under the same folder.
My xaml code (which is a user control)
<Grid x:Name="LayoutRoot">
<MediaElement x:Name="vid" MediaOpened="MediaElement_MediaOpened"
Source="hey.mp4" AutoPlay="True" />
</Grid>
My xaml.cs file code
public Page1()
{
InitializeComponent();
}
private void MediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
vid.Play();
}
The video is not playing. I tried the source to be "/hey.mp4" also, but it didn't play.
What is my mistake ?
Check your URl of the video and to disable locking of the phone, you could do this,
PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
The audio is playing well when giving the local audio file as the source for the MediaElement.
<MediaElement Grid.Row="1" Stretch="Uniform" Name="Player" Margin="0,93,0,0" Source="Assets/test.mp3" />
But it is not working when try to play the remote audio file.
In the MainPage.xaml.cs
Player.Source = new Uri("http://fileraja.com/tamil/A/Alaipayuthey/Pachchai_Nirame-VmusiQ.Com.mp3", UriKind.RelativeOrAbsolute);
Please give me a solution. How to set the source for the MediaElement for a remote audio?
I Used to add MediaElement to for ex. MainPage.xaml as:
<MediaElement Name="MediaContent" AutoPlay="True"/>
and in MainPage.cs Constructor or OnNavigateTo i Use:
MediaContent.Source = new Uri("Ur Uri String Here", UriKind.Absolute);
if you don't want to auto play it in page loading just set AutoPlay to False, and play it on button click -for ex.- or any other event like that:
MediaContent.Play();
if it doesn't work try to use:
MediaFailed, MediaOpened, MediaEnded ,CurrentStateChanged .. events to keep track on why it doesn't work.
I am trying to play a Viddler video in my application but i cant get it to work.
Here is my simple code:
XAML:
< MediaElement VerticalAlignment="Center" Visibility="Visible"
HorizontalAlignment="Center"
Name="myMediaElement" Height="350"
Width="640" />
And my c#:
myMediaElement = new MediaElement();
Uri url = new Uri("http://www.viddler.com/embed/5b17d44f/");
myMediaElement.Source = url;
myMediaElement.Play();
Any help would be great! When i arrive on the page, nothing happens, the application does not break it just does nothing....
Edit:
In the end I have just decided to call the http://www.viddler.com/embed/5b17d44f/ url into a Webview, its not the best idea but it works.
Dont create a new MediaElemet. You already have one created (XAML).
Set AutoPlay="True" in your XAML
Edit: Play works only if the media is already loaded.