Play a sound when I run my app - c#

I have created an app, this app helps us to lock the windows phone screen with one touch. Now I want to play a sound when it locks the screen. I've tried to use MediaElement but it doesn't work.
This is my code (audio is MediaElement's name) :
public MainPage()
{
InitializeComponent();
audio.play();
new WPRequestScreenClosed().RequestLockScreen();
Application.Current.Terminate();
}
Any help would be much appreciated!

You could use the MediaEnded event to know when the sound is done playing and then close the application at that point.
private void MediaElement_MediaEnded_1(object sender, RoutedEventArgs e)
{
Application.Current.Terminate();
}

Related

How to change a video once the current has stopped?

I am currently working with .NET 4.5. Programming a photobooth. The photobooth should start wit fullscreen:
Show an Intro screen with an intro video.
When the user clicks on the screen, it has to show a get ready! video
When the get ready video finishes playing, it has to show a countdown video
When the countdown video finishes, it has to take a picture.
Right now I can do #1 and #2. I can also take pictures. My problem is with the sequence. Once a video finishes, I need to change the video, but since everything runs in the event handler, it only plays the video WHILE the program is running the event handler.
Is there any better and smarter way to do this?
Here is my code:
What I have and works right now:
private void Form1_Load(object sender, EventArgs e)
{
// maximize the screen
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
TopMost = true;
// play an intro video without the UI controls
this.videoPath = this.basePath + "intro.mp4";
this.wmpVideo.URL = this.videoPath;
this.wmpVideo.settings.setMode("Loop", true);
this.wmpVideo.Ctlcontrols.play();
this.wmpVideo.uiMode = "none";
}
Then, if the user clicks on the video, it will change the video to a new file:
private void wmpVideo_MouseDownEvent(object sender, AxWMPLib._WMPOCXEvents_MouseDownEvent e)
{
if (this.current == 0) //this means that currently we are in the intro
{
// Then I change the video to "get ready!"
this.changeVideo("getready.mp4",1);
}
changeVideo basically updates the video to getready.mp4. Then, I need it to change it to countdown.mp4.
In order to move from one video to another, I need to check when the video finishes. I do this right now with:
wmpVideo.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(checkGetReadyFinished);
And the event handler looks like this:
private void checkGetReadyFinished(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8) {
// If we are currently in the "getready state"
if (this.current == 1)
{
// then I change the video to the countdown.mp4
this.changeVideo("countdown.mp4", 2);
}
}
}
PROBLEM
I noticed the video ONLY plays when I am on the event handler method. I realized this because I saw that no video was played, so I added a MessageBox in the changeVideo method. I saw that as long as the MessageBox was showing, the video was playing. When I clicked "OK", the video disappeared
I.E., I need to stay in the event handler method.
QUESTION
Is there any other better way to show a video1, then show video2 when video1 finishes. As I mentioned, I need to do a photobooth program.
I also tried calling another form, but as soon as I set it to fullscreen, the screen sort of flashes.
Is there any optimal way of doing this?

how to set WPF to allways recognize input

I am working on an wpf project with leap motion. My programm is allways checking for input via the leap motion controller and does something with it.
But when I open another programm (and my wpf gets in the background) my wpf freezes.
Is there a way my programm will not freeze even if I open another programm in fullscreen ?
EDIT:
here is how i get my input:
public MainWindow()
{
InitializeComponent();
CompositionTarget.Rendering += Update;
}
protected void Update(object sender, EventArgs e)
{
//get frame
Leap.Frame frame = leap.Frame();
...
}
Thank you Charles Ward this did it for me:
controller.SetPolicy(Controller.PolicyFlag.POLICY_BACKGROUND_FRAMES);
Leap doesn't seem to update when wpf window isn't active

Minimize Event ( C# Win 8.1 app)

I'm developing an App for windows 8.1 and i need to execute a method to pause some tasks or play a sound when the app is minimized in the sidebar.
I tried:
Application.Current.Suspending += new SuspendingEventHandler(Method_Name);
private void Method_Name(object sender, object e)
{
Other_Method();
}
But this event takes a few seconds (5~10) to occur after I minimize the app.
What event occurs when the app is dragged to the sidebar? What process sends the event?
Thanks.
Check out this post for the answer. It's WindowSizeChanged and check the value of ApplicationView.Value.
[EDIT]
Apparently for 8.1 things have changed a bit. The ApplicationView stuff is deprecated (that was quick) but this still takes place in SizeChanged for the window. Check out this for more details.
After long searching I found something that is not exactly what i wanted, but it works.
This event occurs everytime that visibility of the page changes (Is started, is maximized or minimized for exemple), then you must do some conditions using the if operator.
Window.Current.VisibilityChanged += new WindowVisibilityChangedEventHandler(One_Method);
private void One_Method(object sender, Windows.UI.Core.VisibilityChangedEventArgs e)
{
if(Some_Condition)
{
//TODO: Code that you want execute.
}
}
I'll keep the answer in open for the case of someone knows something more efficient.

Calling Play() on C# ShockWave component doesn't start YouTube video playback?

I have a simple WinForms C# application that embeds a ShockWave COM component on a form. I have a Test button that when clicked, calls the Play() method on the component. When I click the button nothing happens. The YouTube player is plainly visible in the ShockWave component with a video still and the player chrome in the frame, with a big Play button on it. But it doesn't start playing.
Sample code:
private void button1_Click(object sender, EventArgs e)
{
axShockwaveFlash1.Play();
}
Does anyone know how to fix this? I'm wondering if it's because the ShockWave component might need to have "requires click to play" or some other registry/system setting cleared before the Play() method works? If that happens to be the case, then how can I do that programmatically so a new install works perfectly without putting the user through some kind of a pre-setup hassle?
With Youtube videos and AxShockwaveFlash objects the Play() method doesn't function, however, there is a nice workaround for this.
The secret is adding ?autoplay=1 to the end of the URL containing the video you wish to play.
Something like this will provide the desired effect:
private void button1_Click(object sender, EventArgs e)
{
string path = #"http://www.youtube.com/v/Q-h4ulFK2Ek?autoplay=1";
axShockwaveFlash1.Load(0, path);
}

Close c# application when video stops playing

I am trying to figure out how to close my application when the video that the form plays stops playing. Currently, the user clicks a button to bring up Form2. The control for the video is set to force the video full screen, and start playing from the beginning of the video. I am using axWindowsMediaPlayer to provide the video. I am also a complete neophyte when it comes to C#.
How would I get my application to close when the video stops playing?
You need to detect when the media has ended, here is a tutorial on: "Detect the End of Media - axWindowsMediaPlayer". Then you need to call Close(); to close the form you are currently playing the media from.
Example (snippets from msdn):
// Subscribe to the Play State Change event
player.PlayStateChange +=
new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(player_PlayStateChange);
Then you need the event handler that can look like this:
private void player_PlayStateChange(object sender,
AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
// 8 = Media Ended
if(e.newState == 8) { Close(); }
}

Categories

Resources