Exemple #1
0
        public AudioPlayerViewModel()
        {
            mediaPlayer = CrossMediaManager.Current;

            MediaPlayer.StatusChanged += (sender, e) =>
            {
                RaisePropertyChanged(() => Status);
                RaisePropertyChanged(() => Position);
            };

            MediaPlayer.PlayingChanged += (sender, e) =>
            {
                RaisePropertyChanged(() => Position);
            };

            MediaPlayer.MediaQueue.QueueMediaChanged += (q_sender, q_e) =>
            {
                RaisePropertyChanged(() => PlayingText);
                if (CurrentTrack != null)
                {
                    CurrentTrack.MetadataUpdated += (m_sender, m_e) =>
                    {
                        RaisePropertyChanged(() => PlayingText);
                    };
                }
            };

            PlayPauseCommand    = new MvxAsyncCommand(async() => await PlaybackController.PlayPause());
            SkipPreviousCommand = new MvxAsyncCommand(async() => await PlaybackController.PlayPreviousOrSeekToStart());
            SkipNextCommand     = new MvxAsyncCommand(async() => await PlaybackController.PlayNext());
        }
        public async Task PlayPause_NotPlaying_Pauses(MediaPlayerStatus notPlayingStatus)
        {
            _mediaManagerMock
            .Setup(mediaManager => mediaManager.Play((IMediaFile)null))
            .Returns(Task.FromResult(0));

            MediaManagerStatus = notPlayingStatus;

            var playbackController = new PlaybackController(MediaManager);

            await playbackController.PlayPause();

            _mediaManagerMock
            .Verify(mediaManager => mediaManager.Play((IMediaFile)null), Times.Once);
        }
        public async Task PlayPause_Playing_Pauses()
        {
            _mediaManagerMock
            .Setup(mediaManager => mediaManager.Pause())
            .Returns(Task.FromResult(0));

            MediaManagerStatus = MediaPlayerStatus.Playing;

            var playbackController = new PlaybackController(MediaManager);

            await playbackController.PlayPause();

            _mediaManagerMock
            .Verify(mediaManager => mediaManager.Pause(), Times.Once);
        }
        //public MediaPlayerViewCtr(string mediaUrl)
        //{
        //	MediaUrl = mediaUrl;
        //	_viewModel = new MediaPlayerViewModel();
        //	_viewModel.Init(mediaUrl);
        //}

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            _viewModel.PropertyChanged += (sender, args) =>
            {
                var propertyName = args.PropertyName;
                var allChanged   = propertyName == null;

                Func <string, bool> hasChanged = property => allChanged || propertyName == property;

                InvokeOnMainThread(() =>
                {
                    if (hasChanged(nameof(MediaPlayerViewModel.PlayingText)))
                    {
                        QueueLabel.Text = _viewModel.PlayingText;
                    }

                    if (hasChanged(nameof(MediaPlayerViewModel.Cover)))
                    {
                        TrackCoverImageView.Image = (UIImage)_viewModel.Cover;
                    }

                    if (hasChanged(nameof(MediaPlayerViewModel.CurrentTrack)))
                    {
                        TitleLabel.Text    = _viewModel.CurrentTrack.Metadata.Title;
                        SubtitleLabel.Text = _viewModel.CurrentTrack.Metadata.Artist;
                    }

                    if (hasChanged(nameof(MediaPlayerViewModel.IsPlaying)))
                    {
                        PlayPauseButton.Selected = _viewModel.IsPlaying;
                    }

                    if (hasChanged(nameof(MediaPlayerViewModel.Duration)))
                    {
                        PlayingProgressSlider.MaxValue  = _viewModel.Duration;
                        BufferedProgressSlider.MaxValue = _viewModel.Duration;

                        var duration = TimeSpan.FromSeconds(_viewModel.Duration);

                        TimeTotalLabel.Text = GetTimeString(duration);
                    }

                    if (hasChanged(nameof(MediaPlayerViewModel.Position)))
                    {
                        PlayingProgressSlider.Value = _viewModel.Position;
                        TimePlayedLabel.Text        = GetTimeString(TimeSpan.FromSeconds(_viewModel.Position));
                    }

                    if (hasChanged(nameof(MediaPlayerViewModel.Downloaded)))
                    {
                        BufferedProgressSlider.Value = _viewModel.Downloaded;
                    }
                });
            };

            MediaPlayer.PlayingChanged += (sender, args) =>
            {
                if (!_viewModel.IsSeeking)
                {
                    InvokeOnMainThread(() =>
                    {
                        TimePlayedLabel.Text = GetTimeString(args.Position);
                    });
                }
            };

            MediaPlayer.MediaQueue.PropertyChanged += (sender, e) =>
            {
                InvokeOnMainThread(() =>
                {
                    var propertyName = e.PropertyName;
                    var allChanged   = propertyName == null;

                    Func <string, bool> hasChanged = property => allChanged || propertyName == property;

                    if (hasChanged(nameof(MediaPlayer.MediaQueue.IsShuffled)))
                    {
                        ShuffleButton.Selected = MediaPlayer.MediaQueue.IsShuffled;
                    }

                    if (hasChanged(nameof(MediaPlayer.MediaQueue.Repeat)))
                    {
                        var iconPrefix = "icon_repeat_";
                        var extension  = ".png";

                        string iconState;

                        switch (MediaPlayer.MediaQueue.Repeat)
                        {
                        case RepeatType.None:
                            iconState = "static";
                            break;

                        case RepeatType.RepeatAll:
                            iconState = "active";
                            break;

                        case RepeatType.RepeatOne:
                            iconState = "one_active";
                            break;

                        default:
                            iconState = "static";
                            break;
                        }

                        var imageUrl = iconPrefix + iconState + extension;

                        var image = new UIImage(imageUrl);
                        RepeatButton.SetImage(image, UIControlState.Normal);
                    }
                });
            };

            PlayingProgressSlider.TouchDown += (sender, e) =>
            {
                _viewModel.IsSeeking = true;
            };

            PlayingProgressSlider.TouchUpInside += (sender, e) =>
            {
                _viewModel.IsSeeking = false;
            };

            PlayingProgressSlider.TouchUpOutside += (sender, e) =>
            {
                _viewModel.IsSeeking = false;
            };

            PlayingProgressSlider.ValueChanged += (sender, e) =>
            {
                _viewModel.IsSeeking = true;
                _viewModel.Position  = (int)PlayingProgressSlider.Value;
            };

            PlayingProgressSlider.Continuous = true;

            PlayPauseButton.TouchUpInside += async(sender, e) =>
            {
                await PlaybackController.PlayPause();

                PlayPauseButton.Selected = _viewModel.IsPlaying;
            };

            NextButton.TouchUpInside += async(sender, e) =>
            {
                await PlaybackController.PlayNext();
            };

            PreviousButton.TouchUpInside += async(sender, e) =>
            {
                await PlaybackController.PlayPrevious();
            };

            ShuffleButton.TouchUpInside += (sender, args) =>
            {
                PlaybackController.ToggleShuffle();
            };

            RepeatButton.TouchUpInside += (sender, e) =>
            {
                PlaybackController.ToggleRepeat();
            };

            _viewModel.Init();

            var sampleFilePath = NSBundle.MainBundle.PathForResource("local-sample", "mp4");

            _viewModel.Queue.Add(new MediaFile
            {
                Type         = MediaFileType.Video,
                Url          = sampleFilePath,
                Availability = ResourceAvailability.Local
            });
        }