zoukankan      html  css  js  c++  java
  • 使用NAudio实现Wav转Mp3

    转换成MP3:

    using Microsoft.Win32;
    using NAudio.MediaFoundation;
    using NAudio.Wave;
    using System.Windows;
    
    namespace NAudioDemo
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "WAV Files (*.wav)|*.wav|All Files (*.*)|*.*";
                openFileDialog.FilterIndex = 1;
                if (openFileDialog.ShowDialog() == true)
                {
                    var inputFileName = openFileDialog.FileName;
                    var outputFileName = inputFileName.Substring(0, inputFileName.Length - 3) + "mp3";
    
                    var mediaType = MediaFoundationEncoder.SelectMediaType(
                                        AudioSubtypes.MFAudioFormat_MP3,
                                        new WaveFormat(44100, 1),
                                        0);
    
                    using (var reader = new MediaFoundationReader(inputFileName))
                    {
                        using (var encoder = new MediaFoundationEncoder(mediaType))
                        {
                            encoder.Encode(outputFileName, reader);
                        }
                    }
                }
                MessageBox.Show("操作成功");
            }
        }
    }

    转换成WMA:

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "WAV Files (*.wav)|*.wav|All Files (*.*)|*.*";
                openFileDialog.FilterIndex = 1;
                if (openFileDialog.ShowDialog() == true)
                {
                    var inputFileName = openFileDialog.FileName;
                    var outputFileName = inputFileName.Substring(0, inputFileName.Length - 3) + "wma";
    
                    var mediaType = MediaFoundationEncoder.SelectMediaType(
                                        AudioSubtypes.MFAudioFormat_WMAudioV8,
                                        new WaveFormat(16000, 1),
                                        16000);
    
                    using (var reader = new MediaFoundationReader(inputFileName))
                    {
                        using (var encoder = new MediaFoundationEncoder(mediaType))
                        {
                            encoder.Encode(outputFileName, reader);
                        }
                    }
                }
                MessageBox.Show("操作成功");
            }
  • 相关阅读:
    NFine框架JqGrid导出选中行为Excel实现方法
    NFine框架全选checkBox列错位
    VS 在文件中查找替换界面死掉。
    WCF各个Service之间共享数据
    Devexpress Winform 使用MVVM
    FontAwesome图标选择器
    Xampp PHPStorm XDebug配置
    SDL 库 无法解析的外部符号 __imp__fprintf
    ffmpeg mp4转yuv
    JAVA环境变量配置
  • 原文地址:https://www.cnblogs.com/wzwyc/p/7510189.html
Copyright © 2011-2022 走看看