예제 #1
0
		static MediaPlayer()
		{            
			MediaManager.Startup(true);
			
			using (var factory = new MediaEngineClassFactory())
			{
				var mediaEngine = new MediaEngine(factory, null, MediaEngineCreateflags.Audioonly);
				_mediaEngineEx = mediaEngine.QueryInterface<MediaEngineEx>();
			}
			
			_mediaEngineEx.PlaybackEvent += MediaEngineExOnPlaybackEvent;            
		}
예제 #2
0
        static void Main(string[] args)
        {
            // Select a File to play
            var openFileDialog = new OpenFileDialog { Title = "Select a music file", Filter = "Music Files(*.WMA;*.MP3;*.WAV)|*.WMA;*.MP3;*.WAV" };
            var result = openFileDialog.ShowDialog();
            if (result == DialogResult.Cancel)
            {
                return;
            }

            // Initialize MediaFoundation
            MediaManager.Startup();

            // Creates the MediaEngineClassFactory
            var mediaEngineFactory = new MediaEngineClassFactory();

            // Creates MediaEngine for AudioOnly 
            var mediaEngine = new MediaEngine(mediaEngineFactory, null, MediaEngineCreateFlags.AudioOnly);

            // Register our PlayBackEvent
            mediaEngine.PlaybackEvent += OnPlaybackCallback;

            // Query for MediaEngineEx interface
            mediaEngineEx = mediaEngine.QueryInterface<MediaEngineEx>();

            // Opens the file
            var fileStream = openFileDialog.OpenFile();
            
            // Create a ByteStream object from it
            var stream = new ByteStream(fileStream);

            // Creates an URL to the file
            var url = new Uri(openFileDialog.FileName, UriKind.RelativeOrAbsolute);

            // Set the source stream
            mediaEngineEx.SetSourceFromByteStream(stream, url.AbsoluteUri);

            // Wait for MediaEngine to be ready
            if (!eventReadyToPlay.WaitOne(1000))
            {
                Console.WriteLine("Unexpected error: Unable to play this file");
            }

            // Play the music
            mediaEngineEx.Play();

            // Wait until music is stopped.
            while (!isMusicStopped)
            {
                Thread.Sleep(10);
            }
        }
예제 #3
0
        private static void PlatformInitialize()
        {
            MediaManager.Startup(true);
            using (var factory = new MediaEngineClassFactory())
            using (var attributes = new MediaEngineAttributes { AudioCategory = AudioStreamCategory.GameMedia })
            {
                var creationFlags = MediaEngineCreateFlags.AudioOnly;

                var mediaEngine = new MediaEngine(factory, attributes, creationFlags, MediaEngineExOnPlaybackEvent);
                _mediaEngineEx = mediaEngine.QueryInterface<MediaEngineEx>();
            }

            _dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
        }
        internal override void InitImpl()
        {
            // Setup Media Engine attributes
            using (var attributes = new MediaEngineAttributes { AudioEndpointRole = AudioEndpointRole.Console,
                                                                AudioCategory = AudioStreamCategory.GameEffects })
            {
                var creationFlags = MediaEngineCreateFlags.None;
#if !SILICONSTUDIO_PLATFORM_WINDOWS_PHONE
                // MSDN: On the phone, the Media Engine only supports frame-server mode. Attempting to initialize the interface in either rendering mode or audio mode will fail.
                creationFlags |= MediaEngineCreateFlags.AudioOnly;
#endif
                using (var factory = new MediaEngineClassFactory())
                    mediaEngine = new MediaEngine(factory, attributes, creationFlags, OnMediaEngineEvent);

                mediaEngineEx = mediaEngine.QueryInterface<MediaEngineEx>();
            }
        }
예제 #5
0
        public virtual void Initialize(DeviceManager deviceManager)
        {
            lock (lockObject)
            {
                // Startup MediaManager
                MediaManager.Startup();

                // Setup multithread on the Direct3D11 device
                var multithread = deviceManager.DeviceDirect3D.QueryInterface<SharpDX.Direct3D.DeviceMultithread>();
                multithread.SetMultithreadProtected(true);

                // Create a DXGI Device Manager
                dxgiDeviceManager = new DXGIDeviceManager();
                dxgiDeviceManager.ResetDevice(deviceManager.DeviceDirect3D);

                // Setup Media Engine attributes
                var attributes = new MediaEngineAttributes
                {
                    DxgiManager = dxgiDeviceManager,
                    VideoOutputFormat = (int)SharpDX.DXGI.Format.B8G8R8A8_UNorm
                };

                using (var factory = new MediaEngineClassFactory())
                    mediaEngine = new MediaEngine(factory, attributes, MediaEngineCreateFlags.WaitForStableState, OnMediaEngineEvent);
                mediaEngineEx = mediaEngine.QueryInterface<MediaEngineEx>();
            }
        }
예제 #6
0
        static void Main(string[] args)
        {
            // Select a File to play
            var openFileDialog = new OpenFileDialog { Title = "Select a file", Filter = "Media Files(*.WMV;*.MP4;*.AVI)|*.WMV;*.MP4;*.AVI" };
            var result = openFileDialog.ShowDialog();
            if (result == DialogResult.Cancel)
            {
                return;
            }

            // Initialize MediaFoundation
            MediaManager.Startup();

            var renderForm = new SharpDX.Windows.RenderForm();

            device = CreateDeviceForVideo(out dxgiManager);

            // Creates the MediaEngineClassFactory
            var mediaEngineFactory = new MediaEngineClassFactory();
            
            //Assign our dxgi manager, and set format to bgra
            MediaEngineAttributes attr = new MediaEngineAttributes();
            attr.VideoOutputFormat = (int)SharpDX.DXGI.Format.B8G8R8A8_UNorm;
            attr.DxgiManager = dxgiManager;

            // Creates MediaEngine for AudioOnly 
            var mediaEngine = new MediaEngine(mediaEngineFactory, attr, MediaEngineCreateFlags.None);

            // Register our PlayBackEvent
            mediaEngine.PlaybackEvent += OnPlaybackCallback;

            // Query for MediaEngineEx interface
            mediaEngineEx = mediaEngine.QueryInterface<MediaEngineEx>();

            // Opens the file
            var fileStream = openFileDialog.OpenFile();
            
            // Create a ByteStream object from it
            var stream = new ByteStream(fileStream);

            // Creates an URL to the file
            var url = new Uri(openFileDialog.FileName, UriKind.RelativeOrAbsolute);

            // Set the source stream
            mediaEngineEx.SetSourceFromByteStream(stream, url.AbsoluteUri);

            // Wait for MediaEngine to be ready
            if (!eventReadyToPlay.WaitOne(1000))
            {
                Console.WriteLine("Unexpected error: Unable to play this file");
            }

            //Create our swapchain
            swapChain = CreateSwapChain(device, renderForm.Handle);

            //Get DXGI surface to be used by our media engine
            var texture = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
            var surface = texture.QueryInterface<SharpDX.DXGI.Surface>();

            //Get our video size
            int w, h;
            mediaEngine.GetNativeVideoSize(out w, out h);

            // Play the music
            mediaEngineEx.Play();

            long ts;

            RenderLoop.Run(renderForm, () =>
            {
                //Transfer frame if a new one is available
                if (mediaEngine.OnVideoStreamTick(out ts))
                {
                    mediaEngine.TransferVideoFrame(surface, null, new SharpDX.Rectangle(0, 0, w, h), null);
                }

                swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);
            });

            mediaEngine.Shutdown();
            swapChain.Dispose();
            device.Dispose();
        }