androidstudio ffmpeg audio conversion

Description of Requirement

The speech recognition and human voice detection after voice wake-up in the project has always been a headache for me. In the past, due to my inexperience in using various types of toolkits and time issues, I have not been able to cure this poor voice detection experience. Problem, the previous solution was to scrape the vad code from other big guys on the post and use it. There are two problems in it. One is that the human voice detection is very sensitive, there is basically a little noise, and the background sound will be heard. It’s the human voice; the second one is about when to stop the audio recording. I solved this problem by adding a timer. After how many seconds vad has not detected the sound, the default user will stop answering the call. Because I wrote it myself, and the time is set based on my feelings. It has always been useless to use. Coupled with the inaccuracy of vad recognition, the second problem can easily be magnified, resulting in a worse experience. I have been researching a certain product recently
. Regarding the documents and codes of speech recognition technology, I found that their voice detection and recording stop functions are very mature, so I decided to use their sdk and skip charging (as long as I don’t fill in the key), because if I write the real name of the product , they may limit this function later, so I won’t go into the
above details here. My solution is to use the vad component in the speech recognition product that has matured to help me complete the human voice detection function, and then I will use the human voice detection function based on the human voice detection function. After processing the saved audio, I can obtain the specified audio format I need.

Functional flow

Voice wake-up->successful wake-up->voice recognition->voice recognition ends automatically->audio conversion->get audio path->for other methods to call

Required conditions

  1. Speech recognition Android SDK for a certain product (needs magic modification)
  2. Android version of ffmpeg (check if there is the encoder you need according to your needs)
  3. It may be a non-essential condition - permission issues related to reading and writing memory between different Android versions

step

step 1

Create a new androidstudio project, transplant the AndroidSDK of speech recognition into the new project, create a test unit in the app through demo or yourself, and check whether the voice wake-up and speech recognition can work normally. To use the voice wake-up function, you need to make sure that the package name when creating the application is the same as yours
. The package names of the newly created projects are the same (a common problem...)
New users of speech recognition will have a certain number of free calls. Old users will be fine, as long as they test a few times, because what we need is the vad included in the sdk. Human voice detection function

Step 2

Turn on vad detection in the speech recognition part of the SDK. If you are in a hurry, you can directly ask the customer service of the corresponding product or check the documentation. What
you need to know

  1. vad detects where it is opened and closed
  2. The voice recognition callback place.
    Generally, after the vad vocal detection is completed, the callback method of the voice recognition result will be used.
    My audio conversion function is executed in the callback.

Step 3

After testing that there are no problems with the function, I need to introduce the Android version of ffmpeg into the project.
Here I need to use the function of converting mp3 to amr, and some ffmpeg does not come with an encoder related to amr. I need to manually add and install it. In terms of time and technology, I use The following ffmpeg is available for personal testing.

https://github.com/tanersener/mobile-ffmpeg

The document explains how to introduce it.
Insert image description here
Regarding how to test after introducing the Android version of ffmpeg, you can find tutorial
tips on the ffmpeg command on the Internet:如果你复制的指令执行后报错,可以试试调换里面参数的位置 比如-i后面尽量跟着原文件路径 转换后的新文件路径

Some exceptions I encountered while using

Option sample_rate not found
This exception is mainly a parameter setting problem. It is recommended to find more ffmpeg instructions and try a few more times.
amr_nb not found
This is the ffmpeg you are currently using. It does not support the conversion of amr format. I
Output file #0 does not contain any stream
have also encountered this exception, but I forgot how to solve it. This encounter Not too many.
There are two commands I use.
One is pcm to MP3.

-y -f s16be -ac 1 -ar 16.0k -acodec pcm_s16le -i 原文件路径.pcm 新文件路径.mp3

requires attention:开头不能有空格;每个参数之间尽量保证只有一个空格;-ar后面跟着的参数不能乱填,采样率是有标准和要求的,自己写会报错

One is to convert MP3 to amr

-i 原文件路径.mp3 -ar 8000 -ac 1 新文件路径.amr

Basically these are the steps.
Because my project cannot print out logs when running on a real machine, some of the problems encountered can only be solved with a little knowledge. It should be noted that
different 文件写入和读取的问题 versions of Android have direct read and write permissions for files. Differently, the higher the version, the stricter the restrictions. Basically, files can only be read and written under the name of android/data/your own package.

The remaining content is to package this function as an aar and call it in other places.
The function has basically been completed...

Thanks for the following post

https://blog.csdn.net/bang152101/article/details/106147264/
https://github.com/dadiyang/jave/issues/24
https://blog.csdn.net/shulianghan/article/details/121268792
https://coding.imooc.com/learn/questiondetail/246831.html
https://blog.csdn.net/xiaokangss/article/details/125539755

Guess you like

Origin blog.csdn.net/pure81/article/details/131090899