Exemple #1
0
        public static IMediaSource ToMediaSource(this IMediaItem mediaItem)
        {
            if (MediaPlayer.DataSourceFactory == null)
            {
                throw new ArgumentNullException(nameof(MediaPlayer.DataSourceFactory));
            }

            IMediaSource mediaSource;

            switch (mediaItem.MediaType)
            {
            default:
            case MediaType.Default:
                mediaSource = new ExtractorMediaSource.Factory(MediaPlayer.DataSourceFactory)
                              .SetTag(mediaItem.ToMediaDescription())
                              .CreateMediaSource(global::Android.Net.Uri.Parse(mediaItem.MediaUri));
                break;

            case MediaType.Dash:
                if (MediaPlayer.DashChunkSourceFactory == null)
                {
                    throw new ArgumentNullException(nameof(MediaPlayer.DashChunkSourceFactory));
                }

                mediaSource = new DashMediaSource.Factory(MediaPlayer.DashChunkSourceFactory, MediaPlayer.DataSourceFactory)
                              .SetTag(mediaItem.ToMediaDescription())
                              .CreateMediaSource(global::Android.Net.Uri.Parse(mediaItem.MediaUri));
                break;

            case MediaType.Hls:
                mediaSource = new HlsMediaSource.Factory(MediaPlayer.DataSourceFactory)
                              .SetAllowChunklessPreparation(true)
                              .SetTag(mediaItem.ToMediaDescription())
                              .CreateMediaSource(global::Android.Net.Uri.Parse(mediaItem.MediaUri));
                break;

            case MediaType.SmoothStreaming:
                if (MediaPlayer.SsChunkSourceFactory == null)
                {
                    throw new ArgumentNullException(nameof(MediaPlayer.SsChunkSourceFactory));
                }

                mediaSource = new SsMediaSource.Factory(MediaPlayer.SsChunkSourceFactory, MediaPlayer.DataSourceFactory)
                              .SetTag(mediaItem.ToMediaDescription())
                              .CreateMediaSource(global::Android.Net.Uri.Parse(mediaItem.MediaUri));
                break;
            }

            return(mediaSource);
        }
        private IMediaSource GetMediaSourceFromUrl(Uri uri, string extension, string tag)
        {
            var BandwidthMeter = DefaultBandwidthMeter.GetSingletonInstance(ActivityContext);
            //DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(ActivityContext, Util.GetUserAgent(ActivityContext, AppSettings.ApplicationName), mBandwidthMeter);
            var buildHttpDataSourceFactory     = new DefaultDataSourceFactory(ActivityContext, BandwidthMeter, new DefaultHttpDataSourceFactory(Util.GetUserAgent(ActivityContext, AppSettings.ApplicationName)));
            var buildHttpDataSourceFactoryNull = new DefaultDataSourceFactory(ActivityContext, BandwidthMeter, new DefaultHttpDataSourceFactory(Util.GetUserAgent(ActivityContext, AppSettings.ApplicationName)));
            int type = Util.InferContentType(uri, extension);

            try
            {
                IMediaSource src;
                switch (type)
                {
                case C.TypeSs:
                    src = new SsMediaSource.Factory(new DefaultSsChunkSource.Factory(buildHttpDataSourceFactory), buildHttpDataSourceFactoryNull).SetTag(tag).SetDrmSessionManager(IDrmSessionManager.DummyDrmSessionManager).CreateMediaSource(uri);
                    break;

                case C.TypeDash:
                    src = new DashMediaSource.Factory(new DefaultDashChunkSource.Factory(buildHttpDataSourceFactory), buildHttpDataSourceFactoryNull).SetTag(tag).SetDrmSessionManager(IDrmSessionManager.DummyDrmSessionManager).CreateMediaSource(uri);
                    break;

                case C.TypeHls:
                    DefaultHlsExtractorFactory defaultHlsExtractorFactory = new DefaultHlsExtractorFactory(DefaultTsPayloadReaderFactory.FlagAllowNonIdrKeyframes, true);
                    src = new HlsMediaSource.Factory(buildHttpDataSourceFactory).SetTag(tag).SetExtractorFactory(defaultHlsExtractorFactory).CreateMediaSource(uri);
                    break;

                case C.TypeOther:
                default:
                    src = new ProgressiveMediaSource.Factory(buildHttpDataSourceFactory).SetTag(tag).CreateMediaSource(uri);
                    break;
                }

                return(src);
            }
            catch (Exception e)
            {
                Methods.DisplayReportResultTrack(e);
                try
                {
                    return(new ProgressiveMediaSource.Factory(buildHttpDataSourceFactory).SetTag(tag).CreateMediaSource(uri));
                }
                catch (Exception exception)
                {
                    Methods.DisplayReportResultTrack(exception);
                    return(null !);
                }
            }
        }
        private void Play()
        {
            if (!string.IsNullOrEmpty(url))
            {
                Uri uri   = Uri.Parse(url);
                var agent = Com.Google.Android.Exoplayer2.Util.Util.GetUserAgent(Context, Context.ApplicationInfo.Name);
                DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory(agent);
                DefaultSsChunkSource.Factory ssChunkFactory        = new DefaultSsChunkSource.Factory(httpDataSourceFactory);
                Handler emptyHandler = new Handler();

                //SsMediaSource ssMediaSource = new SsMediaSource(uri, httpDataSourceFactory, ssChunkFactory, emptyHandler);
                var mediaSource = new HlsMediaSource.Factory(httpDataSourceFactory).CreateMediaSource(uri);
                _player.Prepare(mediaSource);
                var playing = _player.IsPlaying;
            }
        }
