How to play audio in the background of a UWP - c#

I created a media player app using UWP for Windows 10. Currently I cannot play any audio in the background, when I minimize my media player window the audio stops playing and resumes when I maximize the window. This is my media element XAML code I used:
<MediaElement x:Name="PlayerElement" Height="532" Width="829"
AreTransportControlsEnabled="True" Stretch="Uniform" HorizontalAlignment="Center"
VerticalAlignment="Center" PosterSource="/Images/Logo3.png"
AutoPlay="True" MediaEnded="PlayNewSong" Margin="0,0,-9,0" AudioCategory="BackgroundCapableMedia"/>
I thought the AudioCategory property would solve my problem, but unfortunately it doesn't. Any help would be appreciated.

Check this official sample collection from microsoft WindowsUniversalSamples
You can pull BackgroundMediaPlayback folder for sample project you need.

Related

How are the semi-transparent blur windows in OSX and W10 Start Menu created?

I'm currently working on a window that focuses on some elements on screen while blurring the rest of the area.
Using common methods like the WindowCompositionAttribute and the others are not suitable for this situation as there are limitations to it and it doesn't meet the standards regarding the intensity of the blur, contrast and colors which should be strict.
What i have managed to solve my problem was building an image stream with a light image encoder to enhance performance but then that wasn't enough. So, i thought of writing a motion detection algorithm to only steam when there's motion, but it still didn't change the performance drops.
What i need is something like those of the native OSX windows and Windows 10 Start Menu, so how are they created and run efficiently without any heavy load on the performance?
To create a new Window from scratch you have to set WindowsStyle to none (AllowTransparency="True" can be set only along with WindowsStyle="None") and from there build the style of the window.
However, you will face several issues if you follow this approach:
-no buttons (minimize, close)
-no support for fullscreen (taskbar issues)
I would suggest you to have a base view and include the other views inside the main view(which has the blur effect).
The blur effect could be obtained easily by doing something like below:
<Grid>
<Grid Margin="5" Background="#FF3782DC">
<!--<Grid.Background>
<Image Source="test.png"></Image>
</Grid.Background>-->
<Grid.Effect>
<BlurEffect Radius="100" />
</Grid.Effect>
</Grid>
<TextBlock
x:Name="textBlock"
Height="61"
Margin="136,82,211,0"
VerticalAlignment="Top"
Text="test"
TextWrapping="Wrap" />
</Grid>
I've set a color for the background, but you could set an image as background and by default it would be blurred (here you should set a binding and every time the view changes, you have to take a snapshot of the screen and set the source of the Image). However you will probably have some performance issues, but with a good encoder (JPEGencoder) it will be fast enough.
Hope that helps you!

UWP Windows.Media.Playback - Stop/Unload Clip

I'm writing a UWP application which contains the MediaPlayer object. I am wanting to program a stop button which stops the media and sets the media to black however I can't find an approach to do it short of loading a short 'black' clip as an internal asset or hiding the element via the compositor.
The MediaPlayer does not have a function for stop, only play/pause and further, setting the media source to null simply pauses playback and freezes on the last frame shown rather than dropping to black.
Any suggestions would be much appreciated.
I recently had a related problem that boiled down to unloading media from a MediaPlayerElement.
If you navigate away from a page when playback is active, the MediaPlayerElement would not suspend or unload a playback in progress so the user would keep hearing the audio. If you re-open player's page, a new instance of a player control will be created and user may hear multiple audio tracks at once (although this requires different media sources). So it was essential to unload the media at some place like player's Unloaded event handler, page Unloaded handler, or OnNavigatingFrom/OnNavigatedFrom overrides.
Anyway, the code below stops and unloads a video/clip/media from the MediaPlayerElement.
MyMediaPlayerElement.MediaPlayer.Pause();
MyMediaPlayerElement.Source = null;
Actually, a null assignment line works just fine as a stop-and-unload, but since as of now (3/19/2018), MS documentation doesn't seem to cover how nullifying the source supposed to work, I prefer to keep an otherwise unnecessary call to Pause() here as well.
The MediaPlayer does not have a function for stop, only play/pause
As you known, MediaPlayerElement doesn't have stop method at default. Since MediaPlayerElement is newly for version 1607, for earlier version of Windows 10 we use MediaElement instead, so you can simply use MediaElement instead. MediaElement has Stop method, and after you invoke the stop method, the MediaElement will show black and set the media to begin which is just what you want. Code as follows:
<MediaElement x:Name="mediaelement" AreTransportControlsEnabled="True" Height="400" Width="400" AutoPlay="True" Source="http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_30fps_normal.mp4"></MediaElement>
<Button x:Name="btnstop" Click='btnstop_Click' Content="stop"></Button>
Code behind:
private void btnstop_Click(object sender, RoutedEventArgs e)
{
mediaelement.Stop();
}

Is there a way of using ffmpeg in c# app?

