Esempio n. 1
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            if (string.IsNullOrEmpty(FilePath) || !File.Exists(FilePath))
            {
                NavigationController.PopViewController(true);
            }

            string taskType = Task.TaskType.IdName;

            if (taskType == "LISTEN_AUDIO" || taskType == "REC_AUDIO" || taskType == "TAKE_VIDEO")
            {
                // Load video, play and loop
                playerItem   = AVPlayerItem.FromUrl(NSUrl.FromFilename(FilePath));
                player       = AVQueuePlayer.FromItems(new AVPlayerItem[] { playerItem });
                playerLayer  = AVPlayerLayer.FromPlayer(player);
                playerLooper = AVPlayerLooper.FromPlayer(player, playerItem);

                if (taskType == "TAKE_VIDEO")
                {
                    playerLayer.Frame = View.Frame;
                    View.Layer.AddSublayer(playerLayer);
                }
                else
                {
                    ImageView.Hidden = true;
                    AudioIcon.Hidden = false;
                    DescLabel.Text   = Task.Description;
                }

                player.Play();
            }
            else
            {
                // Load image
                ImageService.Instance.LoadFile(FilePath).Error((e) =>
                {
                    Console.WriteLine("ERROR LOADING IMAGE: " + e.Message);
                    NavigationController.PopViewController(true);
                }).Into(ImageView);
            }

            if (DeleteResult != null)
            {
                UIBarButtonItem customButton = new UIBarButtonItem(
                    UIImage.FromFile("ic_delete"),
                    UIBarButtonItemStyle.Plain,
                    (s, e) =>
                {
                    ConfirmDelete();
                }
                    );

                NavigationItem.RightBarButtonItem = customButton;
            }
        }
        public void TestLoopingEnabled()
        {
            string file = Path.Combine(NSBundle.MainBundle.ResourcePath, "Hand.wav");

            Assert.True(File.Exists(file), file);
            using (var url = new NSUrl(file))
                using (var playerItem = AVPlayerItem.FromUrl(url))
                    using (AVQueuePlayer player = AVQueuePlayer.FromItems(new[] { playerItem }))
                        using (var playerLooper = AVPlayerLooper.FromPlayer(player, playerItem)) {
                            Assert.True(playerLooper.LoopingEnabled, "The default value should be true.");
                            playerLooper.DisableLooping();
                            Assert.False(playerLooper.LoopingEnabled, "Looping enabled should return false after 'DisableLooping'");
                        }
        }
        partial void Play(NSObject sender)
        {
            // An AVPlayerLayer has already been created for this asset; just play it.
            if (playerLayer != null)
            {
                playerLayer.Player.Play();
            }
            else
            {
                var options = new PHVideoRequestOptions
                {
                    NetworkAccessAllowed = true,
                    DeliveryMode         = PHVideoRequestOptionsDeliveryMode.Automatic,
                    ProgressHandler      = (double progress, NSError error, out bool stop, NSDictionary info) =>
                    {
                        stop = false;

                        // Handler might not be called on the main queue, so re-dispatch for UI work.
                        DispatchQueue.MainQueue.DispatchSync(() =>
                        {
                            ProgressView.Progress = (float)progress;
                        });
                    }
                };

                // Request an AVAsset for the displayed PHAsset and set up a layer for playing it.
                PHImageManager.DefaultManager.RequestPlayerItem(Asset, options, (playerItem, info) =>
                {
                    DispatchQueue.MainQueue.DispatchSync(() =>
                    {
                        if (this.playerLayer != null || playerItem == null)
                        {
                            return;
                        }

                        // Create an AVPlayer and AVPlayerLayer with the AVPlayerItem.
                        AVPlayer player;

                        if (Asset.PlaybackStyle == PHAssetPlaybackStyle.VideoLooping)
                        {
                            var queuePlayer = AVQueuePlayer.FromItems(new[] { playerItem });
                            playerLooper    = AVPlayerLooper.FromPlayer(queuePlayer, playerItem);
                            player          = queuePlayer;
                        }
                        else
                        {
                            player = AVPlayer.FromPlayerItem(playerItem);
                        }

                        var playerLayer = AVPlayerLayer.FromPlayer(player);

                        // Configure the AVPlayerLayer and add it to the view.
                        playerLayer.VideoGravity = AVLayerVideoGravity.ResizeAspect;
                        playerLayer.Frame        = View.Layer.Bounds;
                        View.Layer.AddSublayer(playerLayer);

                        player.Play();

                        // Refer to the player layer so we can remove it later.
                        this.playerLayer = playerLayer;
                    });
                });
            }
        }