Search code examples
c#uwpmediaplayerelement

MediaPlayerElement play/pause button is always disabled


I'm working on a new app and I decided to use MediaPlayerElement to play background music and also video, But there's a problem. Also, there's the same issue on the SystemMediaTransportControls sample on Universal Windows Platform samples. In that sample, AreTransportControlsEnabled is set to False, but if you change it to True, You'll get a disabled Play/Pause button and there's no property to enable it! Even I tried to get Buttons in MediaTransportControl by their names and tried to set them Enabled, But clicking once on the play or pause button makes it disabled once again without any action.

enter image description here

Is there any solution? Regards.

EDIT: I also changed these lines to add some more buttons.

<MediaPlayerElement x:Name="mediaPlayerElement" Margin="0,0,10,0" AreTransportControlsEnabled="True" VerticalAlignment="Top">
                        <MediaPlayerElement.TransportControls>
                            <MediaTransportControls IsNextTrackButtonVisible="True" IsPreviousTrackButtonVisible="True"
                                                    IsSkipBackwardButtonVisible="True" IsSkipBackwardEnabled="True"
                                                    IsSkipForwardButtonVisible="True" IsSkipForwardEnabled="True"/>
                        </MediaPlayerElement.TransportControls>
                    </MediaPlayerElement>

Skip buttons are also disabled, Next and Previous buttons are Enabled but no action if you click on them. Same behavior also, On my app happens.


Solution

  • MediaPlayerElement play/pause button is always disabled

    The official code sample was handle the SMTC manually, so the internal play/pause button is always disabled.

    If you want to use internal SMTC , please make a blank sample with above xaml code.

    For example

    <MediaPlayerElement
        x:Name="mediaPlayerElement"
        Margin="0,0,10,0"
        AreTransportControlsEnabled="True"
        Source="ms-appx:///Assets/hello.mp4">
        <MediaPlayerElement.TransportControls>
            <MediaTransportControls
                IsNextTrackButtonVisible="True"
                IsPreviousTrackButtonVisible="True"
                IsSkipBackwardButtonVisible="True"
                IsSkipBackwardEnabled="True"
                IsSkipForwardButtonVisible="True"
                IsSkipForwardEnabled="True" />
        </MediaPlayerElement.TransportControls>
    </MediaPlayerElement>
    

    Update

    private async void Page_Drop(object sender, DragEventArgs e)
    {
        var def = e.GetDeferral();
        var file = (StorageFile)(await e.DataView.GetStorageItemsAsync()).FirstOrDefault();
        EmbeddedPlayer.Source = MediaSource.CreateFromStorageFile(file);
    
        //await AppCore.Instance.Play(new StorageFile[] { file });
        //EmbeddedPlayer.SetMediaPlayer(AppCore.Instance.MediaPlayer);
       
        EmbeddedPlayer.MediaPlayer.Play();
        def.Complete();
    }