熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

用VB.net2008打造你的影音播放器

2013-11-13 10:02:24  來源: .NET編程 

  本篇文章的主要開發環境是Visual Studio Visual Studio系列產品一直以來都提供了強大的控件功能然而我們利用這些控件可以編寫出功能強大的應用程序本文主要利用微軟的最開發工具為大家展示一個應用程序的開發過程讓大家對添加/引用控件更加熟悉很適開發工具的初學者具有一定的實用價值

  打開 Visual Studio 在文件 (File) 菜單上單擊新建項目 (New Project) 在新建項目 (New Project) 對話框的模板 (Templates) 窗格中單擊 Windows 應用程序(Windows Application)單擊確定 (OK)

  由於我們需要以Windows Media Player作為播放控件所以我們需要將Windows Media Player的控件添加到我們的工具箱在此之前請安裝最新的Windows Media Player SDK或者Windows Media Player播放器即可一般情況下系統都默認安裝了這個播放器如果你確定已經安裝了請搜索wmpdll這個文件(一般存在\system\wmpdll)如搜索完成後直接將此控件拖入我們的控件工具箱即可

  拖入我們的工具箱

  選擇此控件拖入我們的Form界面

  選擇Form窗體在Form窗體中添加如下控件
    OpenFileDialog控件Timer控件MenuStripSaveFileDialogFolderBrowserDialog ListBox控件HScrollBar控件

  個按鈕控件分別為ButtonButtonButton
    控件屬性設置如下
    Button Text: 打開
    Button Text: 播放
    Button Text: 停止
    MenuStrip 添加菜單選項 文件
    MenuStrip 菜單選項 打開
    MenuStrip 菜單選項 打開目錄
    MenuStrip 菜單選項 關閉

  進入Button_Click事件

    Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick
      
        OpenFileDialogInitialDirectory = c:\
        OpenFileDialogFilter = mp 文件(*mp)|*mp|CD音頻文件(*wav)|*wav| & 視頻(*asf)|*asf|所有文件(**)|**

        If OpenFileDialogShowDialog = WindowsFormsDialogResultOK Then
            AxWindowsMediaPlayerURL = OpenFileDialogFileName
            ListBoxItemsAdd(OpenFileDialogFileName)
        End If
End Sub

  進入Button_Click事件

    Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick
        If ButtonText = 播放 Then
            AxWindowsMediaPlayerCtlcontrolspause()
            ButtonText = 暫停
        Else
            AxWindowsMediaPlayerCtlcontrolsplay()
            ButtonText = 播放
        End If
    End Sub

    Private Sub 打開ToolStripMenuItem_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles 打開ToolStripMenuItemClick
        Button_Click(sender e)
End Sub

  進入Button_Click事件

    Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick

        AxWindowsMediaPlayerCtlcontrolsstop() 停止
        AxWindowsMediaPlayerCtlcontrolscurrentPosition() = 重新開始
        AxWindowsMediaPlayerURL =
End Sub

  進入 打開ToolStripMenuItem_Click事件
    進入打開目錄ToolStripMenuItem_Click事件
    Private Sub 打開目錄ToolStripMenuItem_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles 打開目錄ToolStripMenuItemClick

    If FolderBrowserDialogShowDialog = WindowsFormsDialogResultOK Then
            Dim fi As IOFileInfo
            Dim dir As IODirectoryInfo = New IODirectoryInfo(FolderBrowserDialogSelectedPath)
            Dim file As String
            For Each fi In dirGetFiles(*mp)
                file = fiFullName
                ListBoxItemsAdd(file)
            Next
        End If
End Sub

  進入關閉ToolStripMenuItem_Click事件

    Private Sub 關閉ToolStripMenuItem_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles 關閉ToolStripMenuItemClick
        關閉
        If MessageBoxShow(請確定你要關閉嗎? 關閉 MessageBoxButtonsOKCancel) = WindowsFormsDialogResultOK Then
            Close()
        Else
            Return
        End If
    End Sub

  進入Timer_Tick事件

    Private Sub Timer_Tick(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles TimerTick
        HScrollBarValue = AxWindowsMediaPlayerCtlcontrolscurrentPosition
    End Sub

  進入HScrollBar_Scroll事件

    Private Sub HScrollBar_Scroll(ByVal sender As SystemObject ByVal e As SystemWindowsFormsScrollEventArgs) Handles HScrollBarScroll
        進度條
        AxWindowsMediaPlayerCtlcontrolscurrentPosition() = HScrollBarValue
    End Sub

  進入ListBox_DoubleClick事件

    Private Sub ListBox_DoubleClick(ByVal sender As Object ByVal e As SystemEventArgs) Handles ListBoxDoubleClick
        AxWindowsMediaPlayerURL = ListBoxSelectedItemToString
End Sub    代碼已經輸入完畢接下來我們需要運行程序進行測試

  好了程序運行成功此播放器已經具備了最基本的功能感興趣的朋友還可以向程序增加更多的功能


From:http://tw.wingwit.com/Article/program/net/201311/12359.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.