Exemple #4
0
        /// <summary>
        /// Plays the video contained in the URL at the given starting position.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="position">The position.</param>
        private void PlayVideo(string url, long?position)
        {
            LoadingSpinnerView.Visibility = ViewStates.Visible;
            LoadingSpinnerView.Start();

            //
            // Prepare the standard HTTP information for the source.
            //
            var          mediaUri  = global::Android.Net.Uri.Parse(url);
            var          userAgent = Util.GetUserAgent(Activity, "Crex");
            var          defaultHttpDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent);
            var          defaultDataSourceFactory     = new DefaultDataSourceFactory(Activity, null, defaultHttpDataSourceFactory);
            IMediaSource source;

            //
            // Determine if this is an HLS or MP4 stream.
            //
            if (mediaUri.Path.EndsWith(".m3u8", StringComparison.InvariantCultureIgnoreCase) || mediaUri.Path.EndsWith("/hsl", StringComparison.InvariantCultureIgnoreCase))
            {
                using (var factory = new HlsMediaSource.Factory(defaultDataSourceFactory))
                {
                    source = factory.CreateMediaSource(mediaUri);
                }
            }
            else
            {
                using (var factory = new ExtractorMediaSource.Factory(defaultDataSourceFactory))
                {
                    source = factory.CreateMediaSource(mediaUri);
                }
            }

            //
            // Create the player and get it ready for playback.
            //
            AutoPlay    = true;
            VideoPlayer = ExoPlayerFactory.NewSimpleInstance(Activity, new DefaultTrackSelector());
            VideoPlayer.AddListener(this);
            PlayerView.Player = VideoPlayer;

            if (position.HasValue)
            {
                VideoPlayer.SeekTo(position.Value);
            }

            VideoPlayer.Prepare(source, !position.HasValue, false);
        }
Exemple #5
0
        void SetSource()
        {
            DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory("1");
            IMediaSource MediaSource = null;
            string       uri         = Element.Source;
            bool         autoplay    = Element.AutoPlay;
            bool         loop        = Element.Loop;

            if (!string.IsNullOrWhiteSpace(uri))
            {
                MediaSource = new HlsMediaSource.Factory(httpDataSourceFactory)
                              .CreateMediaSource(Android.Net.Uri.Parse(uri));
            }

            if (MediaSource != null)
            {
                ExoPlayer.Prepare(MediaSource);
                ExoPlayer.PlayWhenReady = autoplay;
            }
        }
Exemple #6
0
        private IMediaSource GetMediaSourceFromUrl(Uri uri, string tag)
        {
            try
            {
                var mBandwidthMeter = new DefaultBandwidthMeter();
                //DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(ActivityContext, Util.GetUserAgent(ActivityContext, AppSettings.ApplicationName), mBandwidthMeter);
                var          buildHttpDataSourceFactory     = new DefaultDataSourceFactory(ActivityContext, mBandwidthMeter, new DefaultHttpDataSourceFactory(Util.GetUserAgent(ActivityContext, AppSettings.ApplicationName), new DefaultBandwidthMeter()));
                var          buildHttpDataSourceFactoryNull = new DefaultDataSourceFactory(ActivityContext, mBandwidthMeter, new DefaultHttpDataSourceFactory(Util.GetUserAgent(ActivityContext, AppSettings.ApplicationName), null));
                int          type = Util.InferContentType(uri, null);
                IMediaSource src;
                switch (type)
                {
                case C.TypeSs:
                    src = new SsMediaSource.Factory(new DefaultSsChunkSource.Factory(buildHttpDataSourceFactory), buildHttpDataSourceFactoryNull).SetTag(tag).CreateMediaSource(uri);
                    break;

                case C.TypeDash:
                    src = new DashMediaSource.Factory(new DefaultDashChunkSource.Factory(buildHttpDataSourceFactory), buildHttpDataSourceFactoryNull).SetTag(tag).CreateMediaSource(uri);
                    break;

                case C.TypeHls:
                    src = new HlsMediaSource.Factory(buildHttpDataSourceFactory).SetTag(tag).CreateMediaSource(uri);
                    break;

                case C.TypeOther:
                    src = new ExtractorMediaSource.Factory(buildHttpDataSourceFactory).SetTag(tag).CreateMediaSource(uri);
                    break;

                default:
                    //src = Exception("Unsupported type: " + type);
                    src = new ExtractorMediaSource.Factory(buildHttpDataSourceFactory).SetTag(tag).CreateMediaSource(uri);
                    break;
                }
                return(src);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(null);
            }
        }
Exemple #7
0
        public void Init(Context context, Com.Google.Android.Exoplayer2.UI.PlayerView playerView)
        {
            // Create a default track selector.
            var bandwidthMeter             = new DefaultBandwidthMeter();
            var videoTrackSelectionFactory =
                new AdaptiveTrackSelection.Factory(bandwidthMeter);
            TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

            // Create a player instance.
            player = ExoPlayerFactory.NewSimpleInstance(context, trackSelector);

            // Bind the player to the view.
            playerView.Player = (player);

            // This is the MediaSource representing the content media (i.e. not the ad).
            var contentUrl = url;            //context.GetString (Resource.String.test_content_url);

            HlsMediaSource.Factory hlsfac = new HlsMediaSource.Factory(mediaDataSourceFactory);
            //var hlsMediaSource = hlsfac.CreateMediaSource (Uri.Parse(contentUrl));
            var hlsMediaSource = new HlsMediaSource(Uri.Parse(contentUrl), mediaDataSourceFactory, null, null);
            //var contentMediaSource =
            //BuildMediaSource (Uri.Parse (contentUrl),"");

            // Compose the content media source into a new AdsMediaSource with both ads and content.
            var mediaSourceWithAds = hlsMediaSource;

            //        new AdsMediaSource(
            //                hlsMediaSource,
            //            /* adMediaSourceFactory= */ this,
            //             adsLoader,
            //            playerView.getOverlayFrameLayout(),
            //            /* eventHandler= */ null,
            //            /* eventListener= */ null);

            // Prepare the player with the source.
            player.SeekTo(contentPosition);
            player.Prepare(hlsMediaSource);
            player.PlayWhenReady = (true);
        }
Exemple #8
0
        private IMediaSource BuildMediaSource(android.Net.Uri uri, string overrideExtension)
        {
            int type = Utils.InferContentType(uri, overrideExtension);

            IMediaSource src = null;

            switch (type)
            {
            case C.TypeDash:
                src = new DashMediaSource.Factory(new DefaultDashChunkSource.Factory(mediaDataSourceFactory), BuildDataSourceFactory(false))
                      .SetManifestParser(new FilteringManifestParser(new DashManifestParser(), GetOfflineStreamKeys(uri)))
                      .CreateMediaSource(uri);
                break;

            case C.TypeSs:
                src = new SsMediaSource.Factory(new DefaultSsChunkSource.Factory(mediaDataSourceFactory), BuildDataSourceFactory(false))
                      .SetManifestParser(new FilteringManifestParser(new SsManifestParser(), GetOfflineStreamKeys(uri)))
                      .CreateMediaSource(uri);
                break;

            case C.TypeHls:
                src = new HlsMediaSource.Factory(mediaDataSourceFactory)
                      .SetPlaylistParserFactory(new FilteringManifestParser(new HlsPlaylistParser(), GetOfflineStreamKeys(uri)))
                      .CreateMediaSource(uri);
                break;

            case C.TypeOther:
                src = new ExtractorMediaSource.Factory(mediaDataSourceFactory).CreateMediaSource(uri);
                break;

            default:
                throw new IllegalStateException("Unsupported type: " + type);
            }

            //Todo: implement IAnalyticsListener
            src.AddEventListener(mainHandler, eventLogger);
            return(src);
        }
        private IMediaSource CreateMediaSource(StreamingUri link)
        {
            Android.Net.Uri uri    = Android.Net.Uri.Parse(link.StreamingUrl.OriginalString);
            IMediaSource    source = null;

            if (link.StreamingUrl.OriginalString.Contains(".mpd"))
            {
                source = new DashMediaSource.Factory(new DefaultDashChunkSource.Factory(mediaSourceFactory), mediaSourceFactory)
                         .SetManifestParser(new FilteringManifestParser(new DashManifestParser(), new List <StreamKey>()))
                         .CreateMediaSource(uri);
            }
            if (link.StreamingUrl.OriginalString.Contains(".m3u8"))
            {
                source = new HlsMediaSource.Factory(mediaSourceFactory)
                         .SetPlaylistParserFactory(new DefaultHlsPlaylistParserFactory(new List <StreamKey>()))
                         .CreateMediaSource(uri);
            }
            if (link.StreamingUrl.OriginalString.Contains(".mp4"))
            {
                source = new ExtractorMediaSource.Factory(mediaSourceFactory).CreateMediaSource(uri);
            }
            return(source);
        }
