在 VB2010 Windows 窗体开始时播放 .wav/.mp3 文件?

2024-06-19

制作 VB2010 已经大约一年了,最近开始突破我可以将哪种媒体合并到我的表单中的界限。 但我无法播放 .wav 或 .mp3 文件。 我尝试按照微软和其他编码网站上的教程进行操作,但没有成功。 任何帮助,将不胜感激。


要播放波形文件,您可以简单地使用:

        My.Computer.Audio.Play("c:\temp\temp.wav")

如果您想播放 mp3、wav、midi,您可以进行一些 WIN32 API 调用。这是我几年前创建的一个课程,可以让您轻松地玩它们。在您的项目中创建一个名为 AudioFile.vb 的类并将其粘贴进去。不过我已经很多年没有看过这段代码了,呵呵,它可能可以被清理。

''' <summary>
''' This class is a wrapper for the Windows API calls to play wave, midi or mp3 files.
''' </summary>
''' <remarks>
''' </remarks>
Public Class AudioFile
'***********************************************************************************************************
'        Class:  PlayFile
'   Written By:  Blake Pell ([email protected] /cdn-cgi/l/email-protection)
' Initial Date:  03/31/2007
' Last Updated:  02/04/2009
'***********************************************************************************************************

' Windows API Declarations
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Int32, ByVal hwndCallback As Int32) As Int32

''' <summary>
''' Constructor:  Location is the filename of the media to play.  Wave files and Mp3 files are the supported formats.
''' </summary>
''' <param name="Location"></param>
''' <remarks></remarks>
Public Sub New(ByVal location As String)
    Me.Filename = location
End Sub

