首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> VC 教學

VB6.0 實現自動播放 VCD 完整解析

     編輯:VC 教學

問題背景

市面上的 VCD 播放軟體(如「超級解霸」)通常提供「播放 VCD」按鈕,點擊即可自動播放光碟影片。若檔案路徑固定,直接使用多媒體控件即可;但不同電腦的硬碟分區數量不同,光碟機代號(D:、E:、F:...)各異。如何自動偵測光碟機代號並播放 VCD?

核心原理:透過註冊表識別裝置類型

註冊表分析

使用 regedit.exe 檢視 Windows 98 註冊表:

  • 光碟機:HKEY_LOCAL_MACHINE\ENUM\SCSI → DeviceType = "5"
  • 硬碟:HKEY_LOCAL_MACHINE\ENUM\ESDI → DeviceType = "0"
  • 軟碟機:HKEY_LOCAL_MACHINE\ENUM\FLOP → DeviceType = "0A"

關鍵發現:DeviceType 參數區分不同裝置類型,光碟機固定為 5。

API 函式:GetDriveType

Win32 API 提供 GetDriveType 函式,傳回磁碟機類型:

UINT GetDriveType(LPCTSTR lpRootPathName);
// 回傳值:
// DRIVE_UNKNOWN     = 0
// DRIVE_NO_ROOT_DIR = 1
// DRIVE_REMOVABLE   = 2  (軟碟、USB)
// DRIVE_FIXED       = 3  (硬碟)
// DRIVE_REMOTE      = 4  (網路磁碟)
// DRIVE_CDROM       = 5  (光碟機) ← 我們要找的
// DRIVE_RAMDISK     = 6

自動偵測光碟機代號演算法

使用窮舉法依序檢查 D: 到 H:,找到 DeviceType = 5 的即為光碟機。

' VB6 程式碼
Dim driveCD As String

If GetDriveType("D:") <> 5 Then
    If GetDriveType("E:") <> 5 Then
        If GetDriveType("F:") <> 5 Then
            If GetDriveType("G:") <> 5 Then
                driveCD = "H"
                GoTo GetCDFiles
            End If
            driveCD = "G"
            GoTo GetCDFiles
        End If
        driveCD = "F"
        GoTo GetCDFiles
    End If
    driveCD = "E"
    GoTo GetCDFiles
Else
    driveCD = "D"
End If

GetCDFiles:
' 取得 VCD 檔案路徑
' 所有 VCD 影片路徑固定為 \MPEGAV\*.DAT
MMControl1.FileName = driveCD & ":\MPEGAV\" & Dir(driveCD & ":\MPEGAV\*.DAT")

完整程式碼實作

表單模組

' 宣告 API
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long

Dim files() As String   ' 儲存 VCD 檔案清單
Dim driveCD As String   ' 光碟機代號
Dim i As Integer        ' 檔案計數
Dim j As Integer        ' 目前播放索引
Dim s As String         ' 暫存檔名

Private Sub Form_Load()
    ' === 1. 偵測光碟機代號 ===
    If GetDriveType("D:") <> 5 Then
        If GetDriveType("E:") <> 5 Then
            If GetDriveType("F:") <> 5 Then
                If GetDriveType("G:") <> 5 Then
                    driveCD = "H"
                    GoTo GetCDFiles
                End If
                driveCD = "G"
                GoTo GetCDFiles
            End If
            driveCD = "F"
            GoTo GetCDFiles
        End If
        driveCD = "E"
        GoTo GetCDFiles
    Else
        driveCD = "D"
    End If

    ' === 2. 掃描 VCD 檔案 ===
GetCDFiles:
    On Error GoTo CDErr
    s = Dir(driveCD & ":\MPEGAV\*.DAT")
    i = 1
    While s <> ""
        ReDim Preserve files(i) As String
        files(i) = s
        i = i + 1
        s = Dir()
    Wend
    
    If i = 1 Then
        MsgBox "光碟中無 VCD 檔案!"
        Unload Me
        Exit Sub
    End If
    
    j = 1
    Call VCDPlay
    On Error GoTo 0
    Exit Sub

