在Play()调用

时间:2019-04-18 11:09:38

标签: c# xamarin.forms

我正在使用Plugin.MediaManager播放URL中的音频。我使用CrossMediaManager.Current.Status播放/暂停音频。

代码:

if(CrossMediaManager.Current.Status == MediaPlayerStatus.Stopped)
{
    await CrossMediaManager.Current.Play(url);
}
else if(CrossMediaManager.Current.Status == MediaPlayerStatus.Playing) // this never called because status is always stopped.
{
    await CrossMediaManager.Current.Pause();
}
else if(CrossMediaManager.Current.Status == MediaPlayerStatus.Paused)
{
    await CrossMediaManager.Current.PlaybackController.Pause();
}

为什么这里的状态没有改变? 请帮助。

谢谢

1 个答案:

答案 0 :(得分:0)

很难准确地知道出了什么问题,而又看不到Plugin.MediaManager的完整实现。

下面是Plugin.MediaManager的有效实现,它使用了Azure Media Player demo page中的视频片段。

完整的示例应用程序可以在这里找到:https://github.com/brminnick/AzureMediaServicesSampleApp

public class NativeVideoPlayerPage : ContentPage
{
    public NativeVideoPlayerPage()
    {
        CrossMediaManager.Current.RequestHeaders.Add("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1cm46bWljcm9zb2Z0OmF6dXJlOm1lZGlhc2VydmljZXM6Y29udGVudGtleWlkZW50aWZpZXIiOiI5ZGRhMGJjYy01NmZiLTQxNDMtOWQzMi0zYWI5Y2M2ZWE4MGIiLCJpc3MiOiJodHRwOi8vdGVzdGFjcy5jb20vIiwiYXVkIjoidXJuOnRlc3QiLCJleHAiOjE3MTA4MDczODl9.lJXm5hmkp5ArRIAHqVJGefW2bcTzd91iZphoKDwa6w8");

        const int horizontalButtonPadding = 5;
        const int verticalButtonPadding = 10;

        var videoView = new VideoView
        {
            Source = @"https://amssamples.streaming.mediaservices.windows.net/830584f8-f0c8-4e41-968b-6538b9380aa5/TearsOfSteelTeaser.ism/manifest(format=m3u8-aapl)";
            AspectMode = Plugin.MediaManager.Abstractions.Enums.VideoAspectMode.AspectFit
        };

        var playButton = new Button { Text = "Play" };
        playButton.Clicked += HandlePlayButtonClicked;

        var pauseButton = new Button { Text = "Pause" };
        pauseButton.Clicked += HandlePauseButtonClicked;

        var stopButton = new Button { Text = "Stop" };
        stopButton.Clicked += HandleStopButtonClicked;

        var relativeLayout = new RelativeLayout();
        relativeLayout.Children.Add(videoView,
                                    Constraint.Constant(0),
                                    Constraint.Constant(0),
                                    Constraint.RelativeToParent(parent => parent.Width),
                                    Constraint.RelativeToParent(parent => parent.Height));
        relativeLayout.Children.Add(playButton,
                                    Constraint.Constant(horizontalButtonPadding),
                                    Constraint.RelativeToParent(parent => parent.Height - verticalButtonPadding - getPlayButtonHeight(parent)),
                                    Constraint.RelativeToParent(parent => (parent.Width - 4 * horizontalButtonPadding) / 3));
        relativeLayout.Children.Add(pauseButton,
                                    Constraint.RelativeToView(playButton, (parent, view) => view.X + view.Width + horizontalButtonPadding),
                                    Constraint.RelativeToParent(parent => parent.Height - verticalButtonPadding - getPauseButtonHeight(parent)),
                                    Constraint.RelativeToParent(parent => (parent.Width - 4 * horizontalButtonPadding) / 3));
        relativeLayout.Children.Add(stopButton,
                                    Constraint.RelativeToView(pauseButton, (parent, view) => view.X + view.Width + horizontalButtonPadding),
                                    Constraint.RelativeToParent(parent => parent.Height - verticalButtonPadding - getStopButtonHeight(parent)),
                                    Constraint.RelativeToParent(parent => (parent.Width - 4 * horizontalButtonPadding) / 3));

        Title = "Native Video Player";

        Content = relativeLayout;

        double getPlayButtonHeight(RelativeLayout p) => playButton.Measure(p.Width, p.Height).Request.Height;
        double getPauseButtonHeight(RelativeLayout p) => pauseButton.Measure(p.Width, p.Height).Request.Height;
        double getStopButtonHeight(RelativeLayout p) => stopButton.Measure(p.Width, p.Height).Request.Height;
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();

        CrossMediaManager.Current.Pause();
    }

    void HandlePlayButtonClicked(object sender, EventArgs e) => CrossMediaManager.Current.Play();
    void HandleStopButtonClicked(object sender, EventArgs e) => CrossMediaManager.Current.Stop();
    void HandlePauseButtonClicked(object sender, EventArgs e) => CrossMediaManager.Current.Pause();
}