I'm using the ffmpeg.org and when I run ffmpeg -y -f vfwcap -r 25 -i 0 out.mp4 in command line I can grab the video from my webcam and write it to the out.mp4 file. However, I can't see that stream anywhere. I thought about writing some simple wrapper in c# that is built on ffmpeg functionality, so far I found post mentioned on Stack before, but there's nothing about displaying the data live (instead of saving it into the file). Does anyone have any experience with it? Can I for example 'draw' the received data from webcam on a picture box or on some other component?
Thanks!
One of the comments in your linked post says this:
How about writing a C++/CLI wrapper around ffmpeg's native interface
and then calling your wrapper interface from your application?
I think this is exactly what you want to do. (Note that FFmpeg has worked fine for years in recent versions of Visual Studio, so the response in the linked post to this comment doesn't apply.)
You would basically create a camera input (this lives in libavdevice), and then you would encode this to h264 in a mp4 container (see output_example.c). To get a live display, you would take the data generated from the vfwcap source, decode it (using the "rawvideo" decoder in libavcodec). This gives you a AVFrame, which has the data pointers to display the image in any native UI element in your application, typically using direct3d or opengl. Read the documentation to learn more about all of this.
You could use a MediaElement or MediaPlayer control.
MediaElement is a UIElement that is supported by the Layout and can be
consumed as the content of many controls. It is also usable in
Extensible Application Markup Language (XAML) as well as code.
MediaPlayer, on the other hand, is designed for Drawing objects and
lacks layout support. Media loaded using a MediaPlayer can only be
presented using a VideoDrawing or by directly interacting with a
DrawingContext. MediaPlayer cannot be used in XAML.
MediaElement
Sample XAML:
<MediaElement Source="path\to\out.mp4" Name="myMediaElement"
Width="450" Height="250" LoadedBehavior="Manual" UnloadedBehavior="Stop" Stretch="Fill"
MediaOpened="Element_MediaOpened" MediaEnded="Element_MediaEnded"/>
MediaPlayer
//
// Create a VideoDrawing.
//
MediaPlayer player = new MediaPlayer();
player.Open(new Uri(#"sampleMedia\xbox.wmv", UriKind.Relative));
VideoDrawing aVideoDrawing = new VideoDrawing();
aVideoDrawing.Rect = new Rect(0, 0, 100, 100);
aVideoDrawing.Player = player;
// Play the video once.
player.Play();
Multimedia Overview on MSDN

Video Controls appears for few seconds when user tries to launch video

I'm playing audio file from network ,when i launch the audio for fraction of second video controls appears and then it turns to audio control.I tried for 'IsAudioOnly' property but result is same.
Following in my xaml code :
<MediaElement x:Name="EtextAudio" Visibility="Visible" AutoPlay="True" AreTransportControlsEnabled="True" Height="100" Width="400"
Source="{Binding LinkObjectModel.LinkValue"/>

Implement Flick Gesture via GestureService from Windows Phone 8

I'm working on a Windows Phone 8 app. This app will allow a user to flick a panel up. I want this to work very similarly to the way the lock screen works. When the user 'flicks' the panel up, I want it to automatically, move up accordingly. Does anyone know how to do this? Currently, I have the following:
<Grid x:Name="myGrid" Background="Peru" VerticalAlignment="Stretch">
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener x:Name="myGridGestureListener" DragStarted="myGridGestureListener_DragStarted" DragDelta="myGridGestureListener_DragDelta" DragCompleted="myGridGestureListener_DragCompleted" Flick="myGridGestureListener_Flick" />
</toolkit:GestureService.GestureListener>
<Grid.RenderTransform>
<TranslateTransform x:Name="bannerGridTransform" Y="5000" />
</Grid.RenderTransform>
</Grid>
private void myGridGestureListener_Flick(object sender, FlickGestureEventArgs e)
{
if (e.Direction == System.Windows.Controls.Orientation.Vertical)
{
}
}
For the life of me, I can't figure out how to get myGrid to smoothly react to the flick gesture accordingly. I figured someone would have already implemented this, however, apparently, I'm wrong.
Thank you!
Gesture Listener is deprecated in Windows Phone 8. When you download the latest toolkit source. It contains a sample toolkit app for window phone 8. When you click the gestures, it throws a dialog which says
The GestureListener is now obsolete in windows phone 8, as the built
in manipulation and gesture events now have functional parity with it.
This sample and the sample code demonstrated how to use the
manipulation and gesture events for purposes for which one previously
would have used the GestureListener
If someone need to implement gestures in Windows Phone 8 - look at this https://github.com/PedroLamas/WPtoolkit/blob/master/PhoneToolkitSample8/Samples/GestureSample.xaml.cs
Unfortunately this isn't as trivial as a couple of lines of code on SO since you have to account for the pan while you are holding down the screen and the flick which needs to seamlessly continue on from the pan when the user lifts their finger. A great example of this is actually a darts game since you want the dart to move while you are dragging and then fly off when you release (the flick).
You can find a great example with source code at http://windowsphone7developerguide.blograby.com/darts-gesture-listener-flick-gesture/
You dont need to use toolkit gesture listener anymore. You can use in built manipulation events. Here is the sample:-
http://phone.codeplex.com/SourceControl/latest#PhoneToolkitSample8/Samples/GestureSample.xaml.cs

Categories