Exemplo n.º 1
0
        private void Play()
        {
            Uri            sourceUri             = Uri.Parse((Element.Source as UriVideoSource).Uri);
            var            httpDataSourceFactory = new DefaultHttpDataSourceFactory("1");
            var            hlsDataSourceFactory  = new DefaultHlsDataSourceFactory(httpDataSourceFactory);
            Handler        emptyHandler          = new Handler();
            HlsMediaSource hlsMediaSource        = new HlsMediaSource(sourceUri, hlsDataSourceFactory, 1, emptyHandler, this);

            _player.Prepare(hlsMediaSource);
        }
Exemplo n.º 2
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);
        }
        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(Android.Net.Uri.Parse(uri), httpDataSourceFactory, emptyHandler, null);
                    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(Android.Net.Uri.Parse(uri), dataSourceFactory, new DefaultExtractorsFactory(), emptyHandler, null);
                    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(fileDataSource.Uri, dataSourceFactory, new DefaultExtractorsFactory(), emptyHandler, null);
                    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(Android.Net.Uri.Parse(uri), httpDataSourceFactory, ssChunkFactory, emptyHandler, null);
                    hasSetSource = true;
                }
            }
            if (videoSource != null)
            {
                ExoPlayer.Prepare(videoSource);
            }
            if (hasSetSource && Element.AutoPlay)
            {
                ExoPlayer.PlayWhenReady = true;
                // videoView.Start();
            }
        }