2

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!


P.S. (20160425)

I try this lib NAudio.Lame. lame-naudio

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

enter image description here this is build error in master branch.

Error About 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?)

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);
    }
Community
  • 1
  • 1
tim
  • 1,454
  • 1
  • 25
  • 45
  • @GökhanKurt Thank you man, It worked, how long have you been using c#/.net? You save me much time. Thank you, And it seemed that I need to learn more about c#, not just unity engine. Do you have some advice about learning c#/.net? – tim Apr 26 '16 at 08:35
  • I am just used to seeing constraints of Unity. There is no advice to give. With time and experience you just learn. Also if you want to learn what others know, Google is your friend. Just correct keywords do the job. – Gokhan Kurt Apr 26 '16 at 08:45

2 Answers2

3

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);
        }
    }
}
Community
  • 1
  • 1
Gokhan Kurt
  • 8,239
  • 1
  • 27
  • 51
  • Thanks for you, I will test in Monday. – tim Apr 22 '16 at 14:30
  • Yeah, I download it, and I found I cannot use the `using NAudio.Wave` or `using NAudio.Lame` like the example codes in unity engine. Do you know how to fix it? I have no ideas. Could you help me? – tim Apr 25 '16 at 07:56
  • I think you should add it in plugins folder. Plugins folder should be in top level of asset folder and should be named "Plugins". Another problem that may arise is that NAudio might be built in .NET 4.0 or .NET 4.5 . In that case you should download the source code and build it yourself to generate the dlls. Visual Studio lets you build the code for Unity easily if you have Unity Tools for Visual Studio. – Gokhan Kurt Apr 25 '16 at 08:02
  • I drag the dll file `libmp3lame` to the `Plugins` folder. It still does not work. I am tring to rebuild dll plugin. – tim Apr 25 '16 at 08:08
  • still not work, when I build it in vs, there is some errors showing. the error I paste it to question. – tim Apr 25 '16 at 08:31
  • I misunderstood something. The dll to be included in Unity is not libmp3lame.dll. You should build the source code. To build it, download as zip, open csproj, save solution, wait for nuget to finish, build as release. There will be 2 dll outputs. Copy them to Unity and after that you can use the library. For encoding to actually work, you may have to include libmp3lame.dll in your project as well. – Gokhan Kurt Apr 25 '16 at 08:41
  • I update the question to add the build error. I truly build as release – tim Apr 25 '16 at 08:52
  • 1
    Don't start debugging, simply rebuild the project. Try to reload Nuget packages. Make sure you save solution before building. – Gokhan Kurt Apr 25 '16 at 10:13
  • fixed after update naduio by nuget. thanks for your help. I will test it on the phone. – tim Apr 25 '16 at 10:15
  • There is still a noisy error, **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?)** – tim Apr 26 '16 at 06:15
  • 1
    Seems like CopyTo is added in .NET 4.0. You can add an extension method to implement CopyTo. Editing answer to include CopyTo extension. – Gokhan Kurt Apr 26 '16 at 07:36
  • It seemed that the dll does not work on android. when I publish the application to android platform. – tim Apr 26 '16 at 10:24
1

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.

Corey
  • 15,524
  • 2
  • 35
  • 68
  • it work on unity. But when I test it on android, it seemed it did not work. I did not test it on ios for now. – tim Apr 26 '16 at 10:23
  • Yeah, it won't work on anything that doesn't know how to load native Windows DLLs because it uses the LAME native DLLs. – Corey Apr 26 '16 at 11:59