Exemple #1
0
        public async Task CreateFromStream_Options()
        {
            Uri uri = new Uri(_UriSource);

            Assert.IsNotNull(uri);

            StorageFile file = await StorageFile.CreateStreamedFileFromUriAsync("d8c317bd-9fbb-4c5f-94ed-501f09841917.mp4", uri, null);

            Assert.IsNotNull(file);

            IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);

            Assert.IsNotNull(readStream);

            // Setup options PropertySet to configure FFmpeg
            PropertySet options = new PropertySet();

            options.Add("rtsp_flags", "prefer_tcp");
            options.Add("stimeout", 100000);
            Assert.IsNotNull(options);

            // CreateFFmpegInteropMSSFromStream should return valid FFmpegInteropMSS object which generates valid MediaStreamSource object
            FFmpegInteropMSS FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(readStream, false, false, options);

            Assert.IsNotNull(FFmpegMSS);

            MediaStreamSource mss = FFmpegMSS.GetMediaStreamSource();

            Assert.IsNotNull(mss);

            // Based on the provided media, check if the following properties are set correctly
            Assert.AreEqual(true, mss.CanSeek);
            Assert.AreNotEqual(0, mss.BufferTime.TotalMilliseconds);
            Assert.AreEqual(_UriLength, mss.Duration.TotalMilliseconds);
        }
Exemple #2
0
        public async Task StartPlayback(MediaPlayer player)
        {
            string contentType = string.Empty;

            var stream = await StreamRef.OpenReadAsync();

            if (!stream.ContentType.EndsWith("mp4"))
            {
                _VideoMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(stream, false, false);
                var mss = _VideoMSS.GetMediaStreamSource();
                mss.SetBufferedRange(TimeSpan.Zero, TimeSpan.Zero);
                _MediaSource = MediaSource.CreateFromMediaStreamSource(mss);
            }
            else
            {
                _MediaSource = MediaSource.CreateFromStream(stream, stream.ContentType);
            }


            if (_MediaSource != null)
            {
                player.Source       = _MediaSource;
                _Stream             = stream;
                _PlayingMediaPlayer = player;
            }
            else
            {
                throw new NotSupportedException("can not play video. vide source from download progress stream.");
            }
        }
Exemple #3
0
        public async Task GetThumbnailFromMedia()
        {
            // Create a stream from the resource URI that we have
            var uri  = new Uri("ms-appx:///silence with album art.mp3");
            var file = await StorageFile.GetFileFromApplicationUriAsync(uri);

            var stream = await file.OpenAsync(FileAccessMode.Read);

            // CreateFFmpegInteropMSSFromUri should return null if uri is blank with default parameter
            FFmpegInteropMSS FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(stream, false, false);

            Assert.IsNotNull(FFmpegMSS);

            var thumbnailData = FFmpegMSS.ExtractThumbnail();

            Assert.IsNotNull(thumbnailData);

            // Verify that we have a valid bitmap
            using (IRandomAccessStream thumbnailstream = thumbnailData.Buffer.AsStream().AsRandomAccessStream())
            {
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(thumbnailstream);

                var bitmap = await decoder.GetFrameAsync(0);

                Assert.IsNotNull(bitmap);
            }
        }
