播放音频文件VB.NET [英] Play audio file VB.NET

查看:58
本文介绍了播放音频文件VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MediaPlayer在VB.Net 2003中播放音频文件(.wav)。我找到了以下文章,将代码添加到我的表单中并且效果很好,但是,我不希望用户使用OpenFileDialog选择文件或单击播放,暂停或停止按钮。我希望MediaPlayer和隐藏的音频文件在Private Sub Form_Load部分中启动。你能帮我修改下面的代码来完成这个吗?谢谢。


CODE FROM ARTICLE ID:821767 11/26/07修订版2.8

选项严格在

公共类Form1







#Region" Windows窗体设计器生成的代码"

Public Sub New()
MyBase.New()

'Windows窗体设计器需要此调用。
InitializeComponent()< br>
'禁用Play,Pause和Stop按钮。
Button2.Enabled = False
Button3.Enabled = False
Button4.Enabled = False

'隐藏Media Player。
AxMedMediaPlayer1.Visible = False

'在InitializeComponent()调用后添加任何初始化。

End Sub

'表格覆盖Dispose to clean up组件列表。
受保护的重载覆盖子处理(ByVal disposing As Boolean)
如果处理那么

如果不是(组件是什么)那么

components.Dispose()
如果
结束如果
M yBase.Dispose(disposing)
End Sub

'Windows窗体设计器需要。
私有组件As System.ComponentModel.IContainer

'注意:需要以下步骤通过Windows窗体设计器
'可以使用Windows窗体设计器进行修改。
'不要使用代码编辑器修改它。
Friend WithEvents AxMediaPlayer1 As AxMediaPlayer.AxMediaPlayer
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms。 Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
< System。 Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.AxMediaPlayer1 = New AxMediaPlayer.AxMediaPlayer
Me.Button1 = New System。 Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.Button4 = New System.Windows.Forms.Button
Me.OpenFileDialog1 =新的System.Windows.Forms.OpenFileDialog
CType(Me.AxMediaPlayer1,System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
' AxMediaPlayer1


Me.AxMediaPlayer1.Location = New System.Drawing.Point(224,0)
Me.AxMediaPlayer1.Name =" AxMediaPlayer1"
Me.AxMediaPlayer1.OcxState = CType(资源.GetObject(" AxMediaPlayer1.OcxState"),System.Windows.Forms.AxHost .State)
Me.AxMediaPlayer1.Size = New System.Drawing.Size(286,225)
Me.AxMediaPlayer1.TabIndex = 0
'''Button1


我.Button1.Location = New System.Drawing.Point(16,72)
Me.Button1.Name =" Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text ="加载"
"
"Button2
"
Me.Button2.Location = New System.Drawing.Point(32,104)
Me.Button2.Name =" Button2"
Me.Button2.TabIndex = 2
Me.Button2.Text =" Play"
'''Button3
'

Me.Button3.Location = New System.Drawing.Point( 40,136)
Me.Button3.Name =" Button3"
Me.Button3.TabIndex = 3
Me.Button3.Text =" Pause"
'''Button4 < br>'
Me.Button4.Location = New System.Drawing.Point(56,192)
Me.Button4.Name =" Button4"
Me.Button4.TabIndex = 4
Me.Button4.Text =" ;停止"''''Form1


Me.AutoScaleBaseSize = New System.Drawing.Size(5,13)
Me.ClientSize = New System.Drawing.Size(712, 397)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add( Me.Button1)
Me.Controls.Add(Me.AxMediaPlayer1)
Me.Name =" Form1"
Me.Text =" Form1"
CType(Me.AxMediaPlayer1,System。 ComponentModel.ISupportInitialize)。EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object ,ByVal e As System.EventArgs)处理Button1.Click
重置"打开"对话框和媒体播放器的文件名。
OpenFileDialog1.FileName =""
AxMediaPlayer1.FileName =""
'显示"打开文件"对话框。 > OpenFileDialog1.ShowDialog()
'确认没有点击取消。
如果不是OpenFileDialog1.FileName =""然后
'禁用加载按钮。
Button1.Enabled = False
'阻止媒体播放器自动播放加载的文件。
AxMediaPlayer1.AutoStart = False
'设置媒体播放器音频文件。
AxMediaPlayer1.FileName = OpenFileDialog1.FileName
MessageBox.Show("以下文件已加载到Media Player控件中:" + AxMediaPlayer1.FileName)
'启用播放按钮。 > Button2.Enabled = True
Else
'禁用播放按钮。
Button2.Enabled = False
结束如果
End Sub

Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理Button2.Click
'禁用加载和播放按钮。
Button1.Enabled = False
Button2.Enabled = Fals e
'播放音频文件。
AxMediaPlayer1.Play()
'启用暂停和停止按钮。
Button3.Enabled = True
Button4.Enabled = True
End Sub

Private Sub Button3_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理Button3.Click
'禁用暂停按钮。
Button3.Enabled = False
'暂停音频文件。
AxMediaPlayer1.Pause()
'启用播放按钮。
Button2.Enabled = True
End Sub

Private Sub Button4_Click(ByVal sender As System。 Object,ByVal e As System.EventArgs)处理Button4.Click
'禁用暂停和停止按钮。
Button3.Enabled = False
Button4.Enabled = False
'停止播放音频文件,然后将下一个播放位置重置为开头。
AxMediaPlayer1.Stop()
AxMediaPlayer1.CurrentPosition = 0
'启用加载和播放按钮。
Button1.Enabled = True
Button2.Enabled = True
End Sub
End Class

解决方案

我发现了一些可以自动运行的东西。我刚刚将Media Player视觉属性设置为false并删除了加载,播放,暂停和停止按钮。然后将以下代码放在Private Sub form_Load部分中。

'重置打开文件对话框和媒体播放器的文件名
OpenFileDialog1.FileName =""
AxMediaPlayer1.FileName =" "

'设置打开文件对话框文件名(path \folder\file.wav)
OpenFileDialog1.FileName =" ; C:\ filename \ file.wav"



设置媒体播放器文件名
AxMediaPlayer1.FileName = OpenFileDialog1.FileName


I'm trying to play an audio file (.wav) in VB.Net 2003 using MediaPlayer.  I found the following article, added the code to my form and it works great, however, I don't want the user to select the file using OpenFileDialog or click play, pause or stop buttons.  I want the MediaPlayer and controls hidden with the audio file started in the Private Sub Form_Load section.  Could you please help me modify the following code to accomplish this?  Thanks.


CODE FROM ARTICLE ID: 821767 11/26/07 Revision 2.8

 Option Strict On

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        ' This call is required by the Windows Form Designer.
      InitializeComponent()

      ' Disable the Play, the Pause, and the Stop buttons.
      Button2.Enabled = False
      Button3.Enabled = False
      Button4.Enabled = False

      ' Hide the Media Player.
      AxMediaPlayer1.Visible = False
     
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    ' Form overrides Dispose to clean up the component list.
    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

    ' Required by the Windows Form Designer.
    Private components As System.ComponentModel.IContainer

    ' NOTE: The following procedure is required by the Windows Form Designer
    ' It can be modified using the Windows Form Designer. 
    ' Do not modify it using the code editor.
   Friend WithEvents AxMediaPlayer1 As AxMediaPlayer.AxMediaPlayer
   Friend WithEvents Button1 As System.Windows.Forms.Button
   Friend WithEvents Button2 As System.Windows.Forms.Button
   Friend WithEvents Button3 As System.Windows.Forms.Button
   Friend WithEvents Button4 As System.Windows.Forms.Button
   Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
      Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
      Me.AxMediaPlayer1 = New AxMediaPlayer.AxMediaPlayer
      Me.Button1 = New System.Windows.Forms.Button
      Me.Button2 = New System.Windows.Forms.Button
      Me.Button3 = New System.Windows.Forms.Button
      Me.Button4 = New System.Windows.Forms.Button
      Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
      CType(Me.AxMediaPlayer1, System.ComponentModel.ISupportInitialize).BeginInit()
      Me.SuspendLayout()
      '
      'AxMediaPlayer1
      '
      Me.AxMediaPlayer1.Location = New System.Drawing.Point(224, 0)
      Me.AxMediaPlayer1.Name = "AxMediaPlayer1"
      Me.AxMediaPlayer1.OcxState = CType(resources.GetObject("AxMediaPlayer1.OcxState"), System.Windows.Forms.AxHost.State)
      Me.AxMediaPlayer1.Size = New System.Drawing.Size(286, 225)
      Me.AxMediaPlayer1.TabIndex = 0
      '
      'Button1
      '
      Me.Button1.Location = New System.Drawing.Point(16, 72)
      Me.Button1.Name = "Button1"
      Me.Button1.TabIndex = 1
      Me.Button1.Text = "Load"
      '
      'Button2
      '
      Me.Button2.Location = New System.Drawing.Point(32, 104)
      Me.Button2.Name = "Button2"
      Me.Button2.TabIndex = 2
      Me.Button2.Text = "Play"
      '
      'Button3
      '
      Me.Button3.Location = New System.Drawing.Point(40, 136)
      Me.Button3.Name = "Button3"
      Me.Button3.TabIndex = 3
      Me.Button3.Text = "Pause"
      '
      'Button4
      '
      Me.Button4.Location = New System.Drawing.Point(56, 192)
      Me.Button4.Name = "Button4"
      Me.Button4.TabIndex = 4
      Me.Button4.Text = "Stop"
      '
      'Form1
      '
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(712, 397)
      Me.Controls.Add(Me.Button4)
      Me.Controls.Add(Me.Button3)
      Me.Controls.Add(Me.Button2)
      Me.Controls.Add(Me.Button1)
      Me.Controls.Add(Me.AxMediaPlayer1)
      Me.Name = "Form1"
      Me.Text = "Form1"
      CType(Me.AxMediaPlayer1, System.ComponentModel.ISupportInitialize).EndInit()
      Me.ResumeLayout(False)

   End Sub

