如何在 ios 上将 wav 音频转换为 mp3/ogg?

How to convert a wav audio to mp3/ogg on ios?

我想为 ios 制作一个 unity3d 应用程序,并且需要录制音频

参考:

我找到了 record audio 的方法。但是保存的音频格式是wav。我想要压缩音频格式,例如 ogg/mp3.

我也看这个question,但是它使用lame,我可以在ios上使用lame吗?

我认为有两种方式:

  1. 录制音频,并将其保存在ogg中,但我不知道如何在unity引擎上压缩来自麦克风的音频
  2. 像下面这样使用SaveWav,并将音频文件转换为ogg或mp3,是否有一些统一的库可以做到这一点?它在 ios 平台上运行良好吗?

暂时没有想法,希望大家多多指教!


P.S。 (20160425)

我试试这个库 NAudio.Lame

但是在unity engine中不能使用,请问如何让它支持unity engine和任何unity平台?或者其他解决方案?

还在等你的帮助!

我在 vs 中重建项目时出错

不仅有这个错误,还有很多其他错误,如何解决?

No matter master or experimental branch. One error is Severity Code Description Project File Line Suppression State Error CS0103 The name 'LibMp3Lame' does not exist in the current context NAudio.Lame \C#Projects\NAudio.Lame\MP3FileWriter.cs 636 Active

这是 master branch.

中的构建错误

关于 CopyTo

的错误

20160416

error CS1061: Type NAudio.Wave.WaveFileReader' does not contain a definition forCopyTo' and no extension method CopyTo' of type NAudio.Wave.WaveFileReader' could be found (are you missing a using directive or an assembly reference?)

你知道怎么解决吗?或者其他方法转换成 mp3 文件而不是下面的代码。

using System.IO;
using NAudio.Wave; 
using NAudio.Lame;

public static class Codec {
    // Convert WAV to MP3 using libmp3lame library
    public static void WaveToMP3(string waveFileName, string mp3FileName, int bitRate = 128)
    {
        using (var reader = new WaveFileReader(waveFileName))
        using (var writer = new LameMP3FileWriter(mp3FileName, reader.WaveFormat, bitRate))
            reader.CopyTo(writer);
    }

获取 NAudio.Lame 上的库并复制项目中的一个 dll。示例代码在源页面中提供。

.NET 4.0 之前不存在 CopyTo 方法。您可以像 answer 中那样编写一个扩展方法来实现它。只需将下面的代码复制到您项目中的某个位置。

public static class StreamExtensions
{
    public static void CopyTo(this Stream input, Stream output)
    {
        byte[] buffer = new byte[16 * 1024]; // Fairly arbitrary size
        int bytesRead;

        while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, bytesRead);
        }
    }
}

考虑到 NAudioNAudio.Lame 都以 .NET framework v4.0 为目标,而 Unity3D 以 .NET framework v2.0 为目标,肯定有一些东西根本不起作用。

如果您确实对它进行了排序,请随时在 GitHub 上分叉 NAudio.Lame 源代码并使用您正在使用的版本更新它。

我不能代表 NAudio 库,但如果你让它在 Unity 上运行,请告诉 Mark。