Exemple #1
0
        public async Task Play(IMediaFile mediaFile = null)
        {
            var sameMediaFile = mediaFile == null || mediaFile.Equals(_currentMediaFile);

            if (Status == MediaPlayerStatus.Paused && sameMediaFile)
            {
                Status = MediaPlayerStatus.Playing;
                //We are simply paused so just start again
                Player.Play();
                return;
            }

            if (mediaFile != null)
            {
                nsUrl             = MediaFileUrlHelper.GetUrlFor(mediaFile);
                _currentMediaFile = mediaFile;
            }

            try
            {
                InitializePlayer();

                Status = MediaPlayerStatus.Buffering;

                var playerItem = GetPlayerItem(nsUrl);

                CurrentItem?.RemoveObserver(this, new NSString("status"));

                Player.ReplaceCurrentItemWithPlayerItem(playerItem);
                CurrentItem.AddObserver(this, new NSString("loadedTimeRanges"),
                                        NSKeyValueObservingOptions.Initial | NSKeyValueObservingOptions.New,
                                        LoadedTimeRangesObservationContext.Handle);

                CurrentItem.SeekingWaitsForVideoCompositionRendering = true;
                CurrentItem.AddObserver(this, (NSString)"status", NSKeyValueObservingOptions.New |
                                        NSKeyValueObservingOptions.Initial,
                                        StatusObservationContext.Handle);


                NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.DidPlayToEndTimeNotification,
                                                               notification => MediaFinished?.Invoke(this, new MediaFinishedEventArgs(mediaFile)), CurrentItem);

                Player.Play();
            }
            catch (Exception ex)
            {
                OnMediaFailed();
                Status = MediaPlayerStatus.Stopped;

                //unable to start playback log error
                Console.WriteLine("Unable to start playback: " + ex);
            }

            await Task.CompletedTask;
        }
        public async Task Play(IMediaFile mediaFile = null)
        {
            if (mediaFile != null)
            {
                nsUrl = new NSUrl(mediaFile.Url);
            }

            if (Status == MediaPlayerStatus.Paused)
            {
                Status = MediaPlayerStatus.Playing;
                //We are simply paused so just start again
                Player.Play();
                return;
            }

            try
            {
                // Start off with the status LOADING.
                Status = MediaPlayerStatus.Buffering;

                var options = MediaFileUrlHelper.GetOptionsWithHeaders(RequestHeaders);

                var nsAsset       = AVUrlAsset.Create(nsUrl, options);
                var streamingItem = AVPlayerItem.FromAsset(nsAsset);

                Player.CurrentItem?.RemoveObserver(this, new NSString("status"));

                Player.ReplaceCurrentItemWithPlayerItem(streamingItem);
                streamingItem.AddObserver(this, new NSString("status"), NSKeyValueObservingOptions.New, Player.Handle);
                streamingItem.AddObserver(this, new NSString("loadedTimeRanges"),
                                          NSKeyValueObservingOptions.Initial | NSKeyValueObservingOptions.New, Player.Handle);

                Player.CurrentItem.SeekingWaitsForVideoCompositionRendering = true;
                Player.CurrentItem.AddObserver(this, (NSString)"status", NSKeyValueObservingOptions.New |
                                               NSKeyValueObservingOptions.Initial,
                                               StatusObservationContext.Handle);

                NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.DidPlayToEndTimeNotification,
                                                               notification => MediaFinished?.Invoke(this, new MediaFinishedEventArgs(mediaFile)), Player.CurrentItem);

                Player.Play();
            }
            catch (Exception ex)
            {
                OnMediaFailed();
                Status = MediaPlayerStatus.Stopped;

                //unable to start playback log error
                Console.WriteLine("Unable to start playback: " + ex);
            }

            await Task.CompletedTask;
        }
        public async Task <IMediaFile> ExtractMediaInfo(IMediaFile mediaFile)
        {
            try
            {
                if (mediaFile.ExtractMetadata)
                {
                    var assetsToLoad = new List <string>
                    {
                        AVMetadata.CommonKeyArtist,
                        AVMetadata.CommonKeyTitle,
                        AVMetadata.CommonKeyArtwork
                    };

                    var url = MediaFileUrlHelper.GetUrlFor(mediaFile);

                    // Default title to filename
                    mediaFile.Metadata.Title = url.LastPathComponent;

                    var asset = AVAsset.FromUrl(url);
                    await asset.LoadValuesTaskAsync(assetsToLoad.ToArray());

                    foreach (var avMetadataItem in asset.CommonMetadata)
                    {
                        if (avMetadataItem.CommonKey == AVMetadata.CommonKeyArtist)
                        {
                            mediaFile.Metadata.Artist = ((NSString)avMetadataItem.Value).ToString();
                        }
                        else if (avMetadataItem.CommonKey == AVMetadata.CommonKeyTitle)
                        {
                            mediaFile.Metadata.Title = ((NSString)avMetadataItem.Value).ToString();
                        }
                        else if (avMetadataItem.CommonKey == AVMetadata.CommonKeyArtwork)
                        {
#if __IOS__ || __TVOS__
                            var image = UIImage.LoadFromData(avMetadataItem.DataValue);
                            mediaFile.Metadata.AlbumArt = image;
#endif
                        }
                    }
                    mediaFile.MetadataExtracted = true;
                }
                return(mediaFile);
            }
            catch (Exception)
            {
                return(mediaFile);
            }
        }
        private AVPlayerItem GetPlayerItem(NSUrl url)
        {
            AVAsset asset;

            if (RequestHeaders != null && RequestHeaders.Any())
            {
                var options = MediaFileUrlHelper.GetOptionsWithHeaders(RequestHeaders);

                asset = AVUrlAsset.Create(url, options);
            }
            else
            {
                asset = AVAsset.FromUrl(url);
            }

            var playerItem = AVPlayerItem.FromAsset(asset);

            return(playerItem);
        }