I'm using MediaElements to play Local Sound Files In a WP7 Application (MP3's)
I'm using this system for Background Music (Ambience) and Button Clicks (User interaction) so there is no need for users to pause/rewind/restart this sound.
These are "play and forget" sounds, and I'd like two or three sounds to be playing simultaniously -- but right now that doesn't happen.
--
When I attempt to play My 'ButtonClick' mp3 file through the MediaElement, it works, and it can also be stopped and then restarted if the user clicks the button again.
I have it set up so when a button is clicked, the sound plays - which it does.
Problems:
However, my second MediaElement that is set up to play the Ambience track at the same time doesn't even play, as if it is broken, or the track cannot be found. Despite asking it to play in my code, and checking the code.
Is there any way to make two MediaElements play Simultaniously? I actually think I did it before, but I forgot how I actually achieved it.
Current Code:
XAML
<MediaElement Height="120"
HorizontalAlignment="Left" Margin="89,263,0,0"
Name="mediaElement2" VerticalAlignment="Top"
Width="160" Source="Audio/ButtonClick.mp3" AutoPlay="False" />
<MediaElement Height="120"
HorizontalAlignment="Left" Margin="89,263,0,0"
Name="mediaElement1" VerticalAlignment="Top"
Width="160" Source="Audio/OverviewMusic.mp3" AutoPlay="False" />
C#
mediaElement2.Stop();
mediaElement2.Play();
Only One MediaElement can play at any given time.
Xna has functions which allow for more sounds to be playing at once, however XAML and CS don't come with this as default.
Related
We are currently playing a 4K h.265 video in an UWP app as background using MediaPlayerElement. We have set the MediaPlayer to infinite loop using IsLoopingEnabled = true. The problem is, that the memory usage is increasing every time the video loops. If we disable looping, the memory leak does not occure. We tried looping the video manually by resetting the position to zero when the video finished, but still it leaks memory. We also tried to call System.GC.collect() but that also did nothing.
Is this a UWP bug or are we missing something?
Edit:
Here is the code we are using:
MainPage.xaml
<Page
x:Class="MyProject.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:MyApp.Controls"
xmlns:xaml="using:Microsoft.Graphics.Canvas.UI.Xaml"
mc:Ignorable="d"
Background="Black" Loaded="MainPage_OnLoaded" Unloaded="MainPage_OnUnloaded">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="{StaticResource AppBrushNewBlue1}">
<MediaPlayerElement Name="bgMovie" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AreTransportControlsEnabled="False" ></MediaPlayerElement>
<Canvas Name="mainCanvas" ManipulationMode="None" Background="Transparent">
</Canvas>
</Grid>
MainPage.xaml.cs
private MediaSource ms;
private async void MainPage_OnLoaded(object sender, RoutedEventArgs e)
{
ms = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/bg_animation_V2.mp4"));
bgMovie.Source = ms;
bgMovie.MediaPlayer.IsLoopingEnabled = true;
bgMovie.MediaPlayer.Play();
[...]
}
We are not doing anything more with the MediaSource or the mediaPlayer itself. When we disable IsLoopingEnabled no memory leak occurs anymore.
As stated here https://stackoverflow.com/a/54947557/1018232 this seems to be a bug in Windows 10. Even the builtin video player "Movies and TV" has this issue. It seems this only happens on h.265 codec. Maybe it is a decoder driver bug or something like this.
There is always a confusing amount of different Media Player elements in WPF. You are using teh MediaPlayerElement. And it's documentation explicitly mentions that it is still in a open Beta-ish phase:
This control is currently available as a developer preview for Windows 10, version 1903, and later. Although we encourage you to try out this control in your own prototype code now, we do not recommend that you use it in production code at this time. For more information, see the XAML Islands feature roadmap. If you have feedback about this control, create a new issue in the Microsoft.Toolkit.Win32 repo and leave your comments there. If you prefer to submit your feedback privately, you can send it to XamlIslandsFeedback#microsoft.com.
So it is entirely possible you just found a bug.
As far as I can tell, the approach to playing Media before the MPE is the Storyboard and the MediaPlayer/MediaElement combination. As far as I can tell, MediaPlayer has no repeat mode. So a event will have to do. Someone with deeper WPF/UWP knowledge will have to tell you wich one is the right one for the time being.
I have created a MediaElement in XAML and assigned a .mpg as the source.
Looking at the XAML controls gallery from Microsoft, there are some examples such as:
<MediaPlayerElement Source="Assets/SampleMedia/fishes.wmv"
MaxWidth="400"
AutoPlay="True" />
However, I am faced with invalid markup that is returning:
The property AutoPlay was not found in type MediaElement
I have tried removing the AutoPlay and using a MediaOpened event to start the video from code behind,but there is never any trigger even?
I am not having too much success.
DO I need to add any additional resources to access certain properties?
<MediaElement x:Name="backdropMotion" Source="Images/inMotion_video.mpg"
Stretch="Fill" Margin="198,126,-326,229" Grid.Column="2"
LoadedBehavior="Play" IsEnabled="True" IsMuted="True"
Grid.Row="1" Grid.ColumnSpan="2" MediaOpened="MediaOpenedEvent"
AutoPlay="True"
>
Ultimately, I am trying to run a media element to a panel that effectively acts as an animated background, looping almost like a .gif. First step is to get the element running.
I'm building a Windows 8 prototype for a large touch enabled 4K screen. My client has a library of existing HTML5 games (the game source is unavailable) they want multiple people to be able to play these browser games on the 4K display at once.
I've inserted 2 WebViews into my app and the games load fin; but only one game can have focus at once, forcing every other game into it's pause screen (automatic behavior in the game when the browser doesn't have focus).
My question is: Is their a way I can prevent the WebView from losing focus or prevent it from knowing that it lost focus? My existing XAML looks like this:
<Grid x:Name="MainGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".5*"></ColumnDefinition>
<ColumnDefinition Width=".5*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<WebView Name="LeftBrowser"
Grid.Column="0"
Source="{Binding LeftBrowserURL, ElementName=browserWin8Window}"/>
<WebView Name="RightBrowser"
Grid.Column="1"
Source="{Binding RightBrowserURL, ElementName=browserWin8Window}"/>
</Grid>
Tldr: I have mutiple WebViews in my Windows Modern app, is there a way I can make them all think they have focus at once?
P.S.: if this is easily done in something else that runs on Windows, please tell me.
If anyone else needs to do something like this in the future, I solved my problem using the WPF version of CefSharp with a custom IWpfFocusHandler.
I am working on windows store app (using xaml/C#), my question is can I have two media-elements on a single page, one will always play some background music and second will play some other sound depending upon user actions?
I tried it but not working for me, only background music is getting played the other sound is not getting played.
I can't see problem. you can use 2 media elements on the same page. mediaControl1 plays always and mediaControl2 starts to play when user clicks on button.
<Button Width="150" Height="150" Style="{StaticResource ButtonAppStyle}" Click="MainPage_Loaded">
<MediaElement x:Name="mediaControl1" Height="400" Source="Assets/muzika.mp3" RealTimePlayback="True" IsLooping="True" />
<MediaElement x:Name="mediaControl2" Source="Assets/firework1.wav" IsMuted="True"/>
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
mediaControl2.IsMuted = false;
mediaControl2.Play();
}
We are trying to switch our application from code behind coding to MVVM pattern.
We have a MediaElement in our XAML file :
<MediaElement Name="MyMedia" LoadedBehavior="Manual" UnloadedBehavior="Manual" MediaEnded="MediaEnd" MediaOpened="MediaBegin" Margin="10,24,13.6,10" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Visibility="Visible" />
For the moment, with the code behind coding approach, we can access the MediaElement directly in the .xaml.cs file to call methods like MediaElement.play() or MediaElement.pause().
The problem is that we are currently blocked when we try to convert our code to MVVM, because we can't access the MediaElement in the .cs file.
We are trying to bind the MediaElement itself so that we can call play or pause methods, but we haven't succeeded yet.
Any ideas ?
Thank you in advance.
This link describes how to control it http://msdn.microsoft.com/en-us/library/ms748248.aspx
it uses an event but still calls the MediaElement.play() in the event handler.