DirectX里的Video和Audio C#的哦
来源:互联网 发布:无锡一米网络怎么样 编辑:程序博客网 时间:2024/06/06 20:47
原本是DX里的一个例子,我觉得还可以,就COPY出来,想改改看。结果让我大失所望。
最简单要求,就是对音频文件和视频文件可以定位查找,本来它的Video和Audio类都提供了Seek方法,但它们根本不能工作,每次Seek后,都回到0的位置上。后来用Reflector来看它的源代码,结果也是不尽人意,很多底层的代码没有办法反射出来。特别是DX里的一些接口,根本无法查看。最后也只好先暂停了。
本来还想着,要是DX在.Net下封装的比较好的话,我就可以少用C++的COM组件了,直接用C#来写组件也方便多了。没想到MS的托管DX确实还不够完善,我看可能要等以后的版本了。不过它的MediaPlay的SDK还是不错的,有机会也看看。
using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using Microsoft.DirectX;using Microsoft.DirectX.AudioVideoPlayback;namespace Webb.DirectX.Debug2{ /**//// <summary> /// Summary description for Form1. /// </summary> public class AVPlayer : System.Windows.Forms.Form { private string filterText = "Video Files (*.avi; *.qt; *.mov; *.mpg; *.mpeg; *.m1v; *.wmv)|*.avi; *.qt; *.mov; *.mpg; *.mpeg; *.m1v; *.wmv|" + "Audio files (*.wav; *.mpa; *.mp2; *.mp3; *.au; *.aif; *.aiff; *.snd; *.wma)|*.wav; *.mpa; *.mp2; *.mp3; *.au; *.aif; *.aiff; *.snd; *.wma|" + "MIDI Files (*.mid, *.midi, *.rmi)|*.mid; *.midi; *.rmi|" + "Image Files (*.jpg, *.bmp, *.gif, *.tga)|*.jpg; *.bmp; *.gif; *.tga|" + "All Files (*.*)|*.*"; private Video ourVideo = null; private Audio ourAudio = null; Winforms variables#region Winforms variables /**//// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; private System.Windows.Forms.MainMenu mnuMain; private System.Windows.Forms.OpenFileDialog ofdOpen; private System.Windows.Forms.MenuItem menuItem3; private System.Windows.Forms.MenuItem mnuFile; private System.Windows.Forms.MenuItem mnuOpen; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem mnuPlay; private System.Windows.Forms.MenuItem mnuStop; private System.Windows.Forms.MenuItem mnuPause; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.MenuItem mnuFull; private System.Windows.Forms.ToolBar toolBar1; private System.Windows.Forms.ToolBarButton toolBarButton1; private System.Windows.Forms.ToolBarButton toolBarButton2; private System.Windows.Forms.ToolBarButton toolBarButton3; private System.Windows.Forms.ToolBarButton toolBarButton4; private System.Windows.Forms.ToolBarButton toolBarButton5; private System.Windows.Forms.MenuItem mnuExit; #endregion public AVPlayer() { // // Required for Windows Form Designer support // InitializeComponent(); OpenFile(); } private bool _IsVideo = false; /**//// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { CleanupObjects(); if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } Windows Form Designer generated code#region Windows Form Designer generated code /**//// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.mnuMain = new System.Windows.Forms.MainMenu(); this.mnuFile = new System.Windows.Forms.MenuItem(); this.mnuOpen = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System.Windows.Forms.MenuItem(); this.mnuExit = new System.Windows.Forms.MenuItem(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.mnuPlay = new System.Windows.Forms.MenuItem(); this.mnuStop = new System.Windows.Forms.MenuItem(); this.mnuPause = new System.Windows.Forms.MenuItem(); this.menuItem2 = new System.Windows.Forms.MenuItem(); this.mnuFull = new System.Windows.Forms.MenuItem(); this.ofdOpen = new System.Windows.Forms.OpenFileDialog(); this.toolBar1 = new System.Windows.Forms.ToolBar(); this.toolBarButton1 = new System.Windows.Forms.ToolBarButton(); this.toolBarButton2 = new System.Windows.Forms.ToolBarButton(); this.toolBarButton3 = new System.Windows.Forms.ToolBarButton(); this.toolBarButton4 = new System.Windows.Forms.ToolBarButton(); this.toolBarButton5 = new System.Windows.Forms.ToolBarButton(); this.SuspendLayout(); // // mnuMain // this.mnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuFile, this.menuItem1}); // // mnuFile // this.mnuFile.Index = 0; this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuOpen, this.menuItem3, this.mnuExit}); this.mnuFile.Text = "&File"; // // mnuOpen // this.mnuOpen.Index = 0; this.mnuOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO; this.mnuOpen.Text = "&Open Clip"; this.mnuOpen.Click += new System.EventHandler(this.mnuOpen_Click); // // menuItem3 // this.menuItem3.Index = 1; this.menuItem3.Text = "-"; // // mnuExit // this.mnuExit.Index = 2; this.mnuExit.Text = "E&xit"; this.mnuExit.Click += new System.EventHandler(this.mnuExit_Click); // // menuItem1 // this.menuItem1.Index = 1; this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuPlay, this.mnuStop, this.mnuPause, this.menuItem2, this.mnuFull}); this.menuItem1.Text = "&Control"; // // mnuPlay // this.mnuPlay.Index = 0; this.mnuPlay.Shortcut = System.Windows.Forms.Shortcut.CtrlP; this.mnuPlay.Text = "&Play"; this.mnuPlay.Click += new System.EventHandler(this.mnuPlay_Click); // // mnuStop // this.mnuStop.Index = 1; this.mnuStop.Shortcut = System.Windows.Forms.Shortcut.CtrlS; this.mnuStop.Text = "&Stop"; this.mnuStop.Click += new System.EventHandler(this.mnuStop_Click); // // mnuPause // this.mnuPause.Index = 2; this.mnuPause.Shortcut = System.Windows.Forms.Shortcut.CtrlA; this.mnuPause.Text = "P&ause"; this.mnuPause.Click += new System.EventHandler(this.mnuPause_Click); // // menuItem2 // this.menuItem2.Index = 3; this.menuItem2.Text = "-"; // // mnuFull // this.mnuFull.Index = 4; this.mnuFull.Text = "Toggle Fu&llscreen\t<Alt-Enter>"; this.mnuFull.Click += new System.EventHandler(this.mnuFull_Click); // // toolBar1 // this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] { this.toolBarButton1, this.toolBarButton2, this.toolBarButton3, this.toolBarButton4, this.toolBarButton5}); this.toolBar1.DropDownArrows = true; this.toolBar1.Location = new System.Drawing.Point(0, 0); this.toolBar1.Name = "toolBar1"; this.toolBar1.ShowToolTips = true; this.toolBar1.Size = new System.Drawing.Size(536, 42); this.toolBar1.TabIndex = 0; this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick); // // toolBarButton1 // this.toolBarButton1.Text = "Play"; // // toolBarButton2 // this.toolBarButton2.Text = "Stop"; // // toolBarButton3 // this.toolBarButton3.Text = "End"; // // toolBarButton4 // this.toolBarButton4.Text = "Begin"; // // toolBarButton5 // this.toolBarButton5.Text = "Next"; // // AVPlayer // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(536, 305); this.Controls.Add(this.toolBar1); this.Menu = this.mnuMain; this.Name = "AVPlayer"; this.Text = "Audio Video Player"; this.ResumeLayout(false); } #endregion private void CleanupObjects() { if (ourVideo != null) ourVideo.Dispose(); if (ourAudio != null) ourAudio.Dispose(); } private void OpenFile() {// if ((ofdOpen.InitialDirectory == null) || (ofdOpen.InitialDirectory == string.Empty))// ofdOpen.InitialDirectory = DXUtil.SdkMediaPath; ofdOpen.Filter = filterText; ofdOpen.Title = "Open media file"; ofdOpen.ShowDialog(this); // Now let's try to open this file if ((ofdOpen.FileName != null) && (ofdOpen.FileName != string.Empty)) { try { if (ourVideo == null) { // First try to open this as a video file ourVideo = new Video(ofdOpen.FileName); ourVideo.Ending += new System.EventHandler(this.ClipEnded); ourVideo.Owner = this; // Start playing now ourVideo.Play(); this._IsVideo = true; } else { ourVideo.Open(ofdOpen.FileName, true); } } catch { try { // opening this as a video file failed.. Maybe it's audio only? ourAudio = new Audio(ofdOpen.FileName); ourAudio.Ending += new System.EventHandler(this.ClipEnded); // Start playing now ourAudio.Play(); this._IsVideo = false; } catch { MessageBox.Show("This file could not be opened.", "Invalid file.", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } private void ClipEnded(object sender, System.EventArgs e) { // The clip has ended, stop and restart it// if (ourVideo != null)// {// ourVideo.Stop();// ourVideo.Play();// }// else// {// if (ourAudio != null)// {// ourAudio.Stop();// ourAudio.Play();// }// } } private void mnuOpen_Click(object sender, System.EventArgs e) { this.OpenFile(); } private void mnuPlay_Click(object sender, System.EventArgs e) { if (ourVideo != null) ourVideo.Play(); else { if (ourAudio != null) ourAudio.Play(); } } private void mnuStop_Click(object sender, System.EventArgs e) { if (ourVideo != null) ourVideo.Stop(); else { if (ourAudio != null) ourAudio.Stop(); } } private void mnuPause_Click(object sender, System.EventArgs e) { if (ourVideo != null) ourVideo.Pause(); else { if (ourAudio != null) ourAudio.Pause(); } } private void mnuExit_Click(object sender, System.EventArgs e) { this.Dispose(); } private void mnuFull_Click(object sender, System.EventArgs e) { if (ourVideo != null) ourVideo.Fullscreen = !ourVideo.Fullscreen; } protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e) { if ( (e.Alt) && (e.KeyCode == System.Windows.Forms.Keys.Return)) { mnuFull_Click(mnuFull, null); } // Allow the control to handle the keystroke now base.OnKeyDown(e); } private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { switch(e.Button.Text) { case "Play": if(this._IsVideo) { this.ourVideo.Play(); } else { this.ourAudio.Play(); } break; case "End": if(_IsVideo) { double m_length = this.ourVideo.Duration; try { if(this.ourVideo.SeekingCaps.CanSeekForwards) { double m_result = this.ourVideo.SeekCurrentPosition(5,SeekPositionFlags.AbsolutePositioning); MessageBox.Show(m_result.ToString()); } } catch(Exception ex) { MessageBox.Show(ex.Message); } this.ourVideo.Pause(); } else { double m_length = this.ourAudio.Duration; double m_result = this.ourAudio.SeekCurrentPosition(5,SeekPositionFlags.AbsolutePositioning); this.ourAudio.Pause(); } break; case "Begin": if(_IsVideo) { this.ourVideo.SeekCurrentPosition(0,SeekPositionFlags.AbsolutePositioning); this.ourVideo.Pause(); } else { this.ourAudio.SeekCurrentPosition(0,SeekPositionFlags.AbsolutePositioning); this.ourAudio.Pause(); } break; case "Stop": if(_IsVideo) { this.ourVideo.Pause(); } else { this.ourAudio.Pause(); } break; case "Next": if(_IsVideo) { this.ourVideo.SeekCurrentPosition(5,SeekPositionFlags.IncrementalPositioning); //this.ourVideo.Pause(); } else { this.ourAudio.SeekCurrentPosition(5,SeekPositionFlags.IncrementalPositioning); //this.ourAudio.Pause(); } break; } } }}
- DirectX里的Video和Audio C#的哦
- Stagefright (7) - Audio和Video的同步
- Stagefright (7) - Audio和Video的同步
- Stagefright (7) - Audio和Video的同步
- 带video和audio的mediastream.c
- 带video和audio的mediastream.c
- HTML5 -video和audio的设置
- 关于html5里object、embed、video、audio的小总结
- stagefright框架(七)-Audio和Video的同步
- stagefright框架(七)-Audio和Video的同步
- stagefright框架(七)-Audio和Video的同步
- stagefright框架(七)-Audio和Video的同步
- stagefright框架(七)-Audio和Video的同步
- stagefright框架(七)-Audio和Video的同步
- stagefright框架(七)-Audio和Video的同步
- stagefright框架(七)-Audio和Video的同步
- stagefright框架(七)-Audio和Video的同步
- stagefright框架(七)-Audio和Video的同步
- Javascript图形处理库 -- Raphaël
- struts2文件上传实例
- XCAP
- 调用STM32F4 库函数FLASH_ProgramWord() 出错问题
- Android Binder总结
- DirectX里的Video和Audio C#的哦
- Oracle数据导入导出imp/exp
- 给盒子的贺文
- [摘自网络]android布局单位sp,dp,pt,px的区别
- PHP函数json_encode中文乱码解决
- 七夕·殇
- dip, dp, px, sp区别
- SQL Server 数据字典: sysobjects和syscolumn
- iOS开发者帐号申请详细步骤