0

I am trying to play video on an AVPlayer and its closing automatically.

Here is the Code when AVPlayer POPS Up

if (asset != null)
            {
                string[] keys = { "playable" , "hasProtectedContent" };

               asset.LoadValuesAsynchronously(keys, () =>
                {
                    DispatchQueue.MainQueue.DispatchAsync(() =>
                    {
                        // Device.BeginInvokeOnMainThread(() => { 
                        if (asset == null) return;

                        playerItem = new AVPlayerItem(asset);
                        player.ReplaceCurrentItemWithPlayerItem(playerItem);
                        if (playerItem != null && Element.AutoPlay)
                            player.Play();
                    });
                });
            }

Here is the Error I am getting only on iOS 13 :

[plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x60000059c5a0> F8BB1C28-BAE8-11D6-9C31-00039315CD46
[] nw_endpoint_flow_copy_multipath_subflow_counts Called on non-Multipath connection
0000000A:  0100  4          4           66
00000016:  0101  4          4           66
00000022:  0102  3          6          110
0000002E:  011A  5          8          116
0000003A:  011B  5          8          124
00000046:  0128  3          2            3
00000052:  0131  2         12          132
0000005E:  0132  2         20          144
000000A6:  0100  4          4          256
000000B2:  0101  4          4          256
000000BE:  0102  3          6          266
000000CA:  0103  3          2            6
000000D6:  0106  3          2            6
000000E2:  0115  3          2            3
000000EE:  0201  4          4          272
000000FA:  0202  4          4         7818
0000000A:  0100  3          2         3840
00000016:  0101  3          2         2400
00000022:  0102  3          6          158
0000002E:  0106  3          2            2
0000003A:  0112  3          2            1

Please let me know what am i missing here.

Here is the code i am using for Video Player Render

    public class VideoPlayerRenderer : ViewRenderer<VideoPlayer, UIView>
    {
        AVPlayer player;
        AVPlayerItem playerItem;
        AVPlayerViewController _playerViewController;       // solely for ViewController property
        AVAsset asset;
        NSObject didPlayToEndObserver;

        public override UIViewController ViewController => _playerViewController;

        protected override void OnElementChanged(ElementChangedEventArgs<VideoPlayer> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                if (Control == null)
                {
                    // Create AVPlayerViewController
                    _playerViewController = new AVPlayerViewController();

                    // Set Player property to AVPlayer
                    player = new AVPlayer();
                    _playerViewController.Player = player;

                    UITapGestureRecognizer uiTapGestureRecognizer = new UITapGestureRecognizer(TapAction);
                    _playerViewController.ContentOverlayView.GestureRecognizers = new UIGestureRecognizer[] { uiTapGestureRecognizer };

                    var x = _playerViewController.View;

                    // Use the View from the controller as the native control
                    SetNativeControl(_playerViewController.View);

                    didPlayToEndObserver = AVPlayerItem.Notifications.ObserveDidPlayToEndTime(AVPlayerItem_DidPlayToEndTimeNotification);
                }

There is some other which i removed as i am unable to Post it over here

Bharat K
  • 71
  • 3
  • 7
  • Are you testing in iOS 13.1? Have a look at [this thread](https://stackoverflow.com/questions/58110827/ios-13-1-crash-in-avaudio-player) may help. It would be better if you can share a sample here which can reproduce this problem. – nevermore Nov 11 '19 at 02:44
  • @JackHua-MSFT : I tested on 13.1 and 13.2. I have the same issue. And i tried modifying my code as mentioned in the thread but still getting same error. – Bharat K Nov 11 '19 at 14:53
  • @JackHua-MSFT : i updated my post with Video Player Code – Bharat K Nov 11 '19 at 15:08
  • @JackHua-MSFT : Thank you. I tried removing element from IF statement and i am still unable to play. Player is closing automatically. I am using PRISM for navigation. Is there anything to do with it ?? – Bharat K Nov 13 '19 at 00:41
  • I tried the code you give me and that's all I can do so far. If you can share us a sample project here which can reproduce this problem, I will check it. I can share you my test project if you need. – nevermore Nov 13 '19 at 01:23
  • @JackHua-MSFT : Updated Nuget Packages and its working now. Thank you for ur help – Bharat K Nov 17 '19 at 06:04

1 Answers1

0

I use your code and it works well on my side, I just removed Element.AutoPlay in the last if-statement because I don't know what Element is. It plays a video successfully after the project start:

[assembly: ExportRenderer(typeof(myVideoPlayer), typeof(VideoPlayerRenderer))]
namespace App48.iOS
{
    public class VideoPlayerRenderer : ViewRenderer<myVideoPlayer, UIView>
    {
        AVPlayer player;
        AVPlayerItem playerItem;
        AVPlayerViewController _playerViewController;       // solely for ViewController property
        AVAsset asset;

        NSError err;

        public override UIViewController ViewController => _playerViewController;

        protected override void OnElementChanged(ElementChangedEventArgs<myVideoPlayer> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                if (Control == null)
                {
                    // Create AVPlayerViewController
                    _playerViewController = new AVPlayerViewController();

                    // Set Player property to AVPlayer
                    player = new AVPlayer();
                    _playerViewController.Player = player;

                    // Use the View from the controller as the native control
                    SetNativeControl(_playerViewController.View);

                    asset = AVAsset.FromUrl(new NSUrl("https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"));
                    if (asset != null)
                    {
                        string[] keys = { "playable" , "hasProtectedContent" };

                        asset.LoadValuesAsynchronously(keys, () =>
                        {
                            DispatchQueue.MainQueue.DispatchAsync(() =>
                            {
                                // Device.BeginInvokeOnMainThread(() => { 
                                if (asset == null) return;

                                Console.WriteLine(asset.StatusOfValue("playable", out err));

                                playerItem = new AVPlayerItem(asset);
                                player.ReplaceCurrentItemWithPlayerItem(playerItem);
                                if (playerItem != null)
                                    player.Play();
                            });
                        });
                    }
                }
            }
        }

    }
}

In xamarin.forms:

<StackLayout>
    <!-- Place new controls here -->
    <app48:myVideoPlayer
       HorizontalOptions="Center"
       VerticalOptions="CenterAndExpand" />
</StackLayout>

And

public class myVideoPlayer : View { 

}

Note: I test on the iOS simulator 13.1.

nevermore
  • 15,432
  • 1
  • 12
  • 30