Using Windows MediaElement to play local Video

I’m integrating a Video (MP4) in to the game I am currently working on, and while it was nice not having to write it nativly for iOS or Android…Mac and Windows/WP have been a pain. I have it working for Mac, so if any one needs help in that area, let me know…but has any one been able to get a video to play on Windows 8.1 universal? There is very little documentation about getting this working in C++, but what I have found is this (still doesn’t work):

const std::string &url = "ms-appx:///Assets/temp_halloween_1.mp4";

 auto dispatcher = cocos2d::GLViewImpl::sharedOpenGLView()->getDispatcher();
 dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new DispatchedHandler([url]() {
	 auto VideoSource = ref new Windows::UI::Xaml::Controls::MediaElement();
	 auto uri = ref new Windows::Foundation::Uri("ms-appx:///Assets/temp_halloween_1.mp4");//
	 VideoSource->Source = uri;
	
		 VideoSource->AutoPlay = true;
	 VideoSource->IsFullWindow = true;
	 VideoSource->Name = "Intro";
	 VideoSource->IsMuted = false;
	 VideoSource->Width = 1920;
	 VideoSource->Height = 1080;
	 VideoSource->Volume = 100;
	 
	 VideoSource->Play();
	 
	 log("%f", VideoSource->Position);

	 
 }));

Then I’ve also tried this, with no luck:

const std::string &url = "ms-appx:///Assets/temp_halloween_1.mp4";

 auto dispatcher = cocos2d::GLViewImpl::sharedOpenGLView()->getDispatcher();
 dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new DispatchedHandler([url]() {

	 auto uri = ref new Windows::Foundation::Uri("ms-appx:///Assets/temp_halloween_1.mp4");

	 auto task = Concurrency::create_task(StorageFile::GetFileFromApplicationUriAsync(uri));

	 task.then([](concurrency::task<StorageFile^> fileTask) {

		 StorageFile^ file;

		 file = fileTask.get(); // this is what actually throws if Uri is wrong
		 //concurrency::task<IRandomAccessStream>(file->OpenAsync(FileAccessMode::Read));

		 create_task(file->OpenAsync(FileAccessMode::Read)).then(
			 [&](IRandomAccessStream^ stream)
		 {
			 if (stream)
			 {
				 auto VideoSource = ref new Windows::UI::Xaml::Controls::MediaElement();

				 VideoSource->SetSource(stream, file->ContentType);
				 VideoSource->AutoPlay = true;
				 VideoSource->IsFullWindow = true;
				 VideoSource->Name = "Intro";
				 VideoSource->IsMuted = false;



				 VideoSource->Play();
			 }
		 });
	 });
 }));

Can any one point me in the right direction? I do not get any crashes or errors, my vid just doesn’t play…at all!

Also forgot to mention that I am running this on cocos version 3.6!

I’ve hit a major road block on this, and with our deadline fast approaching I am offering compensation for whoever can help me out on this! See my thread in paid gigs here!

In the future if any one has this issue here is the code that worked for me:

 const std::string &url = "ms-appx:///temp.mp4";

auto dispatcher = cocos2d::GLViewImpl::sharedOpenGLView()->getDispatcher();
m_panel = cocos2d::GLViewImpl::sharedOpenGLView()->getPanel();

if(dispatcher->GetType() && m_panel->GetType()){
	
dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, url]() {

		auto mediaElement = ref new Windows::UI::Xaml::Controls::MediaElement();
		mediaElement->Width = 200;

		Windows::Foundation::Uri^ uri = ref new Windows::Foundation::Uri("ms-appx:///temp.mp4");
		mediaElement->Source = uri;

		mediaElement->AutoPlay = true;
		mediaElement->IsFullWindow = true;
		mediaElement->Name = "Intro";
		mediaElement->IsMuted = false;
		m_panel.Get()->Children->Append(mediaElement);


		log("play");
	 
	}));
 }

Also add you video to you assets for both Windows and Windows phone.

Hope this helps someone in the future!
:smile:

2 Likes

Also if any one needs the declarations in the Header, this might help:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)

using namespace Windows::UI::Core;
using namespace Windows::UI::ViewManagement;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::Foundation;
using namespace Windows::Storage;
using namespace Windows::Storage::Streams;
using namespace Windows::Storage::Pickers;
using namespace concurrency;
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
Windows::UI::Xaml::Controls::MediaElement^ mediaElement;
Windows::Storage::StorageFile^ localFile;
Platform::AgileWindows::UI::Xaml::Controls::Panel m_panel;
void setPanel(Windows::UI::Xaml::Controls::Panel^ panel);
Windows::UI::Xaml::Controls::Panel^ getPanel() { return m_panel.Get(); }

void setDispatcher(Windows::UI::Core::CoreDispatcher^ dispatcher);
Windows::UI::Core::CoreDispatcher^ getDispatcher() { return m_dispatcher.Get(); }
Platform::Agile<Windows::UI::Core::CoreDispatcher> m_dispatcher;
void checkState(float dt);

#endif

2 Likes

Hi!

Thank you for posting solution here, i’m still new here and wondered in order for this to work: what did you #include ?