Android recording MP3 format files

preface

Recently, I worked on an instant messaging project. To ensure the universality of PC end, IOS end and Android end, it is finally unified into MP3 format. I have been worried about whether MP3 format will be very large, but the actual measurement is still acceptable. Let's take a look at the specific steps:

tool

MP3 format is transferred from an open source project, mp3lame. Since JNI is used in this project, you need to configure the NDK environment. I won't say much about the environment configuration here. You can baidu by yourself. The latest one should be well configured.

Create JNI

Copy file

After downloading (I downloaded version 3.98.4), open it, find the libmp3lame file, copy the. H and. C in it, create a JNI folder in your own project, create a new folder under the JNI folder (my name is lame-3.98.4_libmp3lame, which will be used later), and copy the file just copied, Then copy lame. H in the include folder.

Create android.mk

Create a file in JNI, android.mk

**Note: * * lame_ LIBMP3_ DIR := lame-3.98.4_ Libmp3lame needs to change it to the file name in your project, that is, the new folder under JNI mentioned above.

You should see the last sentence of com_ maxi_ mp3record_ Mp3recorder. C obviously, this is the. C file I created myself. The interface used to call mp3lame corresponds to com.maxi.mp3record.mp3recorder.java in my java. Let's create a java file first.

Create mp3recorder.java

Create an mp3recorder.java file corresponding to your package name. This file is a java file. Just create it corresponding to your package name.

Create C file

When creating a C file, create it under JNI, and name it according to the package name of your java file. Replace "_" with "". For example: com_ maxi_ mp3record_ MP3Recorder.c。 Of course, there must be a header file: com_ maxi_ mp3record_ MP3Recorder.h。

com_ maxi_ mp3record_ MP3Recorder.c

com_ maxi_ mp3record_ MP3Recorder.h

Don't copy these two files without looking at their contents. They are all interfaces connecting Java and C, so there can be no errors. For example, #ifndef_ Included_ com_ maxi_ mp3record_ Mp3recorder, this must be replaced with your corresponding package name, and everything in it must be replaced with the package name corresponding to your own program.

Compile NDK

Create application.mk under JNI of your project file and write: app_ ABI: = all if not added, NDK will only compile "armeabi". However, Android has many different types of processors, so we need more than arm. I'm sure you've got all the NDK configured up to now. Then open the terminal and enter your project (find the NDK bulid in your NDK directory, and you'd better add it to the environment variable. It's more convenient for future compilation. By default, you don't add the environment variable here) and execute NDK bulid. After a moment, you will find that there is an obj folder in your project, and "arm64-v8a", "armeabi", "armeabi-v7a", "MIPS", "MIPS64", "x86" and "x86" will be generated under the obj folder_ 64”。 Open it, and there will be a libmp3lame.so under each folder. OK, that's right. That's the "skateboarding shoes" you want. Put it into your LIBS file. Without creating it yourself, all platforms can be loaded.

usage method

summary

I have been recording with mediarecorder before. I found that what I recorded can only be AMR, ACC and other formats. It is not feasible to convert lame to MP3. I tried and failed. I don't know the specific reason, so you can study it when you have time. Don't try if you don't have time.

The sound recorded by mp3lame is still quite reliable (but it is said that there are some Sasha sounds on IOS), and then the recorded size is acceptable. The audio for five seconds is about 20K. It is very convenient to use. If you have any questions or suggestions, please leave a message.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>