Example #1
0
        public int InitializeMedia(string MediaFile, int dwrate, float dwvolume, bool IsFullScreen)
        {
            int hr;

            MFStartup(MF_VERSION, MFSTARTUP_FULL);
            MFCreateMediaSession(IntPtr.Zero, out mediaSession);

            MFCreateTopology(out IMFTopology topo);
            MFCreateSourceResolver(out IMFSourceResolver resolver);
            IUnknown unknown;

            try
            {
                resolver.CreateObjectFromURL(MediaFile, MF_RESOLUTION_MEDIASOURCE | MF_RESOLUTION_CONTENT_DOES_NOT_HAVE_TO_MATCH_EXTENSION_OR_MIME_TYPE,
                                             null, out uint objtype, out unknown);
            }
            catch
            {
                MessageBox.Show("不支持的媒体格式。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(0);
            }
            IMFMediaSource source = unknown as IMFMediaSource;

            source.CreatePresentationDescriptor(out IMFPresentationDescriptor descriptor);
            if (MFRequireProtectedEnvironment(descriptor) == 0)
            {
                MessageBox.Show("媒体受保护,无法播放。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(0);
            }
            descriptor.GetStreamDescriptorCount(out uint sdcount);
            for (uint i = 0; i < sdcount; i++)
            {
                descriptor.GetStreamDescriptorByIndex(i, out bool IsSelected, out IMFStreamDescriptor sd);
                if (!IsSelected)
                {
                    descriptor.SelectStream(i);
                }
                sd.GetMediaTypeHandler(out IMFMediaTypeHandler typeHandler);
                typeHandler.GetMajorType(out Guid streamtype);
                IMFActivate renderer;
                if (streamtype == MFMediaType_Audio)
                {
                    hr = MFCreateAudioRendererActivate(out renderer);
                }
                else if (streamtype == MFMediaType_Video)
                {
                    hr       = MFCreateVideoRendererActivate(VideoWnd.Handle, out renderer);
                    HasVideo = true;
                }
                else
                {
                    MessageBox.Show("不支持的媒体格式。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(0);
                }
                hr = MFCreateTopologyNode(MF_TOPOLOGY_SOURCESTREAM_NODE, out IMFTopologyNode sourcenode);
                sourcenode.SetUnknown(MF_TOPONODE_SOURCE, source as IUnknown);
                sourcenode.SetUnknown(MF_TOPONODE_PRESENTATION_DESCRIPTOR, descriptor as IUnknown);
                sourcenode.SetUnknown(MF_TOPONODE_STREAM_DESCRIPTOR, sd as IUnknown);
                topo.AddNode(sourcenode);
                MFCreateTopologyNode(MF_TOPOLOGY_OUTPUT_NODE, out IMFTopologyNode outputnode);
                outputnode.SetObject(renderer as IUnknown);
                topo.AddNode(outputnode);
                outputnode.SetUINT32(MF_TOPONODE_NOSHUTDOWN_ON_REMOVE, 0);
                hr = sourcenode.ConnectOutput(0, outputnode, 0);
            }
            mediaSession.SetTopology(0, topo);
            Rate              = dwrate;
            Volume            = dwvolume;
            this.IsFullScreen = IsFullScreen;
            uint eventtype = 0;

            while (eventtype != MESessionTopologyStatus)
            {
                mediaSession.GetEvent(0, out IMFMediaEvent mediaevent);
                mediaevent.GetType(out eventtype);
                mediaevent = null;
            }
            Guid guid_ratecontrol = typeof(IMFRateControl).GUID;

            MFGetService(mediaSession as IUnknown, ref MF_RATE_CONTROL_SERVICE, ref guid_ratecontrol, out IUnknown _ratecontrol);
            IMFRateControl ratecontrol = _ratecontrol as IMFRateControl;

            hr = ratecontrol.SetRate(false, Rate >= 0 ? Rate * 7 / 10 + 1 : 1 / (1 + Rate / -10 * 7));
            try
            {
                Guid guid_volume      = typeof(IMFStreamAudioVolume).GUID;
                Guid guid_audiopolicy = typeof(IMFAudioPolicy).GUID;
                hr = MFGetService(mediaSession as IUnknown, ref MR_STREAM_VOLUME_SERVICE, ref guid_volume, out IUnknown _volume);
                hr = MFGetService(mediaSession as IUnknown, ref MR_AUDIO_POLICY_SERVICE, ref guid_audiopolicy, out IUnknown _policy);
                IMFStreamAudioVolume volumecontrol = _volume as IMFStreamAudioVolume;
                IMFAudioPolicy       policy        = _policy as IMFAudioPolicy;
                volumecontrol.GetChannelCount(out uint channelcount);
                for (uint c = 0; c < channelcount; c++)
                {
                    volumecontrol.SetChannelVolume(c, Volume);
                }
                policy.SetDisplayName(" ");
            }
            catch
            {
            }
            try
            {
                Guid guid_videocontrol = typeof(IMFVideoDisplayControl).GUID;
                hr = MFGetService(mediaSession as IUnknown, ref MR_VIDEO_RENDER_SERVICE, ref guid_videocontrol, out IUnknown _videocontrol);
                IMFVideoDisplayControl videocontrol = _videocontrol as IMFVideoDisplayControl;
                VideoWnd.videocontrol = videocontrol;
                if (IsFullScreen)
                {
                    VideoWnd.Top    = 0;
                    VideoWnd.Left   = 0;
                    VideoWnd.Width  = (int)System.Windows.SystemParameters.PrimaryScreenWidth;
                    VideoWnd.Height = (int)System.Windows.SystemParameters.PrimaryScreenHeight;
                }
                Rectangle videopos = new Rectangle(0, 0, VideoWnd.Width, VideoWnd.Height);
                videocontrol.SetVideoPosition(null, ref videopos);
            }
            catch
            {
            }
            hr = mediaSession.BeginGetEvent((IMFAsyncCallback)this, null);
            return(1);
        }
Example #2
0
        public void InitializeMedia(string MediaFile, int dwrate, float dwvolume)
        {
            int hr;

            MFStartup(MF_VERSION, MFSTARTUP_FULL);
            MFCreateMediaSession(IntPtr.Zero, out IMFMediaSession mediaSession);
            MFCreateTopology(out IMFTopology topo);
            MFCreateSourceResolver(out IMFSourceResolver resolver);
            IUnknown unknown;

            try
            {
                resolver.CreateObjectFromURL(MediaFile, MF_RESOLUTION_MEDIASOURCE | MF_RESOLUTION_CONTENT_DOES_NOT_HAVE_TO_MATCH_EXTENSION_OR_MIME_TYPE,
                                             null, out uint objtype, out unknown);
            }
            catch
            {
                MessageBox.Show("不支持的媒体格式。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            IMFMediaSource source = unknown as IMFMediaSource;

            source.CreatePresentationDescriptor(out IMFPresentationDescriptor descriptor);
            if (MFRequireProtectedEnvironment(descriptor) == 0)
            {
                MessageBox.Show("媒体受保护,无法播放。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            descriptor.GetStreamDescriptorCount(out uint sdcount);
            for (uint i = 0; i < sdcount; i++)
            {
                descriptor.GetStreamDescriptorByIndex(i, out bool IsSelected, out IMFStreamDescriptor sd);
                if (!IsSelected)
                {
                    descriptor.SelectStream(i);
                }
                sd.GetMediaTypeHandler(out IMFMediaTypeHandler typeHandler);
                typeHandler.GetMajorType(out Guid streamtype);
                IMFActivate renderer;
                if (streamtype == MFMediaType_Audio)
                {
                    hr = MFCreateAudioRendererActivate(out renderer);
                }
                else if (streamtype == MFMediaType_Video)
                {
                    hr       = MFCreateVideoRendererActivate(host.Handle, out renderer);
                    hasvideo = true;
                }
                else
                {
                    continue;
                }
                hr = MFCreateTopologyNode(MF_TOPOLOGY_SOURCESTREAM_NODE, out IMFTopologyNode sourcenode);
                sourcenode.SetUnknown(MF_TOPONODE_SOURCE, source as IUnknown);
                sourcenode.SetUnknown(MF_TOPONODE_PRESENTATION_DESCRIPTOR, descriptor as IUnknown);
                sourcenode.SetUnknown(MF_TOPONODE_STREAM_DESCRIPTOR, sd as IUnknown);
                topo.AddNode(sourcenode);
                MFCreateTopologyNode(MF_TOPOLOGY_OUTPUT_NODE, out IMFTopologyNode outputnode);
                outputnode.SetObject(renderer as IUnknown);
                topo.AddNode(outputnode);
                hr = sourcenode.ConnectOutput(0, outputnode, 0);
            }
            mediaSession.SetTopology(0, topo);
            Hide();
            uint eventtype = 0;

            while (eventtype != MESessionTopologyStatus)
            {
                mediaSession.GetEvent(0, out IMFMediaEvent mediaevent);
                mediaevent.GetType(out eventtype);
                mediaevent = null;
            }
            Guid guid_ratecontrol = typeof(IMFRateControl).GUID;

            MFGetService(mediaSession as IUnknown, ref MF_RATE_CONTROL_SERVICE, ref guid_ratecontrol, out IUnknown _ratecontrol);
            IMFRateControl ratecontrol = _ratecontrol as IMFRateControl;

            hr = ratecontrol.SetRate(false, dwrate >= 0 ? dwrate * 7 / 10 + 1 : 1 / (1 + dwrate / -10 * 7));
            try
            {
                Guid guid_audiopolicy = typeof(IMFAudioPolicy).GUID;
                hr = MFGetService(mediaSession as IUnknown, ref MR_AUDIO_POLICY_SERVICE, ref guid_audiopolicy, out IUnknown _policy);
                IMFAudioPolicy policy = _policy as IMFAudioPolicy;
                policy.SetDisplayName(" ");
                playvolume = dwvolume;
            }
            catch
            {
            }
            try
            {
                Guid guid_videocontrol = typeof(IMFVideoDisplayControl).GUID;
                hr = MFGetService(mediaSession as IUnknown, ref MR_VIDEO_RENDER_SERVICE, ref guid_videocontrol, out IUnknown _videocontrol);
                IMFVideoDisplayControl videocontrol = _videocontrol as IMFVideoDisplayControl;
                if (FullScreen.Checked)
                {
                    host.Top    = 0;
                    host.Left   = 0;
                    host.Width  = (int)System.Windows.SystemParameters.PrimaryScreenWidth;
                    host.Height = (int)System.Windows.SystemParameters.PrimaryScreenHeight;
                }
                Rectangle videopos = new Rectangle(0, 0, host.Width, host.Height);
                host.videocontrol = videocontrol;
                videocontrol.SetVideoPosition(null, ref videopos);
            }
            catch
            {
            }
            state = "Prepared";
            while (true)
            {
                if (state == "Playing")
                {
                    eventtype = 0;
                    PropVariant prop = new PropVariant()
                    {
                        vt          = (ushort)VarEnum.VT_I8,
                        unionmember = 0,
                    };
                    #region mediasession
                    if (hasvideo)
                    {
                        host.Show();
                    }
                    preventthread = new Thread(() => {
                        while (true)
                        {
                            if (state == "Playing")
                            {
                                while (state == "Playing")
                                {
                                    if (hasvideo)
                                    {
                                        host.BringToFront();
                                    }
                                    SetAppAndSystemVolume(playvolume, false);
                                }
                            }
                        }
                    });
                    preventthread.Start();
                    hr = mediaSession.Start(Guid.Empty, prop);
                    while (eventtype != MESessionEnded)
                    {
                        hr = mediaSession.GetEvent(1, out IMFMediaEvent mediaevent);
                        if (hr == 0 && mediaevent != null)
                        {
                            mediaevent.GetType(out eventtype);
                            mediaevent = null;
                        }
                        Application.DoEvents();
                    }
                    host.Hide();
                    state = "Ended";
                    #endregion
                }
            }
        }