How to play video on a certain screen

Greetings everyone,

I’m porting our latest game to WP8(and hope to do our previous games after that) and saw that the media api is not supported by Corona. As it seems, I have to call for a C# function to play video but I’m not sure how to do that. I need to play videos as cutscenes. Can anybody help with that?

You can play video via Microsoft’s “System.Windows.Controls.MediaElement” .NET class.  Microsoft documents it here…
   http://msdn.microsoft.com/en-us/library/windows/apps/jj207066(v=vs.105).aspx
   http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement(VS.95).aspx
 
I think Nokia’s website provides the best example on how to play a video file via C#.  Microsoft’s example only shows you how to do it via XAML, which is probably not what you want.
   http://developer.nokia.com/community/wiki/Video_Playback_with_MediaElement_in_Windows_Phone
 
Using Corona’s Lua/.NET bridge, your code should look something like the below…
(Note that it’s your responsibility to create and manage the MediaElment object on your page or on a separate page, because you’ll have to handle remove the MediaElement video player later once you’ve decided that you’re done with it… or caching it for later use.)

private CoronaLabs.Corona.WinRT.ICoronaBoxedData OnPlayVideo( CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment sender, CoronaLabs.Corona.WinRT.CoronaLuaEventArgs e) { try { // Create a path to a video file. // This example assumes a video file named "MyVideo.m4v" is in your Resource directory. var uri = new Uri( "ms-appx:///Assets/Corona/YourVideoFile.m4v", UriKind.RelativeOrAbsolute); // Play the video. // Note: It's your responsibility to create and store the MediaElement object. // Because you'll need to add the ability to remove it later, outside of this method. yourMediaElement.Source = uri yourMediaElement.Play(); } catch (Exception) { // Failed to load the video file! } return null; }

And we document how to use Corona’s Lua/.NET bridge here…
   http://docs.coronalabs.com/daily/coronacards/wp8/communication.html
 
I hope this helps!

Hi @Rob,

I’m a bit lost here :) I’m trying to make the example on the communication doc to work first but I fail to do that, too.

Here is how I call C# code from Lua:

local requestingMessageBoxEvent = { name = "requestingMessageBox", message = "Hello World!" } local result = Runtime:dispatchEvent( requestingMessageBoxEvent ) -- Print the message returned by C# print( "result is", tostring(result) ) --always returns FALSE

As I understand, I have to insert C# code into the MainPage.xaml.cs, right? Here is my MainPage.xaml.cs:

* When I try Console.WriteLine(), it doesn’t seem to print anything to the console (when debugging on device) so I’m not pretty sure that I have access to these methods.

private void OnCoronaRuntimeLoaded(object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e) { e.CoronaRuntimeEnvironment.AddEventListener("requestingMessageBox", OnRequestingMessageBox); //e.CoronaRuntimeEnvironment.AddEventListener("requestingMessageBox", OnPlayVideo); } private CoronaLabs.Corona.WinRT.ICoronaBoxedData OnRequestingMessageBox( CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment sender, CoronaLabs.Corona.WinRT.CoronaLuaEventArgs e) { var boxedMessage = e.Properties.Get("message") as CoronaLabs.Corona.WinRT.CoronaBoxedString; if (boxedMessage == null) { return CoronaLabs.Corona.WinRT.CoronaBoxedString.From("'event.message' is a required field."); } return CoronaLabs.Corona.WinRT.CoronaBoxedString.From("Message box was displayed successfully!"); } private CoronaLabs.Corona.WinRT.ICoronaBoxedData OnPlayVideo( CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment sender, CoronaLabs.Corona.WinRT.CoronaLuaEventArgs e) { try { // Create a path to a video file. // This example assumes a video file named "MyVideo.m4v" is in your Resource directory. var uri = new Uri( "ms-appx:///Assets/Corona/videos/Giris.mp4", UriKind.RelativeOrAbsolute); // Play the video. // Note: It's your responsibility to create and store the MediaElement object. // Because you'll need to add the ability to remove it later, outside of this method. mediaElement.Source = uri; mediaElement.Play(); } catch (Exception) { // Failed to load the video file! } return CoronaLabs.Corona.WinRT.CoronaBoxedString.From("Message box was displayed successfully!"); }

The code you posted is missing the part where you add a “Loaded” event handler in your MainPage’s constructor.  As in you need to add the following line to your MainPage.xaml.cs like this…

public MainPage() { // \<Your existing constructor code goes here.\> // Add a Corona event handler which detects when the Corona project has been loaded, but not started yet. fCoronaPanel.Runtime.Loaded += OnCoronaRuntimeLoaded; }

This sets up your OnCoronaRuntimeLoaded() method to be called when the Corona runtime gets loaded.  This is your opportunity to hook up your own custom Lua event handlers before your “main.lua” gets executed.

Well, I removed that part from the post to make the code more clear. It’s already in the code.

I tried adding some Console.WriteLine() to see if it enters the method but no luck with that. I don’t see it calling the method.

By the way, my

mediaElement

variable is a MediaElement component that I added with UI designer of Visual Studio to the MainPage.

I know what’s wrong.

Console.WriteLine() does *not* write to Visual Studio “Output” panel.  It outputs to stdout instead.

You need to call the static System.Diagnostics.Debugger.Log() method instead.

   http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.log(v=vs.110).aspx

It should look something like this…

System.Diagnostics.Debugger.Log(0, "Corona", "Hello World!!!");

Also note that this will write to Visual Studio’s “Output” panel if the .NET debugger is attached (which is the default) and not the Native C/C++ debugger.

You can also just put a breakpoint in event handler, like in your OnRequestingMessageBox() method, to see if it is actually being called.  You can add breakpoints by right clicking the line of code and selecting “Breakpoint” from the popup menu… or by left clicking the grey column to the left of your line of code.  The breakpoint will be displayed as a red circle.

I see. I’ll be trying that out on Monday because I don’t have access to that development PC right now and write about the result here.

Other than that, is my OnPlayVideo C# code right? Also, how can I call it from the Lua side correctly?

Your OnPlayVideo() method will work provided that you’ve created your mediaElement control before hand and it is already attached to your page.  Perhaps what you should do is create it in your MainPage constructor but don’t attach it to your page until a video needs to be played, such as within your OnPlayVideo() method.  You can then display it via…

fCoronaPanel.Children.Add(mediaElement)

And when the video is done playing, you can remove it from the CoronaPanel.

You can play video via Microsoft’s “System.Windows.Controls.MediaElement” .NET class.  Microsoft documents it here…
   http://msdn.microsoft.com/en-us/library/windows/apps/jj207066(v=vs.105).aspx
   http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement(VS.95).aspx
 
I think Nokia’s website provides the best example on how to play a video file via C#.  Microsoft’s example only shows you how to do it via XAML, which is probably not what you want.
   http://developer.nokia.com/community/wiki/Video_Playback_with_MediaElement_in_Windows_Phone
 
Using Corona’s Lua/.NET bridge, your code should look something like the below…
(Note that it’s your responsibility to create and manage the MediaElment object on your page or on a separate page, because you’ll have to handle remove the MediaElement video player later once you’ve decided that you’re done with it… or caching it for later use.)

private CoronaLabs.Corona.WinRT.ICoronaBoxedData OnPlayVideo( CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment sender, CoronaLabs.Corona.WinRT.CoronaLuaEventArgs e) { try { // Create a path to a video file. // This example assumes a video file named "MyVideo.m4v" is in your Resource directory. var uri = new Uri( "ms-appx:///Assets/Corona/YourVideoFile.m4v", UriKind.RelativeOrAbsolute); // Play the video. // Note: It's your responsibility to create and store the MediaElement object. // Because you'll need to add the ability to remove it later, outside of this method. yourMediaElement.Source = uri yourMediaElement.Play(); } catch (Exception) { // Failed to load the video file! } return null; }

And we document how to use Corona’s Lua/.NET bridge here…
   http://docs.coronalabs.com/daily/coronacards/wp8/communication.html
 
I hope this helps!

Hi @Rob,

I’m a bit lost here :) I’m trying to make the example on the communication doc to work first but I fail to do that, too.

Here is how I call C# code from Lua:

local requestingMessageBoxEvent = { name = "requestingMessageBox", message = "Hello World!" } local result = Runtime:dispatchEvent( requestingMessageBoxEvent ) -- Print the message returned by C# print( "result is", tostring(result) ) --always returns FALSE

As I understand, I have to insert C# code into the MainPage.xaml.cs, right? Here is my MainPage.xaml.cs:

* When I try Console.WriteLine(), it doesn’t seem to print anything to the console (when debugging on device) so I’m not pretty sure that I have access to these methods.

private void OnCoronaRuntimeLoaded(object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e) { e.CoronaRuntimeEnvironment.AddEventListener("requestingMessageBox", OnRequestingMessageBox); //e.CoronaRuntimeEnvironment.AddEventListener("requestingMessageBox", OnPlayVideo); } private CoronaLabs.Corona.WinRT.ICoronaBoxedData OnRequestingMessageBox( CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment sender, CoronaLabs.Corona.WinRT.CoronaLuaEventArgs e) { var boxedMessage = e.Properties.Get("message") as CoronaLabs.Corona.WinRT.CoronaBoxedString; if (boxedMessage == null) { return CoronaLabs.Corona.WinRT.CoronaBoxedString.From("'event.message' is a required field."); } return CoronaLabs.Corona.WinRT.CoronaBoxedString.From("Message box was displayed successfully!"); } private CoronaLabs.Corona.WinRT.ICoronaBoxedData OnPlayVideo( CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment sender, CoronaLabs.Corona.WinRT.CoronaLuaEventArgs e) { try { // Create a path to a video file. // This example assumes a video file named "MyVideo.m4v" is in your Resource directory. var uri = new Uri( "ms-appx:///Assets/Corona/videos/Giris.mp4", UriKind.RelativeOrAbsolute); // Play the video. // Note: It's your responsibility to create and store the MediaElement object. // Because you'll need to add the ability to remove it later, outside of this method. mediaElement.Source = uri; mediaElement.Play(); } catch (Exception) { // Failed to load the video file! } return CoronaLabs.Corona.WinRT.CoronaBoxedString.From("Message box was displayed successfully!"); }

The code you posted is missing the part where you add a “Loaded” event handler in your MainPage’s constructor.  As in you need to add the following line to your MainPage.xaml.cs like this…

public MainPage() { // \<Your existing constructor code goes here.\> // Add a Corona event handler which detects when the Corona project has been loaded, but not started yet. fCoronaPanel.Runtime.Loaded += OnCoronaRuntimeLoaded; }

This sets up your OnCoronaRuntimeLoaded() method to be called when the Corona runtime gets loaded.  This is your opportunity to hook up your own custom Lua event handlers before your “main.lua” gets executed.

Well, I removed that part from the post to make the code more clear. It’s already in the code.

I tried adding some Console.WriteLine() to see if it enters the method but no luck with that. I don’t see it calling the method.

By the way, my

mediaElement

variable is a MediaElement component that I added with UI designer of Visual Studio to the MainPage.

I know what’s wrong.

Console.WriteLine() does *not* write to Visual Studio “Output” panel.  It outputs to stdout instead.

You need to call the static System.Diagnostics.Debugger.Log() method instead.

   http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.log(v=vs.110).aspx

It should look something like this…

System.Diagnostics.Debugger.Log(0, "Corona", "Hello World!!!");

Also note that this will write to Visual Studio’s “Output” panel if the .NET debugger is attached (which is the default) and not the Native C/C++ debugger.

You can also just put a breakpoint in event handler, like in your OnRequestingMessageBox() method, to see if it is actually being called.  You can add breakpoints by right clicking the line of code and selecting “Breakpoint” from the popup menu… or by left clicking the grey column to the left of your line of code.  The breakpoint will be displayed as a red circle.

I see. I’ll be trying that out on Monday because I don’t have access to that development PC right now and write about the result here.

Other than that, is my OnPlayVideo C# code right? Also, how can I call it from the Lua side correctly?

Your OnPlayVideo() method will work provided that you’ve created your mediaElement control before hand and it is already attached to your page.  Perhaps what you should do is create it in your MainPage constructor but don’t attach it to your page until a video needs to be played, such as within your OnPlayVideo() method.  You can then display it via…

fCoronaPanel.Children.Add(mediaElement)

And when the video is done playing, you can remove it from the CoronaPanel.