⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmcontrolm3u.vb

📁 这是一本用Visual Studio.NET进行多媒体编程的读物
💻 VB
字号:
Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class Form1
	Inherits System.Windows.Forms.Form
#Region "Windows 窗体设计器生成的代码"
	Public Sub New()
		MyBase.New()
		If m_vb6FormDefInstance Is Nothing Then
			If m_InitializingDefInstance Then
				m_vb6FormDefInstance = Me
			Else
				Try 
					'对于启动窗体,所创建的第一个实例为默认实例。
					If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
						m_vb6FormDefInstance = Me
					End If
				Catch
				End Try
			End If
		End If
		'此调用是 Windows 窗体设计器所必需的。
		InitializeComponent()
	End Sub
	'窗体重写处置,以清理组件列表。
	Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
		If Disposing Then
			If Not components Is Nothing Then
				components.Dispose()
			End If
		End If
		MyBase.Dispose(Disposing)
	End Sub
    Private components As System.ComponentModel.IContainer
	'Windows 窗体设计器所必需的
		Public ToolTip1 As System.Windows.Forms.ToolTip
	Public WithEvents ProgressBar1 As AxMSComctlLib.AxProgressBar
	Public WithEvents CommonDialog1 As AxMSComDlg.AxCommonDialog
	Public WithEvents MMControl1 As AxMCI.AxMMControl
	Public WithEvents Timer1 As System.Windows.Forms.Timer
	Public WithEvents cmdPre As System.Windows.Forms.Button
	Public WithEvents cmdExit As System.Windows.Forms.Button
	Public WithEvents cmdNext As System.Windows.Forms.Button
	Public WithEvents cmdPause As System.Windows.Forms.Button
	Public WithEvents cmdPlay As System.Windows.Forms.Button
	Public WithEvents cmdOpen As System.Windows.Forms.Button
	Public WithEvents List1 As System.Windows.Forms.ListBox
	'注意:以下过程是 Windows 窗体设计器所必需的
	'可以使用 Windows 窗体设计器来修改它。
	'不要使用代码编辑器来修改它。
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
        Me.cmdNext = New System.Windows.Forms.Button()
        Me.cmdExit = New System.Windows.Forms.Button()
        Me.List1 = New System.Windows.Forms.ListBox()
        Me.cmdPre = New System.Windows.Forms.Button()
        Me.ProgressBar1 = New AxMSComctlLib.AxProgressBar()
        Me.cmdPlay = New System.Windows.Forms.Button()
        Me.cmdOpen = New System.Windows.Forms.Button()
        Me.cmdPause = New System.Windows.Forms.Button()
        Me.MMControl1 = New AxMCI.AxMMControl()
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.CommonDialog1 = New AxMSComDlg.AxCommonDialog()
        CType(Me.ProgressBar1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.MMControl1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.CommonDialog1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'cmdNext
        '
        Me.cmdNext.BackColor = System.Drawing.SystemColors.Control
        Me.cmdNext.Cursor = System.Windows.Forms.Cursors.Default
        Me.cmdNext.Location = New System.Drawing.Point(31, 130)
        Me.cmdNext.Name = "cmdNext"
        Me.cmdNext.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.cmdNext.Size = New System.Drawing.Size(93, 28)
        Me.cmdNext.TabIndex = 4
        Me.cmdNext.Text = "下一曲"
        '
        'cmdExit
        '
        Me.cmdExit.BackColor = System.Drawing.SystemColors.Control
        Me.cmdExit.Cursor = System.Windows.Forms.Cursors.Default
        Me.cmdExit.Location = New System.Drawing.Point(338, 130)
        Me.cmdExit.Name = "cmdExit"
        Me.cmdExit.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.cmdExit.Size = New System.Drawing.Size(93, 28)
        Me.cmdExit.TabIndex = 5
        Me.cmdExit.Text = "退出"
        '
        'List1
        '
        Me.List1.BackColor = System.Drawing.SystemColors.Window
        Me.List1.Cursor = System.Windows.Forms.Cursors.Default
        Me.List1.ForeColor = System.Drawing.SystemColors.WindowText
        Me.List1.ItemHeight = 12
        Me.List1.Location = New System.Drawing.Point(20, 9)
        Me.List1.Name = "List1"
        Me.List1.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.List1.Size = New System.Drawing.Size(422, 64)
        Me.List1.TabIndex = 0
        '
        'cmdPre
        '
        Me.cmdPre.BackColor = System.Drawing.SystemColors.Control
        Me.cmdPre.Cursor = System.Windows.Forms.Cursors.Default
        Me.cmdPre.Location = New System.Drawing.Point(184, 130)
        Me.cmdPre.Name = "cmdPre"
        Me.cmdPre.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.cmdPre.Size = New System.Drawing.Size(94, 28)
        Me.cmdPre.TabIndex = 6
        Me.cmdPre.Text = "上一曲"
        '
        'ProgressBar1
        '
        Me.ProgressBar1.Location = New System.Drawing.Point(20, 176)
        Me.ProgressBar1.Name = "ProgressBar1"
        Me.ProgressBar1.OcxState = CType(resources.GetObject("ProgressBar1.OcxState"), System.Windows.Forms.AxHost.State)
        Me.ProgressBar1.Size = New System.Drawing.Size(422, 16)
        Me.ProgressBar1.TabIndex = 8
        '
        'cmdPlay
        '
        Me.cmdPlay.BackColor = System.Drawing.SystemColors.Control
        Me.cmdPlay.Cursor = System.Windows.Forms.Cursors.Default
        Me.cmdPlay.Location = New System.Drawing.Point(184, 93)
        Me.cmdPlay.Name = "cmdPlay"
        Me.cmdPlay.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.cmdPlay.Size = New System.Drawing.Size(94, 28)
        Me.cmdPlay.TabIndex = 2
        Me.cmdPlay.Text = "播放"
        '
        'cmdOpen
        '
        Me.cmdOpen.BackColor = System.Drawing.SystemColors.Control
        Me.cmdOpen.Cursor = System.Windows.Forms.Cursors.Default
        Me.cmdOpen.Location = New System.Drawing.Point(31, 93)
        Me.cmdOpen.Name = "cmdOpen"
        Me.cmdOpen.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.cmdOpen.Size = New System.Drawing.Size(93, 28)
        Me.cmdOpen.TabIndex = 1
        Me.cmdOpen.Text = "打开"
        '
        'cmdPause
        '
        Me.cmdPause.BackColor = System.Drawing.SystemColors.Control
        Me.cmdPause.Cursor = System.Windows.Forms.Cursors.Default
        Me.cmdPause.Location = New System.Drawing.Point(338, 93)
        Me.cmdPause.Name = "cmdPause"
        Me.cmdPause.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.cmdPause.Size = New System.Drawing.Size(93, 28)
        Me.cmdPause.TabIndex = 3
        Me.cmdPause.Text = "暂停"
        '
        'MMControl1
        '
        Me.MMControl1.Enabled = True
        Me.MMControl1.Location = New System.Drawing.Point(10, 213)
        Me.MMControl1.Name = "MMControl1"
        Me.MMControl1.OcxState = CType(resources.GetObject("MMControl1.OcxState"), System.Windows.Forms.AxHost.State)
        Me.MMControl1.Size = New System.Drawing.Size(302, 25)
        Me.MMControl1.TabIndex = 7
        '
        'Timer1
        '
        Me.Timer1.Enabled = True
        Me.Timer1.Interval = 1
        '
        'CommonDialog1
        '
        Me.CommonDialog1.Enabled = True
        Me.CommonDialog1.Location = New System.Drawing.Point(338, 213)
        Me.CommonDialog1.Name = "CommonDialog1"
        Me.CommonDialog1.OcxState = CType(resources.GetObject("CommonDialog1.OcxState"), System.Windows.Forms.AxHost.State)
        Me.CommonDialog1.Size = New System.Drawing.Size(32, 32)
        Me.CommonDialog1.TabIndex = 9
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(461, 199)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ProgressBar1, Me.CommonDialog1, Me.MMControl1, Me.cmdPre, Me.cmdExit, Me.cmdNext, Me.cmdPause, Me.cmdPlay, Me.cmdOpen, Me.List1})
        Me.Location = New System.Drawing.Point(3, 22)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
        Me.Text = "Form1"
        CType(Me.ProgressBar1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.MMControl1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.CommonDialog1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub
#End Region 
#Region "升级支持"
	Private Shared m_vb6FormDefInstance As Form1
	Private Shared m_InitializingDefInstance As Boolean
	Public Shared Property DefInstance() As Form1
		Get
			If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
				m_InitializingDefInstance = True
				m_vb6FormDefInstance = New Form1()
				m_InitializingDefInstance = False
			End If
			DefInstance = m_vb6FormDefInstance
		End Get
		Set
			m_vb6FormDefInstance = Value
		End Set
	End Property
#End Region 
	
	Dim MaxVal As Integer '进度条Max值变量
	Dim MyInd As Short '播放的文件索引变量
	Dim aM3uFile As String '要播放的M3U文件
	
	Private Sub cmdNext_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdNext.Click '"下一首"按钮代码
		PlayNext()
		List1.Focus() '让按钮好看点,下同
	End Sub
	
	Private Sub cmdPause_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdPause.Click '"暂停"按钮代码
		MMControl1.Command = "Pause"
		List1.Focus()
	End Sub
	
	Private Sub cmdPlay_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdPlay.Click '"播放"按钮代码
		Dim AAA As Single
		List1.Focus()
		If MMControl1.Command = "Stop" Or MMControl1.Command = "Pause" Then
			MMControl1.Command = "Play"
		Else
			Call PlayMe() '交给PlayMe处理
		End If
		AAA = MMControl1.Length
	End Sub
	
	Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click '"退出"按钮代码
		MMControl1.Command = "close"
		End
	End Sub
	
	Private Sub cmdPre_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdPre.Click '"前一首"按钮代码
		Timer1.Enabled = False
		List1.Focus()
		If MyInd = 0 Then '当前是第一首则播放最后一首
			List1.SelectedIndex = List1.Items.Count - 1
		Else
			List1.SelectedIndex = MyInd - 1
		End If
		MyInd = List1.SelectedIndex '这个变量要记得改哟
		Call PlayMe()
	End Sub
	
	
	
	Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load '程序加载
		Timer1.Enabled = False
		Timer1.Interval = 1000
		List1.BackColor = System.Drawing.Color.Black
		List1.ForeColor = System.Drawing.Color.Yellow
		MMControl1.Visible = False
		MyInd = 0
		'cmdOpen.Value = True '"打开"按钮被按下
		'OpenM3u '打开M3U文件
		'PlayMe '播放
	End Sub
	
	Private Sub cmdOpen_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOpen.Click '"打开"按钮代码
		On Error GoTo openerr
		List1.Items.Clear()
		CommonDialog1.Filter = "(*.m3u)|*.m3u"
		CommonDialog1.ShowOpen()
		aM3uFile = CommonDialog1.FileName
		OpenM3u() '打开M3U文件
		Exit Sub
openerr: 
		Timer1.Enabled = False '出错则不能让计时器工作
	End Sub
	
	'UPGRADE_WARNING: Form 事件 Form1.Unload 具有新的行为。 单击以获得更多信息:ms-help://MS.MSDNVS/vbcon/html/vbup2065.htm
	Private Sub Form1_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed
		MMControl1.Command = "Close" '退出程序时关闭设备,以释放资源
	End Sub
	
	Private Sub List1_DoubleClick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles List1.DoubleClick '双击列表框事件
		MyInd = List1.SelectedIndex
		cmdPlay.PerformClick()
	End Sub
	
	'判断是否要播放下一首
	Private Sub MMControl1_StatusUpdate(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MMControl1.StatusUpdate
		If MMControl1.Position = MMControl1.Length Then
			PlayNext()
		End If
	End Sub
	
	Private Sub PlayMe() '播放媒体文件
		
		With MMControl1
			.FileName = VB6.GetItemString(List1, MyInd)
			.Command = "stop"
			.Command = "close"
			.Command = "Open"
			.Command = "play"
		End With
		
		MaxVal = MMControl1.Length
		Timer1.Enabled = True
		Me.Text = VB6.GetItemString(List1, MyInd) '显示正在播放的文件名称
		ToolTip1.SetToolTip(ProgressBar1, "播放总数:" & List1.Items.Count & "首")
		''显示播放文件总数
		
	End Sub
	
	Private Sub Timer1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.Tick
		'进度条的Max和Value值
		ProgressBar1.Max = MaxVal
		ProgressBar1.Value = MMControl1.Position
	End Sub
	
	Private Sub PlayNext() '播放下一首
		Timer1.Enabled = False
		ProgressBar1.Value = 0
		MyInd = MyInd + 1 '当然加1了
		If MyInd >= List1.Items.Count Then
			MyInd = 0
		End If
		Call PlayMe()
		List1.SelectedIndex = MyInd
	End Sub
	
	Private Sub OpenM3u() '打开M3U文件并存入内存
		Dim aStr As String
		
		On Error GoTo M3uErr
		Dim AllLines As Collection = New Collection '内存中集合
		Dim CurrentLine As Integer '当前行集合索引
		Dim nextLine As String '从文件中读出的每一行
		Dim InFile As Short '文件的描述符
		InFile = FreeFile
		
		FileOpen(InFile, aM3uFile, OpenMode.Input) '打开文件
		While Not EOF(InFile)
			nextLine = LineInput(InFile)
			aStr = VB.Right(nextLine, 4)
			If aStr = ".mp3" Then
				AllLines.Add(nextLine)
			End If
		End While
		FileClose(InFile)
		
		Dim i As Short
		'以下为提取各行
		For i = 0 To AllLines.Count() - 1
			CurrentLine = CurrentLine + 1
			If AllLines.Count() < CurrentLine Then
				CurrentLine = 1
			End If
			
			If AllLines.Count() > 0 Then '当然要加入列表框啦
				'UPGRADE_WARNING: 未能解析对象 AllLines.Item(CurrentLine) 的默认属性。 单击以获得更多信息:ms-help://MS.MSDNVS/vbcon/html/vbup1037.htm
				List1.Items.Add(AllLines.Item(CurrentLine))
			End If
		Next i
		
		MMControl1.FileName = VB6.GetItemString(List1, 0)
		List1.SelectedIndex = 0 ' 列表框第一项高亮
		Exit Sub
M3uErr: 
		
	End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -