MediaElement possible memory leak on new media load? - c#

I am developing WPF app for a media player in c# and I am using the mediaElement control to host media.
As I want the user to be able to load both video and images in the player, I ve made a simple if-then-else statement inside the mediaOpened event, to check every time that a new media is loaded, if it hasTimespan (and therefor the player sees it as a video) or else as a picture.
To check if this if/else statement was working, I placed a message box in each case to give me feedback that indeed the player recognizes correctly the media type.
So far so good.
I load a video, everything goes as expected and I get my message box saying "video!".
But when I load a 2nd video the message box appears 2 times.
When I load one more video, the message box appears 3 times!
Even weirder, if I load a picture next, the message box appears 4 times but instead of a sequence of message boxes like this,
"video!" "video!" "video!" "picture!", I get
"picture!" "picture!" "picture!" "picture!".
It seems that the player is storing the media (or the mediaOpened events) in some kind of a list and every time I load a new one, it checks all the media in that list and gives me a message for each one. (I haven't wrote any code to support a playlist feature yet, so I don't know where these media could be being stored...)
I ve tried stating the mediaElement's source as null when the user presses the load new media button, to make sure the mediaElement source is clean before the new media gets loaded, but it did not have any effect.
Does this sound like a memory leak?
Am I missing a specific unloading event that I should call upon media change?
Thank you!

So it turns out that in most online examples on how to develop a mediaplayer,
everybody suggests using this next line upon clicking the load button in the player:
mediaElement.MediaOpened += new RoutedEventHandler(mediaElement_MediaOpened);
but no one seems to be aware that these media need to be unloaded when you load a new one, which is possible by placing the exact opposite, before we set the new source of the media element, so it would be in this order for example:
mediaElement.MediaOpened -= new RoutedEventHandler(mediaElement_MediaOpened);
mediaElement.Source = new Uri(dlg.FileName);
mediaElement.MediaOpened += new RoutedEventHandler(mediaElement_MediaOpened);
Hope this helps somebody out there!

Related

Xamarin Forms - attaching scrollview.Scrolled to a function

When I attach a scrollview to a function like this
textScroll.Scrolled += (sender, e) => { onScrolled(); };
Each time I scroll up or down, OnScrolled() is called multiple times. I know I can get the size of the content and compare it to the ScrollY value, obviously the ScrollY value changes each time, but as far as I can see I won't know when the last call happens (per user scroll).
I only want to call this once per scroll, failing that call it each time as is happening now, but only act when I know I'm on the last call.
Is this possible?
thanks
It is possible, but with a custom renderer for each platform.
On iOS: you will want to implement delegates for DecelerationEnded and WillEndDragging. The reason for also implementing DecelerationEnded is to allow for a fling by the user and waiting for the velocity to come to 0.
On Android it is a bit more complicated. Here is a native Android SO post I followed and translated into c# in a renderer. Works pretty well for me.
Android: Detect when ScrollView stops scrolling
After having both implemented, you can call to your Xamarin.Forms view in order to notify that the view has Stopped scrolling (i.e. final call)

Playing a video without opening another window or having the buttons

I have a C# WinForms application, when you press a button at run-time, the application has to play a video but not in another new window, I want it to be in the form itself, also I don't have to see the Play/Pause/Stop etc. buttons of the Windows Media Player, just to have the video playing and when it ends return to the beginning where the button is situated. I couldn't find a question similar to this one, anyone has any ideas ? If any more information is needed just let me know.
Thanks alot!
Using Windows Media Player Control:
You can hide control buttons by setting uiMode property to none.
You can enable loop mode using settings.setMode method and setting loop to true
You can set a file path to play, by assign a path to URL property.
Since the settings.autoStart property is true by default, when you set the url, the player plays the media. Also you can use commands like Ctlcontrols.play or other control commands.
Sample Code
this.axWindowsMediaPlayer1.uiMode = "none";
this.axWindowsMediaPlayer1.settings.setMode("loop", true);
this.axWindowsMediaPlayer1.URL = #"d:\video1.wmv";
Resources
How to: Embed Windows Media Player on a Form
AxWindowsMediaPlayer Object Properties, Methods and Events

Is my code legal in Android usage?