CDErr:
    MsgBox "CD 未就緒或無法讀取!"
    Unload Me
End Sub

' === 3. 播放控制 ===
Private Sub MMControl1_StatusUpdate()
    ' 當前播放位置等於長度表示播放結束
    If MMControl1.Position = MMControl1.Length Then
        j = j + 1
        If j > i - 1 Then j = 1  ' 循環播放
        Call VCDPlay
    End If
End Sub

Private Sub VCDPlay()
    MMControl1.Command = "Stop"
    MMControl1.Command = "Close"
    MMControl1.FileName = driveCD & ":\MPEGAV\" & files(j)
    MMControl1.Command = "Open"
    MMControl1.Command = "Play"
End Sub

關鍵技術點解析

1. 多媒體控件 (MMControl) 命令序列

命令功能
Stop停止目前播放
Close關閉目前檔案,釋放資源
Open開啟指定檔案
Play開始播放

必須按順序執行:Stop → Close → 設定 FileName → Open → Play

2. Dir() 函式掃描檔案

  • 第一次呼叫 Dir(path) 傳回第一個符合的檔名
  • 後續呼叫 Dir()(無參數)傳回下一個檔名
  • 傳回空字串表示掃描結束
  • ReDim Preserve 動態擴展陣列保留現有資料

3. 狀態更新事件實現自動連播

MMControl1_StatusUpdate 事件在播放狀態改變時觸發。判斷 Position = Length 表示播放結束,自動切換到下一個檔案。

改進與擴展建議

1. 支援更多磁碟機代號

Function GetCDDriveLetter() As String
    Dim drives As String
    drives = "DEFGHIJKLMNOPQRSTUVWXYZ"
    Dim k As Integer
    For k = 1 To Len(drives)
        If GetDriveType(Mid(drives, k, 1) & ":") = 5 Then
            GetCDDriveLetter = Mid(drives, k, 1)
            Exit Function
        End If
    Next k
    GetCDDriveLetter = ""
End Function

2. 錯誤處理強化

  • 檢查光碟是否為 VCD 格式(判斷 \MPEGAV 目錄是否存在)
  • 處理光碟損壞、讀取錯誤
  • 支援多光碟機系統(讓使用者選擇)

3. 播放清單管理

' 增加播放清單類別
Class VCDPlaylist
    Public Files As Collection
    Public CurrentIndex As Long
    
    Public Sub LoadFromDrive(driveLetter As String)
        Set Files = New Collection
        Dim f As String
        f = Dir(driveLetter & ":\MPEGAV\*.DAT")
        Do While f <> ""
            Files.Add f
            f = Dir()
        Loop
        CurrentIndex = 1
    End Sub
    
    Public Function GetNext() As String
        If Files.Count = 0 Then Exit Function
        CurrentIndex = CurrentIndex + 1
        If CurrentIndex > Files.Count Then CurrentIndex = 1
        GetNext = Files(CurrentIndex)
    End Function
End Class

4. 介面增強

  • 顯示目前播放檔案名稱、進度條、時間
  • 支援上一首、下一首、快轉、倒轉按鈕
  • 音量控制、靜音切換
  • 全螢幕播放模式

相容性說明

  • 開發環境:Visual Basic 6.0
  • 測試環境:中文 Windows 98
  • Windows 2000/XP/7 亦可運行,但需注意:
  • Vista 以後 GetDriveType 行為一致
  • VB6 IDE 在新版 Windows 需以相容模式執行
  • 建議遷移至 VB.NET + Windows Media Player 元件

結語

透過 GetDriveType API 識別光碟機、Dir() 掃描 VCD 檔案、MMControl 控制播放流程,即可實現 VB6 自動播放 VCD 功能。此技術展示了早期多媒體應用開發的核心邏輯,對理解現代媒體播放器架構亦有參考價值。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved