Пример #1
0
        private void PlatformPlay()
        {
            // Cleanup the last song first.
            if (State != MediaState.Stopped)
            {
                _session.Stop();
                _session.ClearTopologies();
                _session.Close();
                if (_volumeController != null)
                {
                    _volumeController.Dispose();
                    _volumeController = null;
                }
                _clock.Dispose();
            }

            //create the callback if it hasn't been created yet
            if (_callback == null)
            {
                _callback = new Callback(this);
                _session.BeginGetEvent(_callback, null);
            }

            // Set the new song.
            _session.SetTopology(SessionSetTopologyFlags.Immediate, _currentVideo.Topology);

            // Get the clock.
            _clock = _session.Clock.QueryInterface <PresentationClock>();

            // Start playing.
            var varStart = new Variant();

            _session.Start(null, varStart);
        }
Пример #2
0
        private void PlatformPlay()
        {
            // Cleanup the last song first.
            if (State != MediaState.Stopped)
            {
                _session.Stop();
                _volumeController.Dispose();
                _clock.Dispose();
            }

            // Set the new song.
            _session.SetTopology(0, _currentVideo.Topology);

            _volumeController              = CppObject.FromPointer <SimpleAudioVolume>(GetVolumeObj(_session));
            _volumeController.Mute         = IsMuted;
            _volumeController.MasterVolume = _volume;

            // Get the clock.
            _clock = _session.Clock.QueryInterface <PresentationClock>();

            //create the callback if it hasn't been created yet
            if (_callback == null)
            {
                _callback = new Callback();
                _session.BeginGetEvent(_callback, null);
            }

            // Start playing.
            var varStart = new Variant();

            _session.Start(null, varStart);
        }
 public void Invoke(AsyncResult asyncResultRef)
 {
     if (disposed)
     {
         return;
     }
     try
     {
         var ev = _session.EndGetEvent(asyncResultRef);
         if (disposed)
         {
             return;
         }
         if (ev.TypeInfo == MediaEventTypes.SessionTopologySet)
         {
             _player.Begin();
         }
         if (ev.TypeInfo == MediaEventTypes.SessionEnded)
         {
             _player.Playing = false;
         }
         _session.BeginGetEvent(this, null);
     }
     catch (Exception)
     {
     }
 }
Пример #4
0
        private static void PlatformPlaySong(Song song)
        {
            // Cleanup the last song first.
            if (State != MediaState.Stopped)
            {
                _session.Stop();
                _session.ClearTopologies();
                _session.Close();
                _volumeController.Dispose();
                _clock.Dispose();
            }

            // Set the new song.
            _session.SetTopology(SessionSetTopologyFlags.Immediate, song.Topology);

            // Get the volume interface.
            _volumeController = CppObject.FromPointer <AudioStreamVolume>(MediaPlayer.GetVolumeObj(_session));
            SetChannelVolumes();

            // Get the clock.
            _clock = _session.Clock.QueryInterface <PresentationClock>();

            //create the callback if it hasn't been created yet
            if (_callback == null)
            {
                _callback = new Callback();
                _session.BeginGetEvent(_callback, null);
            }

            // Start playing.
            var varStart = new Variant();

            _session.Start(null, varStart);
        }
        //// ===========================================================================================================
        //// Constructors
        //// ===========================================================================================================

        /// <summary>
        /// Initializes a new instance of the <see cref="MediaSessionCallback"/> class.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <param name="eventCallback">
        /// The event callback. The object <see cref="MediaEvent"/> must be disposed by the callback when finished with it.
        /// </param>
        public MediaSessionCallback(MediaSession session, Action <MediaEvent> eventCallback)
        {
            _session       = session;
            _eventCallback = eventCallback;

            // Subscribe to next events automatically
            session.BeginGetEvent(this, null);
        }
Пример #6
0
        private static void PlatformInitialize()
        {
            // The GUID is specified in a GuidAttribute attached to the class
            AudioStreamVolumeGuid = Guid.Parse(((GuidAttribute)typeof(AudioStreamVolume).GetCustomAttributes(typeof(GuidAttribute), false)[0]).Value);

            MediaManagerState.CheckStartup();
            MediaFactory.CreateMediaSession(null, out _session);

            _callback = new Callback();
            _session.BeginGetEvent(_callback, null);

            _clock = _session.Clock.QueryInterface <PresentationClock>();
        }
Пример #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MediaSessionCallback"/> class.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <param name="eventCallback">The event callback. The object <see cref="MediaEvent"/> must be disposed by the callback when finished with it.</param>
        /// <exception cref="System.ArgumentNullException">session or eventCallback</exception>
        public MediaSessionCallback(MediaSession session, Action<MediaEvent> eventCallback)
        {
            if (session == null)
                throw new ArgumentNullException("session");

            if (eventCallback == null)
                throw new ArgumentNullException("eventCallback");

            this.session = session;
            this.eventCallback = eventCallback;

            // Subscribe to next events automatically
            session.BeginGetEvent(this, null);
        }
