C# 当mp3文件的音量低于阈值时,立即停止播放mp3文件

C# 当mp3文件的音量低于阈值时,立即停止播放mp3文件,c#,audio,naudio,C#,Audio,Naudio,我想用c#播放mp3文件。表单应具有开始和停止按钮 当用户单击停止按钮时,播放不会立即停止,而是当当前播放音量低于某个阈值时,换句话说,当mp3文件在给定的最短时间内出现“静音”时 这样做的有效方法是什么 我需要知道 要使用哪个dll/导入来播放mp3文件 如何使用“1”中的同一dll获取正在播放的文件的当前音量。作为整数、浮点或双精度 有了这两个问题的答案,我可以自己继续 PS:我不想知道沉默在文件中的一般位置。我想要一个函数,告诉我现在是否有静默。因此,我将一些流的字节和阈值传递给它,它返回

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

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

这样做的有效方法是什么

我需要知道

  • 要使用哪个dll/导入来播放mp3文件
  • 如何使用“1”中的同一dll获取正在播放的文件的当前音量。作为整数、浮点或双精度
  • 有了这两个问题的答案,我可以自己继续


    PS:我不想知道沉默在文件中的一般位置。我想要一个函数,告诉我现在是否有静默。因此,我将一些流的字节和阈值传递给它,它返回true或false。

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

    由于有问题的更改而进行编辑:

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

    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
    
    Public Sub GetSilenceLength(ByVal文件为字符串,ByVal阈值为长,ByRef startp为长,ByRef endp为长)
    Dim buf(50000)为整数
    变暗计数为长,pos为长
    陈丹青
    把a调长,b调长
    尺寸c等于长,d等于长
    计数=0
    chan=BASS_StreamCreateFile(BASSFALSE,file,0,0,BASS_STREAM_DECODE)'创建解码通道
    如果(chan=0),则退出子系统
    做
    b=BASS_ChannelGetData(chan,buf(0),20000)”解码一些数据
    b=b/2'字节->样本
    a=0
    不要计算沉默的样本
    a=a+1
    循环时间((a样本
    c=d
    做
    c=c-1'计数静默样本
    循环时间(c>0)和(Abs(buf(c))计数)
    endp=计数
    BASS_StreamFree(chan)
    端接头
    

    另外,如果您想要淡入淡出,这是另一个简单的故事。

    您没有指定要使用什么来播放MP3。但我正在用它。你的问题是在他们的论坛上提出的。在这里顺便说一句,您可能需要一个.NET BASS包装器来将BASS与C#一起使用

    由于有问题的更改而进行编辑:

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

    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
    
    Public Sub GetSilenceLength(ByVal文件为字符串,ByVal阈值为长,ByRef startp为长,ByRef endp为长)
    Dim buf(50000)为整数
    变暗计数为长,pos为长
    陈丹青
    把a调长,b调长
    尺寸c等于长,d等于长
    计数=0
    chan=BASS_StreamCreateFile(BASSFALSE,file,0,0,BASS_STREAM_DECODE)'创建解码通道
    如果(chan=0),则退出子系统
    做
    b=BASS_ChannelGetData(chan,buf(0),20000)”解码一些数据
    b=b/2'字节->样本
    a=0
    不要计算沉默的样本
    a=a+1
    循环时间((a样本
    c=d
    做
    c=c-1'计数静默样本
    循环时间(c>0)和(Abs(buf(c))计数)
    endp=计数
    BASS_StreamFree(chan)
    端接头
    

    另外,如果你想淡出,这是另一个简单的故事。

    我不想知道沉默在文件中的一般位置。我想要一个函数,告诉我现在是否有静默。所以我把一些流的字节和一个阈值传递给它,它返回true或false。我一般不想知道静默在文件中的位置。我想要一个函数,告诉我现在是否有静默。所以我将一些流的字节传递给它和一个阈值,它返回true或false。