国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

Visual C#使用DirectX實現(xiàn)視頻播放

2024-07-21 02:27:12
字體:
來源:轉載
供稿:網(wǎng)友

  很多人第一次接觸到directx大都是通過游戲,至于安裝、升級directx的原因無非是滿足游戲運行的需要。direct x其實是微軟公司推出的一個為windows平臺的多媒體api函數(shù)庫,它提供標準接口來與顯卡和聲卡、輸入設備等進行交互。如果沒有這組標準api函數(shù)庫,那你就需要為每一種顯卡、聲卡的每個組合和每種類型的鍵盤、鼠標和游戲桿編寫不同的代碼。這不又回到了以前的dos時代。為了解決這個問題,微軟公司推出了directx。directx從具體的硬件中抽象出來,并且將一組通用指令轉換成硬件的具體命令。這樣開發(fā)語言通過調(diào)用統(tǒng)一標準的direct x函數(shù)庫就可以操作每一種顯卡、聲卡的每個組合和每種類型的鍵盤、鼠標和游戲桿等多媒體了。

  一.direct x sdk 9.0安裝及類庫介紹:

  .net framework sdk中并沒有包含direct x sdk,所以為了順利的完成本文后面介紹,
必須先下載、安裝direct x sdk。具體的下載地址為:http://download.microsoft.com/download/a/c/d/acdfb557-266f-4af4-8673-6ab8009b4ddd/dxsdk_apr2005.exe。此版本是2005四月份推出的英文版。這并不妨礙下面的程序實現(xiàn)。安裝完direct x后,就會在存在"c:/windows/microsoft.net/managed directx"這樣一個目錄,此目錄中目錄中應該有九個dll文件和九個xml文件。九個dll文件大致對應于directx中的十個命名空間。編程中就是使用其中的命名空間來提供對輸入設備、聲音、網(wǎng)絡播放、圖形等的支持。direct x sdk 9.0中的定義的命名空間及其主要的作用具體如表01所示:

命名空間描述
microsoft.directx公共類和數(shù)學結構
microsoft.directx.direct3d3d圖形和助手庫
microsoft.directx.directdrawdirect draw 圖形api。這是舊式命名空間,現(xiàn)在已經(jīng)不需要使用它。
microsoft.directx.directplay用于多玩家游戲的網(wǎng)絡api
microsoft.directx.directsound聲音支持
microsoft.directx.directinput輸入設備支持(例如,鼠標和游戲桿)
microsoft.directx.audiovideoplayback播放視頻和音頻(例如,在電腦上播放各自視頻動畫文件)
microsoft.directx.diagnostics疑難解答
microsoft.directx.security 訪問安全性
microsoft.directx.security.permissions訪問安全權限

       表01:direct x sdk9.0中的定義的命名空間及其主要的作用

  direct x內(nèi)容十分豐富,下文介紹的只是其中的一個小的應用,即用visual c#調(diào)用direct x中的sdk來播放視頻文件為例子,程序中只使用了命名空間"microsoft.directx.audiovideoplayback"。 microsoft.directx.audiovideoplayback命名空間中定義了三個類:"audio"、"video"和"texturerendereventargs"。其中前二個類是最常用的,分別用來支持音頻和視頻。microsoft.directx.audiovideoplayback命名空間中定義的常用屬性及其具體的作用可參閱表02所示:

屬性描述
audio 獲取視頻文件中的音頻對象,可用來后續(xù)的音頻播放
caption獲取或設置在form上播放視頻的名稱
currentposition獲取或設置播放視頻的當前位置
defaultsize獲取播放視頻的缺省的視頻大小
fullscreen 獲取或設置視頻文件是否在全屏模式下播放
iscursorhidden獲取播放的視頻時鼠標的狀態(tài):隱藏或顯示
owner 獲取或設置視頻播放的宿主組件
paused獲取當前的播放狀態(tài)是否處于暫停狀態(tài)
playing獲取當前的播放狀態(tài)是否處于播放狀態(tài).
seekingcaps獲取是否可以搜索性能
sizeretrieves and sets the size of the video for playback.
state獲取當前的播放狀態(tài)
stopped 獲取當前的播放狀態(tài)是否處于停止狀態(tài)
stopposition獲取播放的視頻的停止播放位置

    表02:microsoft.directx.audiovideoplayback命名空間中定義的常用屬性及其具體的作用

  microsoft.directx.audiovideoplayback命名空間中定義的常用方法及其具體的作用可參閱表03所示:

方法描述
hidecursor隱藏當前播放視頻的鼠標
open 裝入新的文件到video對象中
pause設置為暫停播放狀態(tài).
play設置為播放狀態(tài)
seekcurrentposition搜索轉入到制定的播放位置
seekstopposition設置一個新的停止位置
showcursor 顯示當前播放視頻的鼠標
stop設置為停止播放狀態(tài)
video初始化一個新的video實例

    表03:microsoft.directx.audiovideoplayback命名空間中定義的常用屬性及其具體的作用

  了解了以上這些基礎知識后,下面就開始介紹visual c#調(diào)用direct x播放視頻文件的具體實現(xiàn)方法。

  二.本文章的程序設計、調(diào)試和運行的環(huán)境:

  (1).微軟公司視窗2003中文企業(yè)版。

  (2).visual studio .net 2003企業(yè)構建版,.net framework sdk 1.1版本號4322。

  三.visual c#使用direct x的實現(xiàn)視頻播放


  visual c#使用direct x的實現(xiàn)視頻播放難點無非以下幾點:

  1. 掌握在visual c#引入dirext x函數(shù)庫的方法。

  2. 設置direct x視頻播放的宿主組件。

  3. 基本的播放命令的設置:播放、停止、暫停等。

  下面就是visual c#使用direct x的實現(xiàn)視頻播放具體實現(xiàn)步驟:

  1. 啟動visual studio .net。

  2. 選擇菜單【文件】|【新建】|【項目】后,彈出【新建項目】對話框。

  3. 將【項目類型】設置為【visual c#項目】。

  4. 將【模板】設置為【windows應用程序】。

  5. 在【名稱】文本框中輸入【visual c#中使用driectx實現(xiàn)媒體播放】。

  6. 在【位置】的文本框中輸入【e:/vs.net項目】,然后單擊【確定】按鈕。這樣在"e:/vs.net項目"目錄中就創(chuàng)建了一個名稱為"visual c#中使用driectx實現(xiàn)媒體播放"的文件夾,里面存放的就是"visual c#中使用driectx實現(xiàn)媒體播放"項目的所有文件。

  7. 選中【解決方案管理器】|【引用】,單擊鼠標右鍵,選中【添加引用】菜單,具體如圖01所示:


圖01:添加引用


  8. 選中【添加引用】菜單后,彈出【添加引用】對話框,按照圖02所示,在【選定的組件】欄目中加入"microsoft.directx.audiovideoplayback"后,單擊【確定】按鈕,則引用"microsoft.directx.audiovideoplayback"文件成功。這是因為visual studio .net的缺省編譯環(huán)境中沒有"microsoft.directx.audiovideoplayback.dll"文件,而程序中卻使用到"microsoft.directx.audiovideoplayback"命名空間,所以要在編譯環(huán)境中引用"microsoft.directx.audiovideoplayback"所在的文件。


  9. 把visual studio .net的當前窗口切換到【form1.cs(設計)】窗口,并從【工具箱】中的【windows窗體組件】選項卡中往設計窗體中拖入下列組件,并執(zhí)行相應操作:

  一個openfiledialog組件,用于選擇播放的視頻;一個panel組件,作為視頻播放的宿主組件;四個button按鈕,分別執(zhí)行視頻打開、播放、暫停和停止操作。并在這四個組件拖入設計窗口后分別雙擊它們,則系統(tǒng)會在form1.cs中分別產(chǎn)生這四個組件click事件對應的處理代碼。

  10. 把visual studio .net的當前窗口切換到form1.cs的代碼編輯窗口,在form1.cs的首部的引入命名空間的代碼區(qū)中,用下列代碼替換form1.cs中由系統(tǒng)自動產(chǎn)生的引入命名空間代碼:

using system ;
using system.drawing ;
using system.collections ;
using system.componentmodel ;
using system.windows.forms ;
using system.data ;
using microsoft.directx.audiovideoplayback ;
//引入視頻播放所要使用的direct x命名空間


  11. 在form1.cs的class的代碼區(qū)中添加下列代碼,下列代碼是創(chuàng)建全局使用的video實例:

private video myvideo = null ;
//創(chuàng)建一個video實例


  12. 以下面代碼替代系統(tǒng)產(chǎn)生的initializecomponent過程。下面代碼是對加入窗體的組件以及創(chuàng)建的全局變量進行初始化和定義四個button組件的click事件和form1的load事件:

private void initializecomponent ( )
{
 this.panel1 = new system.windows.forms.panel ( ) ;
 this.button1 = new system.windows.forms.button ( ) ;
 this.button2 = new system.windows.forms.button ( ) ;
 this.button3 = new system.windows.forms.button ( ) ;
 this.button4 = new system.windows.forms.button ( ) ;
 this.openfiledialog1 = new system.windows.forms.openfiledialog ( ) ;
 this.suspendlayout ( ) ;
 this.panel1.dock = system.windows.forms.dockstyle.top ;
 this.panel1.location = new system.drawing.point ( 0, 0 ) ;
 this.panel1.name = "panel1" ;
 this.panel1.size = new system.drawing.size ( 540, 346 ) ;
 this.panel1.tabindex = 0 ;
 this.button1.location = new system.drawing.point ( 62, 380 ) ;
 this.button1.name = "button1" ;
 this.button1.size = new system.drawing.size ( 80, 38 ) ;
 this.button1.tabindex = 1 ;
 this.button1.text = "打開" ;
 this.button1.click += new system.eventhandler ( this.button1_click ) ;
 this.button2.location = new system.drawing.point ( 165, 380 ) ;
 this.button2.name = "button2" ;
 this.button2.size = new system.drawing.size ( 81, 38 ) ;
 this.button2.tabindex = 1 ;
 this.button2.text = "播放" ;
 this.button2.click += new system.eventhandler ( this.button2_click ) ;
 this.button3.location = new system.drawing.point ( 268, 380 ) ;
 this.button3.name = "button3" ;
 this.button3.size = new system.drawing.size ( 80, 38 ) ;
 this.button3.tabindex = 1 ;
 this.button3.text = "暫停" ;
 this.button3.click += new system.eventhandler ( this.button3_click ) ;
 this.button4.location = new system.drawing.point ( 371, 380 ) ;
 this.button4.name = "button4" ;
 this.button4.size = new system.drawing.size ( 81, 38 ) ;
 this.button4.tabindex = 1 ;
 this.button4.text = "停止" ;
 this.button4.click += new system.eventhandler ( this.button4_click ) ;
 this.openfiledialog1.defaultext = "avi" ;
 this.openfiledialog1.filter = "視頻文件|*.avi||" ;
 this.openfiledialog1.title = "請選擇播放的avi文件" ;
 this.autoscalebasesize = new system.drawing.size ( 6, 14 ) ;
 this.clientsize = new system.drawing.size ( 540, 461 ) ;
 this.controls.add ( this.button1 ) ;
 this.controls.add ( this.panel1 ) ;
 this.controls.add ( this.button2 ) ;
 this.controls.add ( this.button3 ) ;
 this.controls.add ( this.button4 ) ;
 this.formborderstyle = system.windows.forms.formborderstyle.fixeddialog ;
 this.maximizebox = false ;
 this.name = "form1" ;
 this.text = "visual c#中使用driectx實現(xiàn)媒體播放" ;
 this.load += new system.eventhandler ( this.form1_load ) ;
 this.resumelayout ( false ) ;
}


  至此【visual c#中使用driectx實現(xiàn)媒體播放】項目的界面設計和功能實現(xiàn)的前期工作就完成了,設計界面如圖03所示:


圖03:【visual c#中使用driectx實現(xiàn)媒體播放】項目的界面設計

 13. 用下列代碼替換form1.cs中的button1組件的click事件對應的處理代碼,下列代碼的功能是打開選定的視頻文件,并在定義的panel組件上顯示開始第一幀:

private void button1_click ( object sender, system.eventargs e )
{
 openfiledialog1.initialdirectory = application.startuppath ;
 if ( openfiledialog1.showdialog ( ) == dialogresult.ok )
 {
  // 記錄panel組件的大小
  int height = panel1.height ;
  int width = panel1.width ;
  // 如果存在打開的video文件,釋放它
  if ( myvideo != null )
  {
   myvideo.dispose ( ) ;
  }
  // 打開一個新的video文件
  myvideo = new video ( openfiledialog1.filename ) ;
  // 把video文件分配給創(chuàng)建的panel組件
  myvideo.owner = panel1 ;
  // 以記錄的panel組件的大小來重新定義
  panel1.width = width ;
  panel1.height = height ;
  // 播放avi文件的第一幀,主要是為了在panel中顯示
  myvideo.play ( ) ;
  myvideo.pause ( ) ;
 }
 //確定窗體中的各按鈕狀態(tài)
 if ( myvideo == null )
 {
  button2.enabled = false ;
  button3.enabled = false ;
  button4.enabled = false ;
 }
 else
 {
  button2.enabled = true ;
  button3.enabled = true ;
  button4.enabled = true ;
 }
}

  14. 用下列代碼替換form1.cs中的button2組件的click事件對應的處理代碼,下列代碼的功能播放當前打開的視頻文件:

private void button2_click ( object sender, system.eventargs e )
{
 if ( myvideo != null )
 {
  myvideo.play ( ) ;
 }
}

  15. 用下列代碼替換form1.cs中的button3組件的click事件對應的處理代碼,下列代碼的功能暫停播放當前打開的視頻文件:

private void button3_click ( object sender, system.eventargs e )
{
 if ( myvideo != null )
 {
  myvideo.pause ( ) ;
 }
}

  16. 用下列代碼替換form1.cs中的button4組件的click事件對應的處理代碼,下列代碼的功能停止播放當前打開的視頻文件:

private void button4_click ( object sender, system.eventargs e )
{
 if ( myvideo != null )
 {
  myvideo.stop ( ) ;
 }
}

  17. 在button4的click事件之后,添加下列代碼,下列代碼的功能是初始化form窗體中的button組件:

//初始化窗體中各按鈕的狀態(tài)
private void form1_load ( object sender, system.eventargs e )
{
 if ( myvideo == null )
 {
  button2.enabled = false ;
  button3.enabled = false ;
  button4.enabled = false ;
 }
 else
 {
  button2.enabled = true ;
  button3.enabled = true ;
  button4.enabled = true ;
 }
}

  18. 至此,在上述步驟都正確完成,并全部保存后,【visual c#中使用driectx實現(xiàn)媒體播放】項目的全部工作就完成了。此時單擊快捷鍵【f5】運行程序后,圖04是播放視頻文件時得到的程序運行界面:


圖04:【visual c#中使用driectx實現(xiàn)媒體播放】項目的運行界面

  四.總結:

  direct x的出現(xiàn)的確解決了程序員的很多底層的工作,把面對各種煩雜硬件的工作交給了direct x了。雖然direct x從非托管版本發(fā)展到現(xiàn)在的托管版本,爭論一直存在,但越來越多的開發(fā)商把自己的軟件用托管的direct x或者結合使用托管和非托管direct x開發(fā),也從另外一個方面證明了托管的direct x的生命力。本文結合一個示例具體介紹visual c#調(diào)用非托管direct x的方法實現(xiàn)視頻播放,這對于托管direct x來說只是其中的一個小應用。最后希望此篇文章對那些希望了解、掌握在visual c#調(diào)用direct x編寫游戲程序的朋友有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 华安县| 丰城市| 黎城县| 张家川| 红河县| 大关县| 安阳市| 军事| 五河县| 麻城市| 石泉县| 晋城| 大名县| 佛教| 张掖市| 定西市| 阿勒泰市| 达孜县| 舞阳县| 凌海市| 开阳县| 崇明县| 广西| 中山市| 千阳县| 黎川县| 拉萨市| 唐山市| 罗江县| 鹤庆县| 蓝田县| 汝城县| 毕节市| 登封市| 平乐县| 红桥区| 托克托县| 津市市| 曲靖市| 沙田区| 曲麻莱县|