处理vb.net后naudio没有发布文件,我认为这真的是一个声明问题

问题描述 投票:0回答:1

当我真的被卡住时,我才会在这里发帖,所以这里就是。

我正在创建一个程序,我可以使用taglib-sharp编辑MP3文件的元数据(所有数据都来自mysql数据库)。如果我使用naudio预览音频文件,我可以听到音频没有问题,我可以阻止它,所以这一切都很好。为了我的代码中发生的事情,这是从列表框中选择一个文件,然后我按下“播放”按钮开始,另一个按钮停止。当我尝试保存元数据时,我的错误显示右键单击上下文菜单。

问题是当我去编辑我刚播放的同一文件的元数据时,会出现错误 - 如下面的代码示例所示。当我尝试调用mp3.save()行时,收到以下错误:

**************异常文本**************

System.IO.IOException:进程无法访问文件'C:\ temp \ test.mp3',因为它正由另一个进程使用。在System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath) at System.IO.FileStream.Init(String path,FileMode mode,FileAccess access,Int32 rights,Boolean useRights,FileShare share,Int32 bufferSize,FileOptions options,SECURITY_ATTRIBUTES secAttrs,String msgPath,Boolean bFromProxy,Boolean useLongPath,Boolean checkHost) 在TagLib.NileContainer.File.Save的TagLib.File.set_Mode(AccessMode值)处的TagLib.File.LocalFileAbstraction.get_WriteStream()处的System.IO.FileStream..ctor(字符串路径,FileMode模式,FileAccess访问,FileShare共享) ()位于C:\ Users \ smonro \ source \ repos \ WindowsApp1 \ WindowsApp1 \ Form1.vb中的LinersDB_META.Form1.menuChoice(Object sender,EventArgs e):System.Windows.Forms.ToolStripItem.RaiseEvent(Object key)中的第2281行,EventArgs e)位于System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)的System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)的System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e) System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)上的System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) 在System.Windows.Forms.Control.WmMouseUp(Message&m,MouseButtons按钮,Int32单击)处System.Windows.Forms.Control.WndProc(Message&m)处于System.Windows.Forms.ToolStrip.WndProc(Message&m)at System System.Windows.Forms.NativeWindow.Callback中的.Windows.Forms.ToolStripDropDown.WndProc(Message&m)(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

等等

我知道它为什么会发生,但我迷失了如何解决它。

根据这个page,(在C#中)我需要发布“Mp3FileReader”。

在我的情况下,它将是“数据” - 这:昏暗的数据作为新的NAudio.Wave.Mp3FileReader(文件)

问题是,它在私有子中,作为私有变量。

但是,当我需要声明文件时,我很难看到如何公开这个声明,但是它也需要知道,并且每次选择不同的音频文件时都要重新初始化。

有什么想法吗?

我应该尝试重新安排我的程序以不同的方式工作吗?提前致谢。

'...

Imports System.IO 'File Operations
Imports TagLib ' Tagging
Imports NAudio
'...

    'Wave out device for playing the sound
    Public Shared Wave1 As New NAudio.Wave.WaveOut 'Wave out device for playing the sound    

    Public Sub start_audio()

        Dim file As String = "C:\test\test.mp3"

        If IO.File.Exists(file) Then
            Dim data As New NAudio.Wave.Mp3FileReader(file)
            Wave1.Init(data)
            Wave1.Play()
        End If
    End Sub

     Public Sub Button5_Click_1(sender As Object, e As EventArgs) Handles Button5.Click        
        Wave1.Stop()        
    End Sub

    Public Sub menuChoice(ByVal sender As Object, ByVal e As EventArgs)
        Dim item = CType(sender, ToolStripMenuItem)
        Dim selection = CInt(item.Tag)        
        Select Case selection
            Case 1
                ' Remove the Artwork
                'Insert Album art and save to file and dispose.
                If IO.File.Exists(GLOBAL_Full_Path) Then

                    Wave1.Stop()
                    Wave1.Dispose()

                    System.GC.Collect()
                    System.GC.WaitForPendingFinalizers()

                    Dim mp3 As TagLib.File = TagLib.File.Create(GLOBAL_Full_Path)

                    ' Clear the artwork from the file.. (It's a big process)
                    Dim pics As Picture = New Picture()
                    Dim picsFrame As New TagLib.Id3v2.AttachedPictureFrame(pics)
                    picsFrame.MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg
                    'set the type of picture (front cover)
                    picsFrame.Type = TagLib.PictureType.FrontCover
                    picsFrame.Description = "" ' So the Actual location of the image is not stored in the file. This would otherwise reveal internal systems to the public.
                    'Id3v2 allows more than one type of image, just one is needed here.
                    Dim pictFrames() As TagLib.IPicture = {picsFrame}
                    'set the pictures in the tag
                    mp3.Tag.Pictures = pictFrames

                    ' ******************************************
                    ' Error occurs here after a file has been played.
                    mp3.Save()
                    ' Error occurs here                 
                    ' ******************************************

                    mp3.Dispose()
                End If
            Case Else
        End Select
    End Sub             
vb.net metadata mp3 naudio
1个回答
0
投票

在@TnTinMn指出我正确的方向后,我最终解决了这个问题。虽然这不是完整的例子,但我现在通过添加和更改一些小问题来解决这个问题。

Imports NAudio
' NEW/required to get the Mp3FileReader, so it can be dynamically populated.
Imports NAudio.Wave 

Dim WavePlayOut As New NAudio.Wave.WaveOut 'Wave out device for playing the sound
 Dim WaveFile As String = "" ' *** NEW/Declared ***
 Dim WaveData As Mp3FileReader ' *** Changed ***

 Public Sub start_audio()

    If File_ListView.SelectedItems.Count > 0 Then
        WaveFile = TxtBx_Path.Text & "\" & File_ListView.FocusedItem.Text
        If WavePlayOut IsNot Nothing Then
            WavePlayOut = New NAudio.Wave.WaveOut
        End If
        If WaveData IsNot Nothing Then
            WaveData.Dispose()
            WaveData = New Mp3FileReader(WaveFile)
            If WaveData.Length > 1 Then
                If IO.File.Exists(WaveFile) Then
                    WavePlayOut.Init(WaveData)
                    WavePlayOut.Play()
                End If
            End If
        Else
            WaveData = New Mp3FileReader(WaveFile)
            If WaveData.Length > 1 Then
                If IO.File.Exists(WaveFile) Then
                    WavePlayOut.Init(WaveData)
                    WavePlayOut.Play()
                End If
            End If
        End If
    End If
End Sub

要卸载音频文件,请使用以下命令:

If WaveData IsNot Nothing Then
     WaveData.Dispose()
End If
If WavePlayOut IsNot Nothing Then
     WavePlayOut.Stop()
     WavePlayOut.Dispose()
End If
© www.soinside.com 2019 - 2024. All rights reserved.