Пример #8
0
        private static void PlatformPlaySong(Song song)
        {
            // Cleanup the last song first.
            if (State != MediaState.Stopped)
            {
                _session.Stop();
                _session.ClearTopologies();
                _session.Close();
                _volumeController.Dispose();
                _clock.Dispose();
            }

            // Set the new song.
            _session.SetTopology(SessionSetTopologyFlags.Immediate, song.Topology);

            // Get the volume interface.
            IntPtr volumeObj;


            try
            {
                MediaFactory.GetService(_session, MRPolicyVolumeService, SimpleAudioVolumeGuid, out volumeObj);
            }
            catch
            {
                MediaFactory.GetService(_session, MRPolicyVolumeService, SimpleAudioVolumeGuid, out volumeObj);
            }


            _volumeController              = CppObject.FromPointer <SimpleAudioVolume>(volumeObj);
            _volumeController.Mute         = _isMuted;
            _volumeController.MasterVolume = _volume;

            // Get the clock.
            _clock = _session.Clock.QueryInterface <PresentationClock>();

            //create the callback if it hasn't been created yet
            if (_callback == null)
            {
                _callback = new Callback();
                _session.BeginGetEvent(_callback, null);
            }

            // Start playing.
            var varStart = new Variant();

            _session.Start(null, varStart);
        }
Пример #9
0
        public override void Invoke(AsyncResult asyncResult)
        {
            // EndGetEvent mandatory
            var evt = session.EndGetEvent(asyncResult);

            var typeInfo = evt.TypeInfo;

            if (typeInfo != MediaEventTypes.SessionClosed)
            {
                // If not closed, continnue to subscribe to next events
                session.BeginGetEvent(this, null);
            }

            // Call the callback
            eventCallback(evt);
        }
Пример #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MediaSessionCallback"/> class.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <param name="eventCallback">The event callback. The object <see cref="MediaEvent"/> must be disposed by the callback when finished with it.</param>
        /// <exception cref="System.ArgumentNullException">session or eventCallback</exception>
        public MediaSessionCallback(MediaSession session, Action <MediaEvent> eventCallback)
        {
            if (session == null)
            {
                throw new ArgumentNullException("session");
            }

            if (eventCallback == null)
            {
                throw new ArgumentNullException("eventCallback");
            }

            this.session       = session;
            this.eventCallback = eventCallback;

            // Subscribe to next events automatically
            session.BeginGetEvent(this, null);
        }