Exemple #4
0
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            m_FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(m_mediaStream, false, false);
            MediaStreamSource mss = m_FFmpegMSS.GetMediaStreamSource();

            mediaElement1.SetMediaStreamSource(mss);
        }
        public async Task CreateFromStream_Force_Audio()
        {
            Uri uri = new Uri(Constants.DownloadUriSource);

            Assert.IsNotNull(uri);

            StorageFile file = await StorageFile.CreateStreamedFileFromUriAsync(Constants.DownloadStreamedFileName, uri, null);

            Assert.IsNotNull(file);

            IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);

            Assert.IsNotNull(readStream);

            // CreateFFmpegInteropMSSFromStream should return valid FFmpegInteropMSS object which generates valid MediaStreamSource object
            FFmpegInteropMSS FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(readStream, true, false);

            Assert.IsNotNull(FFmpegMSS);

            MediaStreamSource mss = FFmpegMSS.GetMediaStreamSource();

            Assert.IsNotNull(mss);

            // Based on the provided media, check if the following properties are set correctly
            Assert.AreEqual(true, mss.CanSeek);
            Assert.AreNotEqual(0, mss.BufferTime.TotalMilliseconds);
            Assert.AreEqual(Constants.DownloadUriLength, mss.Duration.TotalMilliseconds);
        }
        protected override async Task <MediaSource> GetPlyaingVideoMediaSource()
        {
            var file = await StorageFile.GetFileFromPathAsync(File.Path);

            var stream = await file.OpenReadAsync();

            var contentType = stream.ContentType;

            if (contentType == null)
            {
                throw new NotSupportedException("can not play video file. " + File.Path);
            }

            if (contentType == "video/mp4")
            {
                return(MediaSource.CreateFromStream(stream, contentType));
            }
            else
            {
                _VideoMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(stream, false, false);
                var mss = _VideoMSS.GetMediaStreamSource();
                mss.SetBufferedRange(TimeSpan.Zero, TimeSpan.Zero);
                return(MediaSource.CreateFromMediaStreamSource(mss));
            }
        }
        // Handle the returned files from file picker
        public async void ContinueFileOpenPicker(Windows.ApplicationModel.Activation.FileOpenPickerContinuationEventArgs args)
        {
            if (args.Files.Count > 0)
            {
                mediaElement.Stop();

                try
                {
                    // Open StorageFile as IRandomAccessStream to be passed to FFmpegInteropMSS
                    StorageFile         file       = args.Files.FirstOrDefault();
                    IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);

                    // Instantiate FFmpeg object and pass the stream from opened file
                    FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(readStream, forceDecodeAudio, forceDecodeVideo);
                    MediaStreamSource mss = FFmpegMSS.GetMediaStreamSource();

                    if (mss != null)
                    {
                        // Pass MediaStreamSource to Media Element
                        mediaElement.SetMediaStreamSource(mss);
                    }
                    else
                    {
                        DisplayErrorMessage("Cannot open media");
                    }
                }
                catch (Exception ex)
                {
                    DisplayErrorMessage(ex.Message);
                }
            }
        }
Exemple #8
0
        public async Task CreateFromStream_Force_Audio()
        {
            Uri uri = new Uri("http://go.microsoft.com/fwlink/p/?LinkID=272585");

            Assert.IsNotNull(uri);

            StorageFile file = await StorageFile.CreateStreamedFileFromUriAsync("d8c317bd-9fbb-4c5f-94ed-501f09841917.mp4", uri, null);

            Assert.IsNotNull(file);

            IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);

            Assert.IsNotNull(readStream);

            // CreateFFmpegInteropMSSFromStream should return valid FFmpegInteropMSS object which generates valid MediaStreamSource object
            FFmpegInteropMSS FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(readStream, true, false);

            Assert.IsNotNull(FFmpegMSS);

            MediaStreamSource mss = FFmpegMSS.GetMediaStreamSource();

            Assert.IsNotNull(mss);

            // Based on the provided media, check if the following properties are set correctly
            Assert.AreEqual(true, mss.CanSeek);
            Assert.AreNotEqual(0, mss.BufferTime.TotalMilliseconds);
            Assert.AreEqual(113750, mss.Duration.TotalMilliseconds);
        }
Exemple #9
0
        public void CreateFromStream_Null()
        {
            // CreateFFmpegInteropMSSFromStream should return null if stream is null with default parameter
            FFmpegInteropMSS FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(null, false, false);

            Assert.IsNull(FFmpegMSS);

            // CreateFFmpegInteropMSSFromStream should return null if stream is null with non default parameter
            FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(null, true, true);
            Assert.IsNull(FFmpegMSS);
        }
Exemple #10
0
        private async void OpenLocalFile(object sender, RoutedEventArgs e)
        {
            FileOpenPicker filePicker = new FileOpenPicker();

            filePicker.ViewMode = PickerViewMode.Thumbnail;
            filePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
            filePicker.FileTypeFilter.Add("*");

            // Show file picker so user can select a file
            StorageFile file = await filePicker.PickSingleFileAsync();

            if (file != null)
            {
                mediaElement.Stop();

                // Open StorageFile as IRandomAccessStream to be passed to FFmpegInteropMSS
                IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);

                try
                {
                    // Read toggle switches states and use them to setup FFmpeg MSS
                    bool forceDecodeAudio = toggleSwitchAudioDecode.IsOn;
                    bool forceDecodeVideo = toggleSwitchVideoDecode.IsOn;

                    // Instantiate FFmpegInteropMSS using the opened local file stream
                    FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(readStream, forceDecodeAudio, forceDecodeVideo);
                    MediaStreamSource mss = FFmpegMSS.GetMediaStreamSource();

                    var chapters = FFmpegMSS.GetChapterMetadata();

                    foreach (var chapter in chapters)
                    {
                        Debug.WriteLine("{0} : {1} ({2} :: {3})", chapter.GetChapter(), chapter.GetTitle(), chapter.GetStartTime(), chapter.GetEndTime());
                    }

                    if (mss != null)
                    {
                        // Pass MediaStreamSource to Media Element
                        mediaElement.SetMediaStreamSource(mss);

                        // Close control panel after file open
                        Splitter.IsPaneOpen = false;
                    }
                    else
                    {
                        DisplayErrorMessage("Cannot open media");
                    }
                }
                catch (Exception ex)
                {
                    DisplayErrorMessage(ex.Message);
                }
            }
        }
