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("操作成功");
            }
  • 相关阅读:
    syslog日志格式解析
    Linux打补丁的一个简单例子
    Linux打补丁的一些问题
    安全漏洞整改解决方案(很不错网络文章)
    Linux系统启动过程
    chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息
    主机名/etc/hosts文件的作用
    Linux中如何配置IP相关文件
    /bin、/sbin、/usr/bin、/usr/sbin目录Linux执行文档的区别
    日志生成控制文件syslog.conf
  • 原文地址:https://www.cnblogs.com/wzwyc/p/7510189.html
Copyright © 2011-2022 走看看