''' <summary>
''' Plays the file that is specified as the filename.
''' </summary>
''' <remarks></remarks>
Public Sub Play()

    If _filename = "" Or Filename.Length <= 4 Then Exit Sub

    Select Case Right(Filename, 3).ToLower
        Case "mp3"
            mciSendString("open """ & _filename & """ type mpegvideo alias audiofile", Nothing, 0, IntPtr.Zero)

            Dim playCommand As String = "play audiofile from 0"

            If _wait = True Then playCommand += " wait"

            mciSendString(playCommand, Nothing, 0, IntPtr.Zero)
        Case "wav"
            mciSendString("open """ & _filename & """ type waveaudio alias audiofile", Nothing, 0, IntPtr.Zero)
            mciSendString("play audiofile from 0", Nothing, 0, IntPtr.Zero)
        Case "mid", "idi"
            mciSendString("stop midi", "", 0, 0)
            mciSendString("close midi", "", 0, 0)
            mciSendString("open sequencer!" & _filename & " alias midi", "", 0, 0)
            mciSendString("play midi", "", 0, 0)
        Case Else
            Throw New Exception("File type not supported.")
            Call Close()
    End Select

    IsPaused = False

End Sub

''' <summary>
''' Pause the current play back.
''' </summary>
''' <remarks></remarks>
Public Sub Pause()
    mciSendString("pause audiofile", Nothing, 0, IntPtr.Zero)
    IsPaused = True
End Sub

''' <summary>
''' Resume the current play back if it is currently paused.
''' </summary>
''' <remarks></remarks>
Public Sub [Resume]()
    mciSendString("resume audiofile", Nothing, 0, IntPtr.Zero)
    IsPaused = False
End Sub

''' <summary>
''' Stop the current file if it's playing.
''' </summary>
''' <remarks></remarks>
Public Sub [Stop]()
    mciSendString("stop audiofile", Nothing, 0, IntPtr.Zero)
End Sub

''' <summary>
''' Close the file.
''' </summary>
''' <remarks></remarks>
Public Sub Close()
    mciSendString("close audiofile", Nothing, 0, IntPtr.Zero)
End Sub

Private _wait As Boolean = False
''' <summary>
''' Halt the program until the .wav file is done playing.  Be careful, this will lock the entire program up until the
''' file is done playing.  It behaves as if the Windows Sleep API is called while the file is playing (and maybe it is, I don't
''' actually know, I'm just theorizing).  :P
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Wait() As Boolean
    Get
        Return _wait
    End Get
    Set(ByVal value As Boolean)
        _wait = value
    End Set
End Property

''' <summary>
''' Sets the audio file's time format via the mciSendString API.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
ReadOnly Property Milleseconds() As Integer
    Get
        Dim buf As String = Space(255)
        mciSendString("set audiofile time format milliseconds", Nothing, 0, IntPtr.Zero)
        mciSendString("status audiofile length", buf, 255, IntPtr.Zero)

        buf = Replace(buf, Chr(0), "") ' Get rid of the nulls, they muck things up

        If buf = "" Then
            Return 0
        Else
            Return CInt(buf)
        End If
    End Get
End Property

''' <summary>
''' Gets the status of the current playback file via the mciSendString API.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
ReadOnly Property Status() As String
    Get
        Dim buf As String = Space(255)
        mciSendString("status audiofile mode", buf, 255, IntPtr.Zero)
        buf = Replace(buf, Chr(0), "")  ' Get rid of the nulls, they muck things up
        Return buf
    End Get
End Property

''' <summary>
''' Gets the file size of the current audio file.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
ReadOnly Property FileSize() As Integer
    Get
        Try
            Return My.Computer.FileSystem.GetFileInfo(_filename).Length
        Catch ex As Exception
            Return 0
        End Try
    End Get
End Property

''' <summary>
''' Gets the channels of the file via the mciSendString API.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
ReadOnly Property Channels() As Integer
    Get
        Dim buf As String = Space(255)
        mciSendString("status audiofile channels", buf, 255, IntPtr.Zero)

        If IsNumeric(buf) = True Then
            Return CInt(buf)
        Else
            Return -1
        End If
    End Get
End Property

''' <summary>
''' Used for debugging purposes.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
ReadOnly Property Debug() As String
    Get
        Dim buf As String = Space(255)
        mciSendString("status audiofile channels", buf, 255, IntPtr.Zero)

        Return Str(buf)
    End Get
End Property

Private _isPaused As Boolean = False
''' <summary>
''' Whether or not the current playback is paused.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property IsPaused() As Boolean
    Get
        Return _isPaused
    End Get
    Set(ByVal value As Boolean)
        _isPaused = value
    End Set
End Property

Private _filename As String
''' <summary>
''' The current filename of the file that is to be played back.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Filename() As String
    Get
        Return _filename
    End Get
    Set(ByVal value As String)

        If My.Computer.FileSystem.FileExists(value) = False Then
            Throw New System.IO.FileNotFoundException
            Exit Property
        End If

        _filename = value
    End Set
End Property

结束课程

以下是上述类的一些用法示例:

    Dim audio As New AudioFile("C:\Temp\YourMP3File.mp3")
    audio.Play()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 VB2010 Windows 窗体开始时播放 .wav/.mp3 文件? 的相关文章

随机推荐

  • 线程“main”java.lang.UnsatisfiedLinkError中出现异常:java.library.path中没有opencv_java249

    我目前正在尝试在我的 32 位笔记本电脑上设置 OpenCV 但我不断收到一条令我困惑的错误消息 Exception in thread main java lang UnsatisfiedLinkError no opencv java2
  • 当 MediaElementAudioSource 输出为零但 CORS 不再是问题时,如何才能播放音频?

    我正在尝试实施
  • 使用Perl/DBI/MySQL/InnoDB查找外键信息

    我想以编程方式查找 MySQL 数据库中特定 InnoDB 表的外键 我正在使用 Perl 我偶然发现 dbh gt foreign key info 我刚刚尝试使用它 但似乎有点错误 它不会返回 ON DELETE 和 ON UPDATE
  • Gradle 守护进程在“完成作业”步骤中被终止

    我有一个 构建 管道 它在我的 java 代码上运行 gradlew build 然后将 jar 作为工件导出 它目前正在自托管代理上运行 因为我希望这将有助于加快编译速度 我注意到 在每次运行开始时 它都会说它正在启动一个新的守护进程 因
  • Firefox 忽略背景大小的 css

    尝试使用背景大小 CSS 规则缩小图像 但 Firefox 3 5 似乎会忽略该规则 CSS privatejoker background aqua url styles images home privatejoker png no r
  • 简单搜索:使用 CodeIgniter 将表单变量传递到 URI

    我的每个页面上都有一个搜索表单 如果我使用表单助手 它默认为 POST 我希望搜索词显示在 URI 中 http example com search KEYWORD 我已经在谷歌上搜索了大约一个小时 但没有结果 我只找到了有关如何进行的文
  • Visual Studio 2022 CMake 预设

    我在我的项目中使用 CMake 并开始探索 CMakePresets 的可能性 我设法创建了一个默认的 Windows 预设 目前我的 Windows 默认预设将 CMAKE BUILD TYPE 设置为调试 现在我想在左侧下拉列表中选择配
  • php - 未知:第 0 行需要打开失败。laravel 5.6

    我刚刚安装了 laracast flash 并通过 Composer 更新了 nesbot carbon 下载碳时命令发疯了 Cmd界面显示了一会界面上散落的文字和方框 下载完成 做过php artisan serve at localho
  • 列表初始化的缩小转换是错误还是只是警告? [复制]

    这个问题在这里已经有答案了 目前我正在自学C 入门第五版 文字说 当与内置类型的变量一起使用时 这种形式的初始化有一个 重要属性 编译器不会让我们列出内置类型的初始化变量 如果 初始化程序可能会导致信息丢失 这是示例代码 long doub
  • 如何更新中继存储而不推送到服务器

    我的 React Relay 应用程序中有一个表单 我用它来修改一些字段 我不想每次在输入中输入新字母时都发送服务器更新 如何使用 Relay 来支持应用程序状态而不总是推送到服务器 阅读了大部分 Relay 文档后 在我看来 我基本上必须
  • QByteArray 到整数

    正如您可能从标题中看出的那样 我在转换QByteArray为一个整数 QByteArray buffer server gt read 8192 QByteArray q size buffer mid 0 2 int size q siz
  • 如何在 Google App Engine 上部署 1 个实例

    我需要在 Google App Engine 上部署一个简单 Node js 应用程序的 1 个实例 无需任何形式的扩展 我试过做gcloud preview app deploy 但是即使在我尝试关闭它们之后 也会创建许多实例 我的目标是
  • 静态方法的 Java 内存模型

    我来自操作系统和 C 语言背景 在代码编译时 世界很简单 需要处理和理解堆栈 堆文本部分等 当我开始学习 Java 时 我确实了解 JVM 和垃圾收集器 我对静态方法感到很有趣 根据我的理解 类的所有实例都会在堆中创建 然后被清理 但是 对
  • 将 UNNEST 与 jOOQ 结合使用

    我正在使用 PostgreSQL 9 4 Spring Boot 1 3 2 和 jOOQ 3 7 我想 jOOQify 以下查询 SELECT id FROM users WHERE username IN SELECT FROM UNN
  • 设备旋转时的 SwiftUI 重绘视图组件

    如何在 SwiftUI 中检测设备旋转并重新绘制视图组件 当第一个出现时 我有一个 State 变量初始化为 UIScreen main bounds width 的值 但当设备方向改变时 该值不会改变 当用户更改设备方向时 我需要重新绘制
  • 参数编号无效:参数未在[重复]中定义

    这个问题在这里已经有答案了 被困在这里有一段时间了 当我尝试运行代码时收到此错误 警告 PDOStatement execute SQLSTATE HY093 无效参数 number 参数未定义于 Applications XAMPP xa
  • 检查图像文件是否存在,Robot-Framework,Selenium2Library

    我想知道是否可能以及如何检查应该显示图片的元素是否确实显示了图片 图片位于 img src 并且在同一域内 目前尚不完全清楚您的目标是什么 我认为可以安全地假设 如果您的代码执行正确的所有操作 即 URL 正确 并且 css 规则不会导致元
  • App Engine 上的 Django 与 webapp2 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 将 C# WCF 扩展性代码移至配置文件

    以下代码将 ParameterInspector 添加到端点 ChannelFactory
  • 在 VB2010 Windows 窗体开始时播放 .wav/.mp3 文件?

    制作 VB2010 已经大约一年了 最近开始突破我可以将哪种媒体合并到我的表单中的界限 但我无法播放 wav 或 mp3 文件 我尝试按照微软和其他编码网站上的教程进行操作 但没有成功 任何帮助 将不胜感激 要播放波形文件 您可以简单地使用