Exemple #11
0
        /// <summary>
        /// 设置FFmpeg媒体源
        /// </summary>
        private async Task SetFFmpegSource(StorageFile file)
        {
            var stream = await file.OpenAsync(FileAccessMode.Read);

            media.SetSource(stream, file.ContentType);
            FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(stream, force_a, force_v);
            MediaStreamSource mss = FFmpegMSS.GetMediaStreamSource();

            if (mss != null)
            {
                media.SetMediaStreamSource(mss);
            }
        }
        public async Task CreateFromStream_Destructor()
        {
            Uri uri = new Uri(Constants.DownloadUriSource);

            Assert.IsNotNull(uri);

            StorageFile file = await StorageFile.CreateStreamedFileFromUriAsync(Constants.DownloadStreamedFileName, uri, null);

            Assert.IsNotNull(file);

            IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);

            Assert.IsNotNull(readStream);

            // CreateFFmpegInteropMSSFromStream should return valid FFmpegInteropMSS object which generates valid MediaStreamSource object
            FFmpegInteropMSS FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(readStream, false, false);

            Assert.IsNotNull(FFmpegMSS);

            // Validate the metadata
            Assert.AreEqual(FFmpegMSS.AudioCodecName.ToLowerInvariant(), "aac");
            Assert.AreEqual(FFmpegMSS.VideoCodecName.ToLowerInvariant(), "h264");

            MediaStreamSource mss = FFmpegMSS.GetMediaStreamSource();

            Assert.IsNotNull(mss);

            // Based on the provided media, check if the following properties are set correctly
            Assert.AreEqual(true, mss.CanSeek);
            Assert.AreNotEqual(0, mss.BufferTime.TotalMilliseconds);
            Assert.AreEqual(Constants.DownloadUriLength, mss.Duration.TotalMilliseconds);

            // Keep original reference and ensure object are not destroyed until each reference is released by setting it to nullptr
            FFmpegInteropMSS  OriginalFFmpegMSS = FFmpegMSS;
            MediaStreamSource Originalmss       = mss;

            FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(null, false, false);
            Assert.IsNull(FFmpegMSS);
            Assert.IsNotNull(OriginalFFmpegMSS);
            Assert.IsNotNull(Originalmss);

            mss = null;
            Assert.IsNull(mss);
            Assert.IsNotNull(Originalmss);

            OriginalFFmpegMSS = null;
            Originalmss       = null;
            Assert.IsNull(OriginalFFmpegMSS);
            Assert.IsNull(Originalmss);
        }
Exemple #13
0
        private async void OpenLocalFile(object sender, RoutedEventArgs e)
        {
            var filePicker = new FileOpenPicker();

            filePicker.ViewMode = PickerViewMode.Thumbnail;
            filePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
            filePicker.FileTypeFilter.Add("*");

            // Show file picker so user can select a file
            var file = await filePicker.PickSingleFileAsync();

            if (file == null)
            {
                return;
            }

            mediaElement.Stop();

            var stream = await file.OpenAsync(FileAccessMode.Read);

            bool forceDecodeAudio = toggleSwitchAudioDecode.IsOn;
            bool forceDecodeVideo = toggleSwitchVideoDecode.IsOn;

            try
            {
                FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(stream, forceDecodeAudio, forceDecodeVideo);
                if (FFmpegMSS == null)
                {
                    DisplayErrorMessage("Cannot open media");
                    return;
                }

                var mss = FFmpegMSS.GetMediaStreamSource();
                if (mss == null)
                {
                    DisplayErrorMessage("Cannot open media");
                    return;
                }

                mediaElement.SetMediaStreamSource(mss);
                Splitter.IsPaneOpen = false;
            }
            catch (Exception ex)
            {
                DisplayErrorMessage(ex.Message);
            }
        }
