public void Play() { if (frameLoopTask == null) { frameLoopTask = Task.Run(() => FrameLoopAsync(tokenSource.Token), tokenSource.Token); } engine.Play(); }
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); } }
/// <summary> /// Plays the audio/video. /// </summary> public void Play() { if (mediaEngineEx != null) { if (mediaEngineEx.HasVideo() && isVideoStopped) { isVideoStopped = false; } if (IsEndOfStream) { PlaybackPosition = 0; IsPlaying = true; } else { if (textureView == null) { int width = 0; int height = 0; mediaEngineEx.GetNativeVideoSize(out width, out height); OutputVideoTexture = new SharpDX.Direct3D11.Texture2D( d3dDevice, new SharpDX.Direct3D11.Texture2DDescription() { ArraySize = 1, Width = width, Height = height, Usage = SharpDX.Direct3D11.ResourceUsage.Default, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None, BindFlags = SharpDX.Direct3D11.BindFlags.RenderTarget | SharpDX.Direct3D11.BindFlags.ShaderResource, OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), MipLevels = 1, }); textureView = new ShaderResourceView(d3dDevice, OutputVideoTexture); } mediaEngineEx.Play(); } IsEndOfStream = false; } }
/// <summary> /// Plays the audio/video. /// </summary> public void Play() { if (mediaEngineEx != null) { if (mediaEngineEx.HasVideo() && isVideoStopped) { isVideoStopped = false; } if (isEndOfStream) { PlaybackPosition = 0; IsPlaying = true; } else { mediaEngineEx.Play(); } isEndOfStream = false; } }
/// <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(); }