Cast video to TV using MediaPlayerElement

空扰寡人 提交于 2020-01-17 07:59:14

问题


I have the following XAML

<MediaPlayerElement x:Name="EmbeddedPlayer" AreTransportControlsEnabled="True" HorizontalAlignment="Stretch" IsDoubleTapEnabled="True" DoubleTapped="OnEmbeddedPlayerDoubleTapped"> </MediaPlayerElement>

According to this official documentation, I should be able to use the available Cast button to cast the video to my TV. The Movies & TV app can does that: When I clicked the cast button in that app, it lists the available targets. But when I do the same thing for my app, it asks me to make sure that the devices are discoverable and no progress ring indicating device searching/discovery was going on. (I am on a Lumia 635.) Once again, I feel the frustration of mismatching between documentation and reality!

Is there a complete working example for video/audio casting?

EDIT: I added the simplified code following the third method for device discovery given in the article:

using namespace Windows::Devices::Enumeration;

MainPage::MainPage()
{
     // Other set up
     DeviceWatcher ^deviceWatcher;
     CastingConnection ^castingConnection;
     //Create our watcher and have it find casting devices capable of video casting
     deviceWatcher = DeviceInformation::CreateWatcher(CastingDevice::GetDeviceSelector(CastingPlaybackTypes::Video));

    //Register for watcher events
    deviceWatcher->Added += ref new TypedEventHandler<DeviceWatcher^, DeviceInformation^>(this, &MainPage::DeviceWatcher_Added);

    deviceWatcher->Start();
}

void MainPage::DeviceWatcher_Added(Windows::Devices::Enumeration::DeviceWatcher^ sender, Windows::Devices::Enumeration::DeviceInformation^ args)
{
    Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new DispatchedHandler([args]()
    {
        //Add each discovered device to our listbox
    create_task(CastingDevice::FromIdAsync(args->Id)).then([](CastingDevice^ addedDevice)
    {
        OutputDebugString(("Found cast device " + addedDevice->FriendlyName + "\n")->Data());
    }, task_continuation_context::use_current());
    //castingDevicesListBox.Items.Add(addedDevice);
    }));
}

As I anticipated, there is no device discovered. There might probably be some extra steps to take care of permission (allow app to discover & cast to devices) etc. that are never specified in the documentation.


回答1:


Contrary to the documentation, one MUST NOT use MediaPlayerElement but the deprecated MediaElement for casting on mobile. This hint is taken from https://social.msdn.microsoft.com/Forums/en-US/0c37a74f-1331-4fb8-bfdf-3df11d953098/uwp-mediaplayerelement-mediacasting-is-broken-?forum=wpdevelop

This also solves a problem I previously asked about showing video in fullscreen: Fullscreen works like a charm for MediaElement on mobile; but not the supposely upgraded MediaPlayerElement.

I should have realized this obvious fact given that Microsoft already abandoned Windows 10 Mobile.



来源:https://stackoverflow.com/questions/44227789/cast-video-to-tv-using-mediaplayerelement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!