Exemple #14
0
        public async void Execute(object parameter)
        {
            MediaElement mediaPlayer = (MediaElement)parameter;

            mediaPlayer.Stop();
            StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets");

            StorageFile file = await folder.GetFileAsync(SerieEpisodeDetailViewModel.SerieEpisode.Resource);

            IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);

            FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(readStream, true, true);
            MediaStreamSource mss = FFmpegMSS.GetMediaStreamSource();

            if (mss != null)
            {
                mediaPlayer.AreTransportControlsEnabled = true;

                mediaPlayer.TransportControls.IsFastForwardButtonVisible = true;
                mediaPlayer.TransportControls.IsFastForwardEnabled       = true;

                mediaPlayer.TransportControls.IsFastRewindButtonVisible = true;
                mediaPlayer.TransportControls.IsFastRewindEnabled       = true;

                mediaPlayer.TransportControls.IsSkipBackwardButtonVisible = true;
                mediaPlayer.TransportControls.IsSkipBackwardEnabled       = true;

                mediaPlayer.TransportControls.IsSkipForwardButtonVisible = true;
                mediaPlayer.TransportControls.IsSkipForwardEnabled       = true;

                mediaPlayer.TransportControls.IsStopButtonVisible = true;
                mediaPlayer.TransportControls.IsStopEnabled       = true;

                mediaPlayer.TransportControls.IsRightTapEnabled = true;

                mediaPlayer.TransportControls.IsStopButtonVisible = true;
                mediaPlayer.TransportControls.IsStopEnabled       = true;

                mediaPlayer.SetMediaStreamSource(mss);
                mediaPlayer.Play();
            }
            else
            {
                var msg = new MessageDialog("ERROR");
                await msg.ShowAsync();
            }
        }
Exemple #15
0
        public async Task CreateFromStream_Destructor()
        {
            Uri uri = new Uri("http://go.microsoft.com/fwlink/p/?LinkID=272585");

            Assert.IsNotNull(uri);

            StorageFile file = await StorageFile.CreateStreamedFileFromUriAsync("d8c317bd-9fbb-4c5f-94ed-501f09841917.mp4", uri, null);

            Assert.IsNotNull(file);

            IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);

            Assert.IsNotNull(readStream);

            // CreateFFmpegInteropMSSFromStream should return valid FFmpegInteropMSS object which generates valid MediaStreamSource object
            FFmpegInteropMSS FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(readStream, false, false);

            Assert.IsNotNull(FFmpegMSS);

            MediaStreamSource mss = FFmpegMSS.GetMediaStreamSource();

            Assert.IsNotNull(mss);

            // Based on the provided media, check if the following properties are set correctly
            Assert.AreEqual(true, mss.CanSeek);
            Assert.AreNotEqual(0, mss.BufferTime.TotalMilliseconds);
            Assert.AreEqual(113750, mss.Duration.TotalMilliseconds);

            // Keep original reference and ensure object are not destroyed until each reference is released by setting it to nullptr
            FFmpegInteropMSS  OriginalFFmpegMSS = FFmpegMSS;
            MediaStreamSource Originalmss       = mss;

            FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(null, false, false);
            Assert.IsNull(FFmpegMSS);
            Assert.IsNotNull(OriginalFFmpegMSS);
            Assert.IsNotNull(Originalmss);

            mss = null;
            Assert.IsNull(mss);
            Assert.IsNotNull(Originalmss);

            OriginalFFmpegMSS = null;
            Originalmss       = null;
            Assert.IsNull(OriginalFFmpegMSS);
            Assert.IsNull(Originalmss);
        }
Exemple #16
0
        private async void AppBarButton_Browse_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker filePicker = new FileOpenPicker();

            filePicker.ViewMode = PickerViewMode.Thumbnail;
            filePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
            filePicker.FileTypeFilter.Add("*");

            // Show file picker so user can select a file
            StorageFile file = await filePicker.PickSingleFileAsync();

            if (file != null)
            {
                mediaElement.Stop();

                // Open StorageFile as IRandomAccessStream to be passed to FFmpegInteropMSS
                IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);

                try
                {
                    // Instantiate FFmpeg object and pass the stream from opened file
                    FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(readStream, forceDecodeAudio, forceDecodeVideo);
                    MediaStreamSource mss = FFmpegMSS.GetMediaStreamSource();

                    if (mss != null)
                    {
                        // Pass MediaStreamSource to Media Element
                        mediaElement.SetMediaStreamSource(mss);
                    }
                    else
                    {
                        DisplayErrorMessage("Cannot open media");
                    }
                }
                catch (Exception ex)
                {
                    DisplayErrorMessage(ex.Message);
                }
            }

            // Set the TopAppBar to non-sticky so it will hide automatically after first file open
            this.TopAppBar.IsSticky = false;
            this.TopAppBar.IsOpen   = false;
        }
