Problem / Error with converting M4A Audio File to MP3 using NAudio in C#

0

I'm in a process of creating a demo for an audio conversion software that I create as a hobby. I wanted converting an M4A (MPEG-4) audio file to an MP3 using a simple GUI for the user to interact. I also implemented a feature that allows the user to play the selected audio file (M4A) before conversion, which I have learned from watching NAudio video tutorials. However, I was stuck with a specific issue that stops me from progressing this demo project.

I have received an error right after the SaveFileDialogue selection to place the converted file (MP3) which prevents me from continuing the process. (I have put the error message in the comments for all of you to see in the method of btnConvertToMp3_Click()).

I have tried using MediaFoundationEncoder.EncodeToMp3() to solve this case. Unfortunately, I get an error, stating that there are "No available MP3 encoders" which I have researched that it is not fully supported enough to be used functionally for every audio format.

I have tried searching for more solutions, close to this problem but sadly could not find what I was hoping for, so I decided to ask if you have any input about this problem.

Here is my current code:

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;
                }
            }
        }
    }
}

If there isn't a suitable solution to this error, is there a possible alternative or simple solution that would allow me to convert M4A to MP3 without using NAudio, even with GUI?

c#
audio
naudio
asked on Stack Overflow Aug 2, 2020 by user14037230

1 Answer

0

A simple approach without using NAudio (which relies on Windows codecs) is to use FFMPEG, and do a command line conversion.

answered on Stack Overflow Aug 17, 2020 by Mark Heath

User contributions licensed under CC BY-SA 3.0