Exemple #10
0
        void SetSource()
        {
            isPrepared = false;
            bool hasSetSource = false;
            DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory("1");

            DefaultSsChunkSource.Factory ssChunkFactory = new DefaultSsChunkSource.Factory(httpDataSourceFactory);
            Handler      emptyHandler = new Handler();
            IMediaSource videoSource  = null;

            if (Element.Source is HLSVideoSource)
            {
                string uri = (Element.Source as HLSVideoSource).Uri;

                if (!string.IsNullOrWhiteSpace(uri))
                {
                    //videoView.SetVideoURI(Android.Net.Uri.Parse(uri));
                    videoSource = new HlsMediaSource.Factory(httpDataSourceFactory)
                                  .CreateMediaSource(Android.Net.Uri.Parse(uri));
                    hasSetSource = true;
                }
            }
            else if (Element.Source is UriVideoSource)
            {
                string uri = (Element.Source as UriVideoSource).Uri;

                if (!string.IsNullOrWhiteSpace(uri))
                {
                    //videoView.SetVideoURI(Android.Net.Uri.Parse(uri));
                    var dataSourceFactory = new DefaultDataSourceFactory(Context, Util.GetUserAgent(Context, "Multimedia"));
                    videoSource  = new ExtractorMediaSource.Factory(dataSourceFactory).CreateMediaSource(Android.Net.Uri.Parse(uri));
                    hasSetSource = true;
                }
            }
            else if (Element.Source is FileVideoSource)
            {
                string filename = (Element.Source as FileVideoSource).File;

                if (!string.IsNullOrWhiteSpace(filename))
                {
                    DataSpec       dataSpec       = new DataSpec(Android.Net.Uri.FromFile(new Java.IO.File(filename)));
                    FileDataSource fileDataSource = new FileDataSource();
                    try
                    {
                        fileDataSource.Open(dataSpec);
                    }
                    catch (FileDataSource.FileDataSourceException e)
                    {
                        e.PrintStackTrace();
                    }
                    // videoView.SetVideoPath(filename);
                    IDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this.Context, "CloudMusic");
                    videoSource  = new ExtractorMediaSource.Factory(dataSourceFactory).CreateMediaSource(fileDataSource.Uri);
                    hasSetSource = true;
                }
            }
            else if (Element.Source is ResourceVideoSource)
            {
                string package = Context.PackageName;
                string path    = (Element.Source as ResourceVideoSource).Path;

                if (!string.IsNullOrWhiteSpace(path))
                {
                    string filename = Path.GetFileNameWithoutExtension(path).ToLowerInvariant();
                    string uri      = "android.resource://" + package + "/raw/" + filename;
                    //videoView.SetVideoURI(Android.Net.Uri.Parse(uri));
                    videoSource = new SsMediaSource.Factory(new DefaultSsChunkSource.Factory(httpDataSourceFactory), BuildDataSourceFactory(new DefaultBandwidthMeter()))
                                  .SetManifestParser(new FilteringManifestParser(new SsManifestParser(), null))
                                  .CreateMediaSource(Android.Net.Uri.Parse(uri));
                    hasSetSource = true;
                }
            }
            if (videoSource != null)
            {
                ExoPlayer.Prepare(videoSource);
            }
            if (hasSetSource && Element.AutoPlay)
            {
                ExoPlayer.PlayWhenReady = true;
                // videoView.Start();
            }
        }