用vb.net写的播放器,在点歌曲以后出错,代码也不提示
我用 2003 写了一段代码,实现MP3播放功能,引用media player 总是出错,现在代码不提示出错了,也不知道问题出在那里,就是在播放的时候会中断停止运行,谁能帮帮我,谢谢。以下是代码:
Imports AxMediaPlayer
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports
Public Class Form6
Inherits System.Windows.Forms.Form
Dim AxMediaPlayer1 As New AxMediaPlayer.AxMediaPlayer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Me.OpenFileDialog1.ShowDialog() = DialogResult.Cancel Then
Exit Sub
End If
Me.ListView1.Items.Clear()
Dim MyFileNames() As String = Me.OpenFileDialog1.FileNames
Dim MyFileName As String
For Each MyFileName In MyFileNames
'取得文件大小
Dim MyFileInfo As FileInfo = New FileInfo(MyFileName)
Dim MyFileSize As Int16 = MyFileInfo.Length \ 1024 \ 1024
Me.AxMediaPlayer1.FileName = MyFileName
'取得作者信息
Dim MyAuthor As String = Me.AxMediaPlayer1.GetMediaInfoString(MediaPlayer.MPMediaInfoType.mpClipAuthor)
'取得不含路径的文件名
Dim MyShortFileName As String = MyFileName.Substring(MyFileName.LastIndexOf("\") + 1)
MyShortFileName = MyShortFileName.Substring(0, MyShortFileName.Length - 4)
'填充歌曲列表
Dim SubItem() As String = {MyShortFileName, MyAuthor, MyFileSize.ToString() + "M", MyFileName}
Dim Item As New ListViewItem(SubItem)
Me.ListView1.Items.Add(Item)
Next
Me.AxMediaPlayer1.Stop()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Me.ListView1.SelectedItems.Count < 1 Then
MessageBox.Show("请选择歌曲!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
Dim MyPos As Int16 = Me.ListView1.SelectedItems(0).Index
Dim MyFileName As String = Me.ListView1.Items(MyPos).SubItems(3).Text
Me.AxMediaPlayer1.FileName = MyFileName
Me.AxMediaPlayer1.Play()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
Me.AxMediaPlayer1.Pause()
Catch ex As Exception
MessageBox.Show("请选择歌曲!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If Me.ListView1.SelectedItems.Count > 0 Then
Dim MyPos As Int16 = Me.ListView1.SelectedItems(0).Index
If MyPos > 0 Then
Me.ListView1.Items(MyPos - 1).Selected = True
Dim MyFileName As String = Me.ListView1.Items(MyPos - 1).SubItems(3).Text
Me.AxMediaPlayer1.FileName = MyFileName
Me.AxMediaPlayer1.Play()
Else
MessageBox.Show("已经是第一首歌曲!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If Me.ListView1.SelectedItems.Count > 0 Then
Dim MyPos As Int16 = Me.ListView1.SelectedItems(0).Index
If MyPos < Me.ListView1.Items.Count - 1 Then
Me.ListView1.Items(MyPos + 1).Selected = True
Dim MyFileName As String = Me.ListView1.Items(MyPos + 1).SubItems(3).Text
Me.AxMediaPlayer1.FileName = MyFileName
Me.AxMediaPlayer1.Play()
Else
MessageBox.Show("已经是最后一首歌曲!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Try
Me.AxMediaPlayer1.Stop()
Catch ex As Exception
MessageBox.Show("请选择歌曲!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
End Class
在刚开始的Me.AxMediaPlayer1.FileName = MyFileName处出现如下错误
未处理的“InvalidActiveXStateException”类型的异常出现在 axinterop.mediaplayer.dll 中。
怎么改正??谢谢