简体   繁体   中英

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

I want to make a unity3d appllication for ios, and need to record audio .

Ref:

I found a way to record audio . But the saving audio format is wav . I want a compressed audio format, like ogg/mp3 .

And I watch this question too, but it use lame , can I use lame on ios?

I thinks there are 2 ways:

  1. record audio, and save it in ogg, but I do not know how to compress audio from microphone on unity engine
  2. use SaveWav like the below, and convert the audio file to ogg or mp3, is there some library for unity to do this? And does it work well on ios platform?

I have no ideas now, hope your help!


PS (20160425)

I try this lib NAudio.Lame . 跛脚n音讯

But it cannot be used in unity engine, do you know how to make it support unity engine and any platforms for unity? Or other solutions?

Still wait for your help!

Error When I rebuid project in vs

There is not only this error, but many other errors too, how to fix it?

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

在此处输入图片说明 this is build error in master branch .

Error About CopyTo

20160416

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

Do you know how to fix it? Or other method to convert to mp3 file instead of the below codes.

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

Get the library on NAudio.Lame and copy one of the dll's in your project. Example code is provided in the source page.

CopyTo method doesn't exist before .NET 4.0. You can write an extension method as in this answer to implement it. Simply copy the code below to somewhere in your project.

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

Considering that both NAudio and NAudio.Lame target .NET framework v4.0 and Unity3D targets .NET framework v2.0 there are bound to be several things that simply don't work.

If you do ever get it sorted, feel free to fork the NAudio.Lame source on GitHub and update it with the version that you've got working.

I can't speak for the NAudio library, but if you get it working on Unity let Mark know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM