【问题标题】:Problem / Error with converting M4A Audio File to MP3 using NAudio in C#使用 C# 中的 NAudio 将 M4A 音频文件转换为 MP3 的问题/错误
【发布时间】:2020-08-02 16:56:13
【问题描述】:

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

我在选择 SaveFileDialogue 以放置转换后的文件 (MP3) 后立即收到错误消息,这使我无法继续该过程。 (我已经把错误信息放在了cmets中,让大家在btnConvertToMp3_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:\Users\CeX\source\repos\AudioConverterUI-WorkInProgress\AudioConverterUI-WorkInProgress\Resources\play-button-size.png");
                    btnPlayPauseButton.Image = playButton;
                }

                else if (output.PlaybackState == NAudio.Wave.PlaybackState.Paused)
                {
                    output.Play();

                    Image pauseButton = new Bitmap(@"C:\Users\CeX\source\repos\AudioConverterUI-WorkInProgress\AudioConverterUI-WorkInProgress\Resources\pause-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?

【问题讨论】:

    标签: c# audio naudio


    【解决方案1】:

    不使用 NAudio(依赖于 Windows 编解码器)的简单方法是使用 FFMPEG,然后执行 command line conversion

    【讨论】:

    • 感谢 Mark Heath 提供了类似的信息。如果可能的话,我会尝试在 GUI 上实现 FFMPEG 命令行,当然,还要了解 FFMPEG 本身。
    猜你喜欢
    • 1970-01-01
    • 2019-01-07
    • 2021-08-15
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多