#End Region

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      ' Reset the file names for the Open dialog box and for the Media Player.
      OpenFileDialog1.FileName = ""
      AxMediaPlayer1.FileName = ""
      ' Display the Open File dialog box.
      OpenFileDialog1.ShowDialog()
      ' Verify that Cancel was not clicked.
      If Not OpenFileDialog1.FileName = "" Then
         ' Disable the Load button.
         Button1.Enabled = False
         ' Prevent the Media Player from automatically playing loaded files.
         AxMediaPlayer1.AutoStart = False
         ' Set the Media Player audio file.
         AxMediaPlayer1.FileName = OpenFileDialog1.FileName
         MessageBox.Show("The following file has been loaded in the Media Player control: " + AxMediaPlayer1.FileName)
         ' Enable the Play button.
         Button2.Enabled = True
      Else
         ' Disable the Play button.
         Button2.Enabled = False
      End If
   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      ' Disable the Load and the Play buttons.
      Button1.Enabled = False
      Button2.Enabled = False
      ' Play the audio file.
      AxMediaPlayer1.Play()
      ' Enable the Pause and the Stop buttons.
      Button3.Enabled = True
      Button4.Enabled = True
   End Sub

   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
      ' Disable the Pause button.
      Button3.Enabled = False
      ' Pause the audio file.
      AxMediaPlayer1.Pause()
      ' Enable the Play button.
      Button2.Enabled = True
   End Sub

   Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
      ' Disable the Pause and the Stop buttons.
      Button3.Enabled = False
      Button4.Enabled = False
      ' Stop playing the audio file, and then reset the next play position to the beginning.
      AxMediaPlayer1.Stop()
      AxMediaPlayer1.CurrentPosition = 0
      ' Enable the Load and the Play buttons.
      Button1.Enabled = True
      Button2.Enabled = True
   End Sub
End Class

解决方案

I found something that works automatically.  I just made the Media Player visual property = false and deleted the load, play, pause and stop buttons.  Then place the following code in the Private Sub form_Load section.

'Reset the file names for the Open File Dialog box and Media Player
     OpenFileDialog1.FileName  = ""
      AxMediaPlayer1.FileName = ""

'Set Open File Dialog File Name (path\folder\file.wav)
      OpenFileDialog1.FileName = "C:\filename\file.wav"

"Set Media Player File Name
      AxMediaPlayer1.FileName = OpenFileDialog1.FileName
    
        


这篇关于播放音频文件VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