Sound disabled after changing background image - c#

I have an issue with sound being disabled after changing a background image.
Basically, what I have is a PanoramaItem. In that Item I have placed 1 big image as the background image (lets say a flag). On that flag I have several buttons that will change the button image on press (XAML Code created in Blend) and will also play a sound. (using Click Event Handler Sound.Play();)
All works fine, as long as the image is preloaded (. Layout-Image is in the back and buttons are on top/front.
However, I have now also added a button that enables the user to change the image to something from their own library, using the PhotoChooserTask. The problem now is that, yes the image gets replaced by the one the user selects but the buttons are not playing any sound anymore. The Button image still changes though, when pressed and released, which means the button is still active/enabled yet the sound is either disabled or the click even handler is no longer working.
Would you have an idea as to why that is? I played around with it all night last night and couldn't figure it out.
Any help is greatly appreciated :-)
EDIT:
Here is the code that I am using to load the picture:
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
BackGroundIMG.Source = bmp;
}
}
My XAML Code for the Picture is:
<Image Name="BackGroundIMG" Source="/Assets/Test.jpg" Grid.RowSpan ="3" Grid.ColumnSpan = "3"/>
XAML Code for MediaSound is:
<MediaElement x:Name="Theme" Source="/Assets/Theme.wav" Volume="1" Autoplay="False"/>
XAML Code for the Button is:
<Button Name="Button1" Click="Button1_Click" Grid.Row="1" Grid.Column ="1"/>
Image and Button are in the same Grid. under Layout I have set the BackGroundImage to "Sent to Back" and the Button is on top of it.
C# for playing Sound:
private void Button1_Click (object sender, RoutedEventargs e)
{Theme.Play();}
So again, the sound plays fine when I start the App and just press the button but no sound will be played once I change the background image.
So my guess is, that the picture is not loaded correctly, but I am not sure how to test/change that.

I think the issue is not exactly with changing the image but with the opening of the PhotoChooserTask. I suspect if you used any of the built-in launchers and choosers the audio would cut out as well. http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.tasks(v=vs.105).aspx
For a MediaElement to work it has to be part of the visual tree, otherwise it will stop playing. When these chooser tasks open they are navigating away from the current page. I think if you use a global MediaElement in your App.Xaml instead you could get around this behaviour. This post has a method of creating a global media element: Global MediaElement that continues playing after navigating to other page

Related

Faster image paint method

My project consists of a video player and a image viewer, both fullscreen in a panel with only one visible at a time. When the video pauses I capture the frame, display it in the image viewer and swap which control is visible. The problem is, when the image viewer is shown you see the previous image for a brief second before the new image is painted.
Does anyone have any suggestions on how I can prevent this? Ideally I want it to be seemless when the controls swap.
I've already set the image viewer to be double buffered.
Edit:
Code, doesn't really show much here.
private void ShowImageView()
{
this.axWindowsMediaPlayer.Visible = false;
this.imageViewer.Visible = true;
}
private void ShowVideoView()
{
this.axWindowsMediaPlayer.Visible = true;
this.imageViewer.Visible = false;
}
I've managed to solve it, the image viewer had a Image Changed event that I hadn't spotted (I'd tried using the Image Loaded event). Subscribing to the image changed event and only showing the control then, seems to give me the transistion I was looking for.

Is there any default time interval for mediaended event to be fired?

I have a WPF application which dynamically loads video or image files(depending on user's choice, to a MediaElement control.
It is working fine when it is a video and gets the MediaEnded event fired on ending the video.
But when I load an image, the MediaEndedevent is fired within 5 seconds.
Is it a default value? or can I change it programmatically?
Is there any property to change this interval or disable such an option?
Is it possible to make it paused until a specific action?
I have set the following properties as follows
MediaControl1.LoadedBehavior = MediaState.Manual;
MediaControl1.UnloadedBehavior = MediaState.Manual;
MediaElement is a (very thin) wrapper around Windows Media Player (or rather - uses the same framework which is used by Windows Media Player). If you open an image in Windows Media Player - you will see it will "play" it like a slideshow (even for 1 image), for about 5 seconds. That's why you get MediaEnded event in 5 seconds - Windows Media Player plays slideshow with your image for that duration. I doubt there is a way to change this from WPF (because it's behavior of external program\framework, not related to MediaElement itself) and I'm not aware of the way to change this for Windows Media Player (and even if there is such a way - it will have global effect and you probably don't want to modify your clients computer in such a way).
To solve your problem - just don't use MediaElement for displaying images - use something like Image control. If you have really strong reasons to do that - you can pause MediaElement with Pause method after your "slideshow" has been loaded, then it will not fire MediaEnded event. All in all - I cannot imagine any use case where you really have to use MediaElement for images.
You can repeat the media when you load image. Handle MediaEnded event like this:
void me_MediaEnded(object sender, EventArgs e)
{
//play video again
mediaElement.Position = new TimeSpan(0, 0, 1);
mediaElement.Play();
}
Additionally see this. It may help:
https://stackoverflow.com/a/3406857/5675763

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();
}

