Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我能';我不明白为什么这段代码在与后台工作程序一起使用时会失败_C#_Audio_Backgroundworker - Fatal编程技术网

C# 我能';我不明白为什么这段代码在与后台工作程序一起使用时会失败

C# 我能';我不明白为什么这段代码在与后台工作程序一起使用时会失败,c#,audio,backgroundworker,C#,Audio,Backgroundworker,我正在使用Alvas音频库将文件从Mp3转换为Wave格式,以便编辑它们,然后将它们转换回Mp3。转换成Wave很好,但是当我试图转换回Mp3时,我遇到了麻烦。出于某种原因,此失败与我使用后台工作程序执行初始转换有关 我知道如果没有库的源代码,就很难尝试找出到底发生了什么,但我希望有人能对可能出现的问题提出建议 当我在没有后台工作程序的情况下同步调用同一代码时,它工作得非常好。有什么想法吗 下面是我从后台工作程序调用以执行转换的代码: public Tuple<float, flo

我正在使用Alvas音频库将文件从Mp3转换为Wave格式,以便编辑它们,然后将它们转换回Mp3。转换成Wave很好,但是当我试图转换回Mp3时,我遇到了麻烦。出于某种原因,此失败与我使用后台工作程序执行初始转换有关

我知道如果没有库的源代码,就很难尝试找出到底发生了什么,但我希望有人能对可能出现的问题提出建议

当我在没有后台工作程序的情况下同步调用同一代码时,它工作得非常好。有什么想法吗

下面是我从后台工作程序调用以执行转换的代码:

    public Tuple<float, float> convertMp3ToWav(Track track) //with Detection, duration check, and TODO: silence removal
    {
        try
        {
            string baseFile = Path.GetFileName(track.location);
            ////////////////////////////
            //string baseFile = track.location.Remove(track.location.Length - 4);
            string outputFile = directory + "Temp\\" + baseFile.Remove(baseFile.Length - 4) + ".wav";
            cleanupFiles.Add(outputFile);
            if (!File.Exists(outputFile))
            {
                int soundStart = -1;
                int soundEnd = 0;
                Mp3Reader mr = new Mp3Reader(File.OpenRead(track.location));
                IntPtr mp3Format = mr.ReadFormat();
                IntPtr pcmFormat = AudioCompressionManager.GetCompatibleFormat(mp3Format, AudioCompressionManager.PcmFormatTag);
                AcmConverter acm = new AcmConverter(mp3Format, pcmFormat, false);

                int sec = 1024;
                int i = 0;

                bool soundFound = false;
                while (true)
                {
                    byte[] mp3Data = mr.ReadDataInBytes(i, sec);
                    if (mp3Data.Length == 0)
                    {
                        break;
                    }
                    byte[] pcmData = acm.Convert(mp3Data);
                    foreach (byte d in pcmData) //THIS SECTION CHECKS IF THE Section in question has silence
                    {
                        if (d != 0)
                        {
                            soundFound = true;
                        }

                    }
                    if ((soundStart == -1) && (soundFound == true)) //if no beginning sound has been found yet, but has now been found
                    {
                        soundStart = i; //This will be precise to whatever value of sec has been chosen
                    }
                    else if ((soundStart != -1) && (soundFound == true)) //this is a possible end value
                    {
                        soundEnd = i;   //this value will be rewritten each time there is sound found after soundstart is set.
                                        //so this value will remain the same if no further sound is found in the track, and will represent the 
                                        //end of sound in the track
                    }

                    soundFound = false;
                    i += sec;
                }
                int finalDuration = soundEnd - soundStart;
                mr.Close();
                Mp3Reader reader = new Mp3Reader(File.OpenRead(track.location));
                IntPtr thisFormat = reader.ReadFormat();
                byte[] completeTrack = reader.ReadDataInBytes(soundStart, finalDuration);
                byte[] convertedTrack = AudioCompressionManager.Convert(thisFormat, pcmFormat, completeTrack, false);
                WaveWriter ww = new WaveWriter(File.OpenWrite(outputFile), AudioCompressionManager.FormatBytes(pcmFormat));
                ww.WriteData(convertedTrack);
                ww.Close();
                reader.Close();

                float bpm = performBeatDetection(track);
                float duration = getTrackDuration(track);

                return new Tuple<float, float>(bpm, duration);
            }
            else
            {
                //File already exists, just remove silence, get bpm and duration
                //string silenceRemoved = removeSilenceFromTrack(outputFile);
                float bpm = performBeatDetection(track);
                float duration = getTrackDuration(track);
                return new Tuple<float, float>(bpm, duration);
            }
        }
        catch (Alvas.Audio.AudioException e)
        {
            MessageBox.Show("ERROR: " + e.ToString());
            return new Tuple<float, float>(0f, 0f);
        }
    }
然后调用静态方法进行转换

AudioCompressionManager.GetCompatibleFormat(oldFormat, newFormatTag);
当我在使用后台工作程序后调用最后一个方法时,它抛出了一个错误英语的异常“it is notsuccessed to format conversion”。在库中使用稍微不同的方法:

AudioCompressionManager.GetCompatibleFormatList(oldFormat);
显示当我不使用后台工作程序并且使用后一种方法(GetCompatibleFormatList)时,它返回51个结果,其中一个结果是转换为我想要的mp3格式


如果我在使用后台工作程序执行初始转换后执行相同的方法,它只返回20个结果,其中没有一个是兼容的mp3格式。

如果不定义“麻烦”,则很难具体说明。但也许这些想法会有所帮助:

  • 后台工作人员使用的任何数据(或文件)是否被前台线程触及?(例如,
    跟踪
    参数在使用过程中是否会以任何方式被处理或修改?)

  • 后台线程运行时,前台线程是否调用任何库API?您的任何代码是否在多个线程上调用库代码?前台线程是否正确等待后台线程完成结果?(例如,作为一个测试,试着让你的前台线程加入后台线程(或者做一个繁忙的等待/睡眠),这样它就可以在执行处理时简单地等待-问题会消失吗?)

  • 库是否执行了可能需要在特定线程上调用它的任何操作?特别是,它是否需要在UI线程中运行以更新进度条或类似的操作?(这包括在代码中调用可能需要跨UI线程调用的事件处理程序)

  • 除了返回类型之外,是否还有其他数据传递回主线程?如果是,您是否使用同步(锁或易失性等)来确保读取正确(最新)的值

  • 您确定在处理方法返回之前,所有数据都已写入、刷新到磁盘,并且所有文件都已关闭吗


什么是“失败”?我不知道这是否解决了问题,但您应该在using语句中使用所有`
IDisposable
类型。我确信像
Mp3Reader
这样的类是一次性的。这样做的好处是,在后台工作人员留下所有文件并关闭所有句柄之后。我正在对我的所有IDisPobles调用Close(),这不是一回事吗?感谢您的建议,我今晚将仔细检查这些文件,看看是否有什么可以解决问题。
AudioCompressionManager.GetCompatibleFormatList(oldFormat);