Exemple #17
0
        protected override async Task <MediaSource> GetPlyaingVideoMediaSource()
        {
            var videoUri = VideoUrl;

            MovieType videoContentType = MovieType.Mp4;
            var       tempStream       = await HttpSequencialAccessStream.CreateAsync(
                NiconicoSession.Context.HttpClient
                , videoUri
                );

            if (tempStream is IRandomAccessStreamWithContentType)
            {
                var contentType = (tempStream as IRandomAccessStreamWithContentType).ContentType;

                if (contentType.EndsWith("mp4"))
                {
                    videoContentType = MovieType.Mp4;
                }
                else if (contentType.EndsWith("flv"))
                {
                    videoContentType = MovieType.Flv;
                }
                else if (contentType.EndsWith("swf"))
                {
                    videoContentType = MovieType.Swf;
                }
                else
                {
                    throw new NotSupportedException($"{contentType} is not supported video format.");
                }
            }

            if (videoContentType != MovieType.Mp4)
            {
                _VideoMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(tempStream, false, false);
                var mss = _VideoMSS.GetMediaStreamSource();
                mss.SetBufferedRange(TimeSpan.Zero, TimeSpan.Zero);
                return(MediaSource.CreateFromMediaStreamSource(mss));
            }
            else
            {
                return(MediaSource.CreateFromStream(tempStream, (tempStream as IRandomAccessStreamWithContentType).ContentType));
            }
        }
Exemple #18
0
        public async Task CreateFromStream_Bad_Input()
        {
            Uri uri = new Uri("ms-appx:///test.txt");

            Assert.IsNotNull(uri);

            StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);

            Assert.IsNotNull(file);

            IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);

            Assert.IsNotNull(readStream);

            // CreateFFmpegInteropMSSFromStream should return null since test.txt is not a valid media file
            FFmpegInteropMSS FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(readStream, false, false);

            Assert.IsNull(FFmpegMSS);
        }
        public async Task CreateFromStream_Options()
        {
            Uri uri = new Uri(Constants.DownloadUriSource);

            Assert.IsNotNull(uri);

            StorageFile file = await StorageFile.CreateStreamedFileFromUriAsync(Constants.DownloadStreamedFileName, uri, null);

            Assert.IsNotNull(file);

            IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);

            Assert.IsNotNull(readStream);

            // Setup options PropertySet to configure FFmpeg
            PropertySet options = new PropertySet();

            options.Add("rtsp_flags", "prefer_tcp");
            options.Add("stimeout", 100000);
            Assert.IsNotNull(options);

            // CreateFFmpegInteropMSSFromStream should return valid FFmpegInteropMSS object which generates valid MediaStreamSource object
            FFmpegInteropMSS FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(readStream, false, false, options);

            Assert.IsNotNull(FFmpegMSS);

            // Validate the metadata
            Assert.AreEqual(FFmpegMSS.AudioCodecName.ToLowerInvariant(), "aac");
            Assert.AreEqual(FFmpegMSS.VideoCodecName.ToLowerInvariant(), "h264");

            MediaStreamSource mss = FFmpegMSS.GetMediaStreamSource();

            Assert.IsNotNull(mss);

            // Based on the provided media, check if the following properties are set correctly
            Assert.AreEqual(true, mss.CanSeek);
            Assert.AreNotEqual(0, mss.BufferTime.TotalMilliseconds);
            Assert.AreEqual(Constants.DownloadUriLength, mss.Duration.TotalMilliseconds);
        }
Exemple #20
0
        //Loads video and adds controls to commandbar
        private async void OpenMediaFile(object sender, RoutedEventArgs e)
        {
            //Get stream from VideoHandler
            stream = await videoHandler.LoadMediaFile();

            try
            {
                //Try reading the stream using FFmpegInterop https://github.com/Microsoft/FFmpegInterop
                ffmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(stream, true, true);
                MediaStreamSource mediaStreamSource = ffmpegMSS.GetMediaStreamSource();

                if (mediaStreamSource != null)
                {
                    //Add a bunch of command bar controls
                    mediaElement.AreTransportControlsEnabled                 = true;
                    mediaElement.TransportControls.IsFullWindowEnabled       = true;
                    mediaElement.TransportControls.IsFullWindowButtonVisible = true;
                    mediaElement.TransportControls.IsStopEnabled             = true;
                    mediaElement.TransportControls.IsStopButtonVisible       = true;
                    mediaElement.TransportControls.IsVolumeEnabled           = true;
                    mediaElement.TransportControls.IsVolumeButtonVisible     = true;

                    mediaElement.SetMediaStreamSource(mediaStreamSource);
                    mediaElement.Play();
                }
                else
                {
                    var message = new MessageDialog("An error has occured while opening your selected video file.");
                    await message.ShowAsync();
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception.Message);
                var message = new MessageDialog("An error has occured while opening your selected video file.");
                await message.ShowAsync();
            }
        }
