使用C#中的NAudio将M4A音频文件转换为MP3时出现问题/错误



我正在为我作为业余爱好创建的音频转换软件创建一个演示。我想使用一个简单的GUI将M4A(MPEG-4(音频文件转换为MP3,供用户交互。我还实现了一个功能,允许用户在转换前播放选定的音频文件(M4A(,这是我从观看NAudio视频教程中学到的。然而,我遇到了一个特定的问题,这个问题阻止了我进行这个演示项目。

我在选择SaveFileDialogue后立即收到一个错误,用于放置转换后的文件(MP3(,这使我无法继续该过程。(我已经把错误消息放在注释中,让大家在btnCovertToMp3_Click((的方法中看到(。

我尝试过使用MediaFoundationEncoder.EncodeToMp3((来解决这个问题。不幸的是,我得到了一个错误,指出有";没有可用的MP3编码器";我已经研究过,它没有得到足够的充分支持,无法在功能上用于每种音频格式。

我试着寻找更多接近这个问题的解决方案,但遗憾的是找不到我所希望的,所以我决定问你是否对这个问题有任何意见。

这是我当前的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NAudio.Wave;
using NAudio;
using NAudio.Lame;
using NAudio.Dmo;
using System.Windows.Forms;
namespace M4AtoMP3ConversionDEMO
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private NAudio.Wave.BlockAlignReductionStream stream = null;
private NAudio.Wave.DirectSoundOut output = null;
public void btnOpenM4AFile_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "M4A Audio File (*.m4a)|*.m4a;";
if (open.ShowDialog() != DialogResult.OK)
{
return;
}
MediaFoundationReader mfM4A = new MediaFoundationReader(open.FileName);
stream = new NAudio.Wave.BlockAlignReductionStream(mfM4A);
btnPlayPauseButton.Enabled = true;
btnConvertToMP3.Visible = true;
output = new NAudio.Wave.DirectSoundOut();
output.Init(mfM4A);
output.Play();
MessageBox.Show(".M4A Format has been accepted", ".M4A Audio Format", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void btnConvertToMP3_Click(object sender, EventArgs e)
{
DialogResult mb1 = MessageBox.Show("Are you sure you want to convert this M4A File?", "Converting to MP3", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
switch (mb1)
{
case DialogResult.Yes:
SaveFileDialog save = new SaveFileDialog();
save.Filter = "MP3 File (*.mp3)|*.mp3";
if (save.ShowDialog() != DialogResult.OK) return;
// Unable to cast COM object of type 'System.__ComObject' to interface type 'NAudio.MediaFoundation.IMFSourceReader'.
// This operation failed because the QueryInterface call on the COM component for the interface with IID '{70AE66F2-C809-4E4F-8915-BDCB406B7993}' failed due to the following error:
// No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))
using (var mp3FileReader = new LameMP3FileWriter(save.FileName, stream.WaveFormat, LAMEPreset.ABR_320))
{
stream.CopyTo(mp3FileReader);
}
break;
case DialogResult.No:
break;
}
}
private void btnPlayPauseButton_Click(object sender, EventArgs e)
{
if (output != null)
{
if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing)
{
output.Pause();
Image playButton = new Bitmap(@"C:UsersCeXsourcereposAudioConverterUI-WorkInProgressAudioConverterUI-WorkInProgressResourcesplay-button-size.png");
btnPlayPauseButton.Image = playButton;
}
else if (output.PlaybackState == NAudio.Wave.PlaybackState.Paused)
{
output.Play();
Image pauseButton = new Bitmap(@"C:UsersCeXsourcereposAudioConverterUI-WorkInProgressAudioConverterUI-WorkInProgressResourcespause-button-size.png");
btnPlayPauseButton.Image = pauseButton;
}
}
}
private void DisposeWave()
{
if (output != null)
{
if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing)
{
output.Stop();
output.Dispose();
output = null;
}
if (stream != null)
{
stream.Dispose();
stream = null;
}
}
}
}
}

如果没有合适的解决方案来解决这个错误,有没有一个可能的替代方案或简单的解决方案可以让我在不使用NAudio的情况下将M4A转换为MP3,即使是使用GUI?

在不使用NAudio(依赖于Windows编解码器(的情况下,一种简单的方法是使用FFMPEG,并进行命令行转换。

最新更新