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>(); } }
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); } }
public StreamPlayer(Device device, int fps) { Device = device.QueryInterface <SharpDX.Direct3D11.Device>(); using (var manager = new DXGIDeviceManager()) using (var factory = new MediaEngineClassFactory()) using (var attributes = new MediaEngineAttributes(2)) { manager.ResetDevice(device); attributes.DxgiManager = manager; attributes.VideoOutputFormat = (int)Format.B8G8R8A8_UNorm; var baseEngine = new MediaEngine(factory, attributes, playbackCallback: OnEngineEvent); engine = baseEngine.QueryInterface <MediaEngineEx>(); } frameTime = TimeSpan.FromSeconds(1.0 / fps); SetCurrentState(StreamPlayerState.NoSource); }
private void PlatformSpecificInit() { // 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>(); } }
public void Initialize(SharpDXContext context) { lock (this.lockObject) { this.sharpDxContext = context; MediaManager.Startup(false); this.multithread = this.sharpDxContext.D3DContext.QueryInterface <DeviceMultithread>(); this.multithread.SetMultithreadProtected((Bool)true); this.dxgiDeviceManager = new DXGIDeviceManager(); this.dxgiDeviceManager.ResetDevice((ComObject)this.sharpDxContext.D3DDevice); this.attributes = new MediaEngineAttributes(0) { DxgiManager = this.dxgiDeviceManager, VideoOutputFormat = 87 }; using (MediaEngineClassFactory resource_0 = new MediaEngineClassFactory()) this.mediaEngine = new MediaEngine(resource_0, this.attributes, MediaEngineCreateFlags.None, new MediaEngineNotifyDelegate(this.OnMediaEngineEvent)); this.mediaEngineEx = this.mediaEngine.QueryInterface <MediaEngineEx>(); this.mediaEngine.Loop = (Bool)true; this.mediaEngine.AutoPlay = (Bool)true; } }
/// <summary> /// Defines the entry point of the application. /// </summary> /// <param name="args">The args.</param> public static void Run() { // Select a File to play var openFileDialog = new OpenFileDialog { Title = "Select a file", }; 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(); }