Exemple #21
0
        public async Task StartPlayback(MediaPlayer player)
        {
            var videoUri = await GetVideoContentUri();

            // Note: HTML5プレイヤー移行中のFLV動画に対するフォールバック処理
            // サムネではContentType=FLV,SWFとなっていても、
            // 実際に渡される動画ストリームのContentTypeがMP4となっている場合がある

            var         videoContentType = MovieType.Mp4;
            MediaSource mediaSource      = null;

            if (!videoUri.IsFile)
            {
                // オンラインからの再生


                var tempStream = await HttpSequencialAccessStream.CreateAsync(
                    _Context.HttpClient
                    , videoUri
                    );

                if (tempStream is IRandomAccessStreamWithContentType)
                {
                    var contentType = (tempStream as IRandomAccessStreamWithContentType).ContentType;

                    if (contentType.EndsWith("mp4"))
                    {
                        videoContentType = MovieType.Mp4;
                    }
                    else if (contentType.EndsWith("flv"))
                    {
                        videoContentType = MovieType.Flv;
                    }
                    else if (contentType.EndsWith("swf"))
                    {
                        videoContentType = MovieType.Swf;
                    }
                    else
                    {
                        throw new NotSupportedException($"{contentType} is not supported video format.");
                    }
                }

                if (videoContentType != MovieType.Mp4)
                {
                    _VideoMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(tempStream, false, false);
                    var mss = _VideoMSS.GetMediaStreamSource();
                    mss.SetBufferedRange(TimeSpan.Zero, TimeSpan.Zero);
                    mediaSource = MediaSource.CreateFromMediaStreamSource(mss);
                }
                else
                {
                    tempStream.Dispose();
                    tempStream = null;

                    mediaSource = MediaSource.CreateFromUri(videoUri);
                }
            }
            else
            {
                // ローカル再生時


                var file = await StorageFile.GetFileFromPathAsync(videoUri.OriginalString);

                var stream = await file.OpenReadAsync();

                var contentType = stream.ContentType;

                if (contentType == null)
                {
                    throw new NotSupportedException("can not play video file. " + videoUri.OriginalString);
                }

                if (contentType == "video/mp4")
                {
                    mediaSource = MediaSource.CreateFromStream(stream, contentType);
                }
                else
                {
                    _VideoMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(stream, false, false);
                    var mss = _VideoMSS.GetMediaStreamSource();
                    mss.SetBufferedRange(TimeSpan.Zero, TimeSpan.Zero);
                    mediaSource = MediaSource.CreateFromMediaStreamSource(mss);
                }
            }


            if (mediaSource != null)
            {
                player.Source       = mediaSource;
                _MediaSource        = mediaSource;
                _PlayingMediaPlayer = player;

                OnStartStreaming();
            }
            else
            {
                throw new NotSupportedException("can not play video. Video URI: " + videoUri);
            }
        }
        private async void LoadVideoFile(object sender, TappedRoutedEventArgs e)
        {
            var picker = new FileOpenPicker();

            picker.ViewMode = PickerViewMode.Thumbnail;
            picker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
            picker.FileTypeFilter.Add(".mp4");
            StorageFile file = await picker.PickSingleFileAsync();

            if (file != null)
            {
                if (mediaPlayer.CanPause == true)
                {
                    try
                    {
                        mediaPlayer.Pause();
                    }
                    catch (Exception exception)
                    {
//                    System.Console.WriteLine(exception);
                        throw;
                    }
                }
                else
                {
                    try
                    {
                        mediaPlayer.Stop();
                    }
                    catch (Exception exception)
                    {
//                        System.Console.WriteLine(exception);
                        throw;
                    }
                }

                IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);

                try
                {
                    FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(stream, true, true);
                    MediaStreamSource mss = FFmpegMSS.GetMediaStreamSource();

                    if (mss != null)
                    {
                        mediaPlayer.AreTransportControlsEnabled = true;

                        mediaPlayer.TransportControls.IsStopButtonVisible = true;
                        mediaPlayer.TransportControls.IsStopEnabled       = true;

                        mediaPlayer.TransportControls.IsFastForwardButtonVisible = true;
                        mediaPlayer.TransportControls.IsFastForwardEnabled       = true;

                        mediaPlayer.TransportControls.IsFastRewindButtonVisible = true;
                        mediaPlayer.TransportControls.IsFastRewindEnabled       = true;
                        mediaPlayer.SetMediaStreamSource(mss);

                        mediaPlayer.Play();
                        VideoName.Text = "Now playing: " + file.Name;
                    }
                    else
                    {
                        var msg = new MessageDialog("errs");
                        await msg.ShowAsync();
                    }
                }
                catch (Exception exception)
                {
                    //                System.Console.WriteLine(exception);
                    throw;
                }
            }
        }
        private async void LoadMediaFile(object sender, TappedRoutedEventArgs e)
        {
            var picker = new FileOpenPicker();

            picker.ViewMode = PickerViewMode.Thumbnail;
            picker.SuggestedStartLocation = PickerLocationId.VideosLibrary;

            picker.FileTypeFilter.Add(".mp4");
            picker.FileTypeFilter.Add(".avi");
            picker.FileTypeFilter.Add(".fla");
            picker.FileTypeFilter.Add(".fvl");
            picker.FileTypeFilter.Add(".mpeg");
            picker.FileTypeFilter.Add(".mpeg2");
            picker.FileTypeFilter.Add(".3gp");
            picker.FileTypeFilter.Add(".mpg");
            picker.FileTypeFilter.Add(".mov");
            picker.FileTypeFilter.Add(".rmvb");
            picker.FileTypeFilter.Add(".vob");
            picker.FileTypeFilter.Add(".vmw");
            picker.FileTypeFilter.Add(".webm");

            StorageFile file = await picker.PickSingleFileAsync();

            if (file != null)
            {
                if (mediaPlayer.CanPause == true)
                {
                    try
                    {
                        mediaPlayer.Pause();
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            else
            {
                try
                {
                    mediaPlayer.Stop();
                }
                catch (Exception)
                {
                }
            }

            IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);

            try
            {
                FFmpegMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(readStream, true, true);
                MediaStreamSource mss = FFmpegMSS.GetMediaStreamSource();

                if (mss != null)
                {
                    mediaPlayer.AreTransportControlsEnabled = true;

                    mediaPlayer.TransportControls.IsFastForwardButtonVisible   = true;
                    mediaPlayer.TransportControls.IsFastForwardEnabled         = true;
                    mediaPlayer.TransportControls.IsFastRewindButtonVisible    = true;
                    mediaPlayer.TransportControls.IsFastRewindEnabled          = true;
                    mediaPlayer.TransportControls.IsNextTrackButtonVisible     = true;
                    mediaPlayer.TransportControls.IsPreviousTrackButtonVisible = true;
                    mediaPlayer.TransportControls.IsPlaybackRateButtonVisible  = true;
                    mediaPlayer.TransportControls.IsPlaybackRateEnabled        = true;
                    mediaPlayer.TransportControls.IsSkipBackwardButtonVisible  = true;
                    mediaPlayer.TransportControls.IsSkipBackwardEnabled        = true;
                    mediaPlayer.TransportControls.IsSkipForwardButtonVisible   = true;
                    mediaPlayer.TransportControls.IsSkipForwardEnabled         = true;
                    mediaPlayer.TransportControls.IsStopButtonVisible          = true;
                    mediaPlayer.TransportControls.IsStopEnabled     = true;
                    mediaPlayer.TransportControls.IsRightTapEnabled = true;

                    mediaPlayer.SetMediaStreamSource(mss);
                    mediaPlayer.Play();
                }
                else
                {
                    var msg = new MessageDialog("Error");
                    await msg.ShowAsync();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemple #24
0
        /// <summary>
        /// 動画ストリームの取得します
        /// 他にダウンロードされているアイテムは強制的に一時停止し、再生終了後に再開されます
        /// </summary>
        public async Task <NicoVideoQuality> StartPlay(NicoVideoQuality?quality, TimeSpan?initialPosition = null)
        {
            IfVideoDeletedThrowException();

            // 再生動画画質決定
            // 指定された画質>キャッシュされた画質>デフォルト指定画質>再生可能な画質
            DividedQualityNicoVideo divided = null;

            if (quality.HasValue)
            {
                divided = GetDividedQualityNicoVideo(quality.Value);
            }

            if (divided == null || !divided.CanPlay)
            {
                var cachedQuality = GetAllQuality().Where(x => x.IsCached).FirstOrDefault();
                if (cachedQuality != null)
                {
                    divided = cachedQuality;
                }
                else if (quality == NicoVideoQuality.Original)
                {
                    divided = GetDividedQualityNicoVideo(NicoVideoQuality.Low);
                }
                else
                {
                    divided = GetDividedQualityNicoVideo(HohoemaApp.UserSettings.PlayerSettings.DefaultQuality);

                    if (!divided.CanPlay)
                    {
                        foreach (var dmcQuality in GetAllQuality().Where(x => x.Quality.IsDmc()))
                        {
                            if (dmcQuality.CanPlay)
                            {
                                divided = dmcQuality;
                                break;
                            }
                        }
                    }

                    if (divided == null || !divided.CanPlay)
                    {
                        divided = GetDividedQualityNicoVideo(NicoVideoQuality.Original);
                        if (!divided.CanPlay)
                        {
                            divided = GetDividedQualityNicoVideo(NicoVideoQuality.Low);
                        }
                    }
                }
            }

            if (divided == null || !divided.CanPlay)
            {
                throw new NotSupportedException("再生可能な動画品質を見つけられませんでした。");
            }

            Debug.WriteLine(divided.Quality);

            // キャッシュ済みの場合は
            if (divided.HasCache)
            {
                var file = await divided.GetCacheFile();

                NicoVideoCachedStream = await file.OpenReadAsync();

                await OnCacheRequested();
            }
            else if (ProtocolType == MediaProtocolType.RTSPoverHTTP)
            {
                if (Util.InternetConnection.IsInternet())
                {
                    var videoUrl = await divided.GenerateVideoContentUrl();

                    if (videoUrl == null)
                    {
                        divided  = GetDividedQualityNicoVideo(NicoVideoQuality.Low);
                        videoUrl = await divided.GenerateVideoContentUrl();
                    }
                    NicoVideoCachedStream = await HttpSequencialAccessStream.CreateAsync(
                        HohoemaApp.NiconicoContext.HttpClient
                        , videoUrl
                        );
                }
            }

            if (NicoVideoCachedStream == null)
            {
                throw new NotSupportedException();
            }

            if (initialPosition == null)
            {
                initialPosition = TimeSpan.Zero;
            }

            if (NicoVideoCachedStream is IRandomAccessStreamWithContentType)
            {
                var contentType = (NicoVideoCachedStream as IRandomAccessStreamWithContentType).ContentType;

                if (contentType.EndsWith("mp4"))
                {
                    ContentType = MovieType.Mp4;
                }
                else if (contentType.EndsWith("flv"))
                {
                    ContentType = MovieType.Flv;
                }
                else if (contentType.EndsWith("swf"))
                {
                    ContentType = MovieType.Swf;
                }
            }

            MediaSource mediaSource = null;

            if (ContentType == MovieType.Mp4)
            {
                string contentType = null;

                if (NicoVideoCachedStream is IRandomAccessStreamWithContentType)
                {
                    contentType = (NicoVideoCachedStream as IRandomAccessStreamWithContentType).ContentType;
                }

                if (contentType == null)
                {
                    throw new Exception("unknown movie content type");
                }

                mediaSource = MediaSource.CreateFromStream(NicoVideoCachedStream, contentType);
            }
            else
            {
                _VideoMSS?.Dispose();

                _VideoMSS = FFmpegInteropMSS.CreateFFmpegInteropMSSFromStream(NicoVideoCachedStream, false, false);

                if (_VideoMSS != null)
                {
                    var mss = _VideoMSS.GetMediaStreamSource();
                    mss.SetBufferedRange(TimeSpan.Zero, TimeSpan.Zero);
                    mediaSource = MediaSource.CreateFromMediaStreamSource(mss);
                }
                else
                {
                    throw new NotSupportedException();
                }
            }

            if (mediaSource != null)
            {
                HohoemaApp.MediaPlayer.Source = mediaSource;
                HohoemaApp.MediaPlayer.PlaybackSession.Position = initialPosition.Value;

                _MediaSource = mediaSource;

                var smtc = HohoemaApp.MediaPlayer.SystemMediaTransportControls;

                smtc.DisplayUpdater.Type = Windows.Media.MediaPlaybackType.Video;
                smtc.DisplayUpdater.VideoProperties.Title = Title ?? "Hohoema";
                if (ThumbnailUrl != null)
                {
                    smtc.DisplayUpdater.Thumbnail = RandomAccessStreamReference.CreateFromUri(new Uri(this.ThumbnailUrl));
                }
                smtc.IsPlayEnabled  = true;
                smtc.IsPauseEnabled = true;
                smtc.DisplayUpdater.Update();

                divided.OnPlayStarted();
            }

            return(divided.Quality);
        }