【问题标题】:Stop playback of an mp3 file as soon as the volume level of the mp3 file goes below a threshold一旦 mp3 文件的音量低于阈值,就停止播放 mp3 文件
【发布时间】:2012-10-18 08:20:39
【问题描述】:

我想用 c# 播放一个 mp3 文件。表单应该有一个开始和停止按钮。

当用户单击停止按钮时,播放不会立即停止,而是当当前播放音量低于某个阈值时,即当 mp3 - 文件在给定的最短时间内出现“静音”时。

什么是执行此操作的有效方法?

我需要知道

  1. 使用哪个 dll/import 来播放 mp3 文件
  2. 如何使用“1”中的相同 dll 获取正在播放的文件的当前音量级别。 作为整数、浮点数或双精度数

有了这两个问题的答案,我可以自己继续。

PS:我不想知道一般文件中的静音在哪里。我想要一个函数来告诉我现在是否有沉默。所以我将一些流的字节传递给它和一个阈值,它返回真或假。

【问题讨论】:

    标签: c# audio naudio


    【解决方案1】:

    您没有指定用于播放 MP3 的内容。但我正在使用BASS。您的问题是在他们的论坛中提出的。这里it is。顺便说一句,您可能需要 BASS .NET,它是 BASS 的 .NET 包装器,才能将 BASS 与 C# 一起使用。

    因问题更改而编辑:

    您可以在我上面给出的链接中使用 bass.dll。下载 .NET 包装器,将其添加到您的引用中。这是VB 6中的示例。只需将longs更改为整数,将整数更改为shorts,函数名称相同。你应该从这里得到想法。

    Public Sub GetSilenceLength(ByVal file As String, ByVal threshold As Long, ByRef startp As Long, ByRef endp As Long)
       Dim buf(50000) As Integer
       Dim count As Long, pos As Long
       Dim chan As Long
       Dim a As Long, b As Long
       Dim c As Long, d As Long
       count = 0
    
       chan = BASS_StreamCreateFile(BASSFALSE, file, 0, 0, BASS_STREAM_DECODE) 'create decoding channel
    
       If (chan = 0) Then Exit Sub
    
       Do
             b = BASS_ChannelGetData(chan, buf(0), 20000) 'decode some data
             b = b / 2 'bytes -> samples
             a = 0
             Do      'count silent samples
                   a = a + 1
             Loop While ((a < b) And (Abs(buf(a)) <= threshold))
             count = count + (a * 2)
             If (a < b) Then 'sound has bagun
                   'move back to a quieter sample (to avoid "click")
                   Do
                         a = a - 1
                         count = count - 2
                   Loop While ((a) And (Abs(buf(a)) > threshold / 4))
                   Exit Do
             End If
       Loop While (BASS_ChannelIsActive(chan))
    
       startp = count
    
       pos = BASS_StreamGetLength(chan)
       Do
             pos = IIf(pos < 100000, 0, pos - 100000) 'step back a bit
             BASS_ChannelSetPosition chan, pos
             d = BASS_ChannelGetData(chan, buf(0), 100000) ' decode some data
             d = d / 2 'bytes -> samples
             c = d
             Do
                   c = c - 1  'count silent samples
             Loop While (c > 0) And (Abs(buf(c)) <= threshold / 2) 'Here is the correction
             If (c > 0) Then   'sound has begun
                   count = pos + c * 2
                   Exit Do
             End If
       Loop While (pos > count)
       endp = count
       BASS_StreamFree (chan)
    End Sub
    

    另外,如果你想褪色,那是另一个简单的故事。

    【讨论】:

    • 我不想知道一般文件中的静音在哪里。我想要一个函数来告诉我现在是否有沉默。所以我将一些流的字节传递给它和一个阈值,它返回真或假。
    猜你喜欢
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    相关资源
    最近更新 更多