I asked a question recently about how to disable the back button is Android, after a while I got it working with these lines of code
public override void OnBackPressed ()
{
// base.OnBackPressed (); /* Comment this base call to avoid calling Finish() */
// Do nothing
}
And just recently someone commented this
Disabling the back button is counter-intuitive and breaks the device
usage contract imposed by Android. So i suggest you rethink.
-Question-
What would be a possible change to this? I dont want to be able to press the back button when playing my quiz game because that would make be able to cheat. New to android Development
Instead of simply making the back button do nothing, you could have it create a popup asking something along the lines of "Are you sure you want to leave the quiz? (This will count as a loss)". And have it take the user back to the main page of your app if he confirms (instead of back to the previous page).
Why not imitate what many websites do and make it so going 'back' to a page works but doesn't display any information?
It depends on your code, but perhaps you can make your buttons and text (or whatever it is you don't want them interacting with) change to be unseen whenever they move on to a new page. Or just throw up a message that says 'You can't do that' to cover the page that they'll only ever see if they go back to view it again.

Capture and save multiple images with `CameraCaptureDialog`

I have a Windows Mobile (Compact framework 2) application that defines a user control MPhotoControl. MPhotoControl shows a default image and when the user clicks on this image a CameraCaptureDialog is opened to allow a photo to be captured. Once captured, the photo is then displayed in the user control. This works fine for capturing a single photo and then going back to the application.
The problem is that when there are lots of these controls on a particular form then the user interface becomes very unfriendly because the user has to show the camera dialog, take a photo, save and close the dialog for every photo control on the form. What the users are asking for is a mechanism to open the CameraCaptureDialog, take several photos without the dialog closing until all the photo controls have images.
I am trying to implement this, but I don't see a way to get the CameraCaptureDialog to capture and save several photos at once. As far as I can tell it is not possible, because when the dialog shows on my HTC Touch Diamond, I only have the options to "Accept the photo" (arrow icon), "Capture again" (camera icon) or "cancel and close dialog" (dustbin icon). And when I click the arrow to accept it always closes the dialog box.
So does anyone know of a way of capturing and saving more than one image at a time using CameraCaptureDialog?
I then thought of trying to open the CameraCaptureDialog multiple times as a work around. So as soon as the first image is saved the dialog is immediately opened again to capture the second image. Here is my code showing my attempt at a workaround:
public partial class MPhotoControl : UserControl
{
public static IEnumerable<MPhotoControl> PhotoControls;
...
private void CaptureMultiplePhotos()
{
foreach (MPhotoControl photo in PhotoControls)
{
using (CameraCaptureDialog cameraDialog = new CameraCaptureDialog())
{
if (cameraDialog.ShowDialog() != DialogResult.OK)
{
break;
}
photo.CapturePhoto(cameraDialog.FileName);
}
}
}
}
The problem with this is that the CameraCaptureDialog still only opens once and the subsequent call to the ShowDialog method simply returns DialogResult.Cancel. So, does anyone know why this workaround does not work and if it is possible to get the dialog to immediately re-open once the previous captured image has been saved?
Please look here: http://www.hjgode.de/wp/2012/10/17/windows-mobile-cameracapturedialog-alternative/
I am unable to attach any code or binaries here, so I did a new blog post.
The code start the camera app, waits for its close-down and presents you with a list of new photos.
Code is not yet perfect but a starting point.

Turn off default sound when displaying MessageBox in WP7

Is there a way to turn off the sound that the Messagebox plays when it is displayed in WP7?
There's no way to change this behaviour with the Silverlight MessageBox class. If, however, you use Guide.BeginShowMessageBox (from the XNA libraries) you can control whether sounds are played or not.
No, currently there is no way you can disable the sound that is triggered by the MessageBox class. That is a feature tied to the system and unless the device is on mute, the sound will be played.
With XNA's async version you can do this, though (as mentioned by Matt):
Guide.BeginShowMessageBox("Title", "Text", new List<string>() { "OK" }, 0, MessageBoxIcon.None, new AsyncCallback(YourCallback), null);
Here, MessageBoxIcon defines the sound and not the icon (which is nonexistent on Windows Phone for a MessageBox).
Note: you need yo add a reference to Microsoft.Xna.Framework.GamerServices.

Categories