WPF mediaElement disappearing after some time

I have a user control with a WPF MediaElement and an Image control. This user control is again used in a few more user control which uses this in two differnt layouts. The application is supposed to show a list of videos and images for a long period of time in loops. Each user control will be loaded for a particular time period and after that time, this will be unloaded and the next control will be loaded. When the videos are running every now and then the position of the video is changed to show a particular part of the video.
XAML Code. The MediaElement is inside a Grid with one column and one row.
<MediaElement Volume="0" Name="m1" Stretch="Fill" Visibility="Hidden" LoadedBehavior="Manual" UnloadedBehavior="Manual" />
C# Code, setting source
this.mediaElement.Source = item.Path;
this.mediaElement.Position = new TimeSpan(0, 0, 0);
this.mediaElement.Play();
C# Code changing position. This position change is done using a DispatcherTimer
t = TimeSpan.FromMilliseconds(Position);
mediaElement.Position = t;
C# Code disposing the MediaElement. I use this code to dispose the MediaElement because previously I found out after playing for a while the MediaElement was using too much memory and becomes frozen after a while.. The below code fixed that issue.
mediaElement.Close();
mediaElement = null;
GC.Collect();
GC.WaitForPendingFinalizers();
The application works fine, but the problem is after some time (like 20 mins) the WPF MediaElement disappears. The application works smoothly (It doesn't get frozen), the images are shown, but the MediaElement is gone.
When this problem occurred, I did some debugging and found these.
The WPF MediaElement visibility is Visible. The Source has correct filenames. The MediaElement is not hidden behind any other controls.
However the actual width and actual height are shown as 0.
I checked all the code to find out where the height and width becomes zero. But could not find any. So I decided to try and override the Height and Width evey millisecond using a DispatcherTimer (Just to check if it was possible). But even then the 0 in actual height and width remained the same.
Also, the application doesn't seem to use excessive memory and everything else seems to be fine.
Does anyone got a clue as to why this is happening? Any solutions or workarounds? Thanks

Stop preview after photo is taken

My app is supposed to take a photo, then stop there showing the photo, save it in a file and move on to another view.
I use the Microsoft.Devices.PhotoCamera and wait after a photo is taken for CaptureCompleted to take the image and for CaptureImageAvailable to store it and then move on.
Whan happens is: After a picture is taken, I get the camera preview again, which might be confusing for users (though a progress-bar is shown to tell them "wait pls". still not nice) and after 1-2 seconds it shows the taken picture again and moves on to the next view.
Is there a nice way to disable this preview-thing?
If I dispose the PhotoCamera after the first event, it hangs. If I dispose it after the 2nd event, it doesn't seem to make much difference..
Edit:
private void Cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)
{
this.imageStream = e.ImageStream;
Deployment.Current.Dispatcher.BeginInvoke(delegate
{
this.image = new BitmapImage();
this.image.SetSource(this.imageStream);
this.PreviewImage.Source = this.image;
// Part where I store the image into a file.
// Part where I navigate to the next view.
});
}
I need that Dispatcher-Thing because setting images and navigating needs the UI-thread but the method runs in a different one.
Also a strange point: it doesn't matter that the PreviewImage lies OVER the canvas with the camera-preview. It is still overwritten as soon as the picture is taken and being processed... ôo
The problem might be that I do many things and the UI is never refreshed. I could add a small sleep (1-5ms) but that would be async again. Also I can't do this completely serparated because the storing part makes a "screenshot" which should contain the final image instead of the camera-preview-thing.
Edit2:
I see the main problem in the capture itself. It takes a picture, shows it for a while, then switches to the camera preview and then back to my shown picture.

Categories