Пример #11
0
        private void PlatformPlay()
        {
            // Cleanup the last song first.
            if (State != MediaState.Stopped)
            {
                _session.Stop();
                _session.ClearTopologies();
                _session.Close();
                if (_volumeController != null)
                {
                    _volumeController.Dispose();
                    _volumeController = null;
                }
                _clock.Dispose();
            }

            //create the callback if it hasn't been created yet
            if (_callback == null)
            {
                _callback = new Callback(this);
                _session.BeginGetEvent(_callback, null);
            }

            // Set the new song.
            _session.SetTopology(SessionSetTopologyFlags.Immediate, _currentVideo.Topology);

            // Get the clock.
            _clock = _session.Clock.QueryInterface <PresentationClock>();

            // Start playing.
            var varStart = new Variant();

            _session.Start(null, varStart);

            // we need to dispose of the old texture if we have one
            if (_videoCache != null)
            {
                _videoCache.Dispose();
            }
            // Create cached texture
            _videoCache = new Texture2D(_graphicsDevice, _currentVideo.Width, _currentVideo.Height, false, SurfaceFormat.Bgr32);
        }
        public override void PlayFile(string filename)
        {
            //Load the file
            MediaSource mediaSource;

            {
                var        resolver = new SourceResolver();
                ObjectType otype;
                var        source = new ComObject(resolver.CreateObjectFromURL(filename, SourceResolverFlags.MediaSource, null, out otype));
                try
                {
                    // Sometimes throws HRESULT: [0x80004002], Module: [General], ApiCode: [E_NOINTERFACE/No such interface supported], Message: No such interface supported. Bug?
                    mediaSource = source.QueryInterface <MediaSource>();
                }
                catch (SharpDXException)
                {
                    mediaSource = null;
                    FLLog.Error("VideoPlayerWMF", "QueryInterface failed on Media Foundation");
                }
                resolver.Dispose();
                source.Dispose();
            }
            if (mediaSource is null)
            {
                return;
            }

            PresentationDescriptor presDesc;

            mediaSource.CreatePresentationDescriptor(out presDesc);

            for (int i = 0; i < presDesc.StreamDescriptorCount; i++)
            {
                SharpDX.Mathematics.Interop.RawBool selected;
                StreamDescriptor desc;
                presDesc.GetStreamDescriptorByIndex(i, out selected, out desc);
                if (selected)
                {
                    TopologyNode sourceNode;
                    MediaFactory.CreateTopologyNode(TopologyType.SourceStreamNode, out sourceNode);

                    sourceNode.Set(TopologyNodeAttributeKeys.Source, mediaSource);
                    sourceNode.Set(TopologyNodeAttributeKeys.PresentationDescriptor, presDesc);
                    sourceNode.Set(TopologyNodeAttributeKeys.StreamDescriptor, desc);

                    TopologyNode outputNode;
                    MediaFactory.CreateTopologyNode(TopologyType.OutputNode, out outputNode);

                    var majorType = desc.MediaTypeHandler.MajorType;
                    if (majorType == MediaTypeGuids.Video)
                    {
                        Activate activate;

                        videoSampler = new MFSamples();
                        //retrieve size of video
                        long sz = desc.MediaTypeHandler.CurrentMediaType.Get <long>(new Guid("{1652c33d-d6b2-4012-b834-72030849a37d}"));
                        int  height = (int)(sz & uint.MaxValue), width = (int)(sz >> 32);
                        _texture = new Texture2D(width, height, false, SurfaceFormat.Color);
                        mt       = new MediaType();

                        mt.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video);

                        // Specify that we want the data to come in as RGB32.
                        mt.Set(MediaTypeAttributeKeys.Subtype, new Guid("00000016-0000-0010-8000-00AA00389B71"));
                        GetMethods();
                        MFCreateSampleGrabberSinkActivate(mt, videoSampler, out activate);
                        outputNode.Object = activate;
                    }

                    if (majorType == MediaTypeGuids.Audio)
                    {
                        Activate activate;
                        MediaFactory.CreateAudioRendererActivate(out activate);

                        outputNode.Object = activate;
                    }

                    topology.AddNode(sourceNode);
                    topology.AddNode(outputNode);
                    sourceNode.ConnectOutput(0, outputNode, 0);

                    sourceNode.Dispose();
                    outputNode.Dispose();
                }
                desc.Dispose();
            }

            presDesc.Dispose();
            mediaSource.Dispose();
            //Play the file
            cb = new MFCallback(this, session);
            session.BeginGetEvent(cb, null);
            session.SetTopology(SessionSetTopologyFlags.Immediate, topology);
            // Get the clock
            clock = session.Clock.QueryInterface <PresentationClock>();

            // Start playing.
            Playing = true;
        }
Пример #13
0
        private static void PlaySong(Song song)
        {
#if WINDOWS_MEDIA_ENGINE
            _mediaEngineEx.Source = song.FilePath;
            _mediaEngineEx.Load();
            _mediaEngineEx.Play();
#elif WINDOWS_MEDIA_SESSION
            // Cleanup the last song first.
            if (State != MediaState.Stopped)
            {
                _session.Stop();
                _volumeController.Dispose();
                _clock.Dispose();
            }

            // Set the new song.
            _session.SetTopology(0, song.GetTopology());

            // Get the volume interface.
            IntPtr volumeObj;


            try
            {
                MediaFactory.GetService(_session, MRPolicyVolumeService, SimpleAudioVolumeGuid, out volumeObj);
            }
            catch
            {
                MediaFactory.GetService(_session, MRPolicyVolumeService, SimpleAudioVolumeGuid, out volumeObj);
            }


            _volumeController              = CppObject.FromPointer <SimpleAudioVolume>(volumeObj);
            _volumeController.Mute         = _isMuted;
            _volumeController.MasterVolume = _volume;

            // Get the clock.
            _clock = _session.Clock.QueryInterface <PresentationClock>();

            //create the callback if it hasn't been created yet
            if (_callback == null)
            {
                _callback = new Callback();
                _session.BeginGetEvent(_callback, null);
            }

            // Start playing.
            var varStart = new Variant();
            _session.Start(null, varStart);
#elif WINDOWS_PHONE
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                _mediaElement.Source = new Uri(song.FilePath, UriKind.Relative);
                _mediaElement.Play();

                // Ensure only one subscribe
                _mediaElement.MediaEnded -= OnSongFinishedPlaying;
                _mediaElement.MediaEnded += OnSongFinishedPlaying;
            });
#else
            song.SetEventHandler(OnSongFinishedPlaying);
            song.Volume = _isMuted ? 0.0f : _volume;
            song.Play();
#endif
            State = MediaState.Playing;
        }