Implementation code of recording and playing function of Android voice instant messaging

In Android, the function of recording and voice playback is relatively simple, but as a reference, it is still necessary to make a brief record of voice related knowledge.

First, in Android, recording is supported in two ways. It mainly includes byte stream mode and file stream mode. Using file stream mode for recording is relatively simple, and relatively speaking, because of its good encapsulation, the recorded files will be relatively small. However, compared with file stream mode, it is not as flexible as byte stream mode, but it still needs a little effort to make good use of byte stream mode.

Let's start with the voice operation of file stream mode:

File stream mode

Let's look at the implementation of the recording part. First, let's implement the part to start recording:

Before formal coding, you still need to give a brief description. Generally speaking, the recording function is implemented in the Jin layer, and in this layer, it is implemented by single thread. If we multithread the recording API in coding, the program will flash back directly, and we can't catch its exceptions in the Java layer. Therefore, we must use single thread to ensure the normal operation of recording.

Generally speaking, there are only three steps to start recording, and the code is as follows:

Next, let's take a look at the implementation of the above three methods: the implementation is the method of releasing related resources, releaserecord:

The global variable of the mediarecorder of the mmmediarecorder. The next step is to really implement the implementation logic of starting recording dostartrecord()

There are many codes in this part, but I have given comments on the key parts. I believe it is not difficult to understand. In this part, we mainly create a new directory of voice under the root directory of sdcrad, and then create a new file with = =. M4a = = as the suffix. After configuring the relevant parameters of maudiorecord, store the collected recording in the previous file. If everything goes well, it returns true, indicating that the recording is successful.

Finally, the user is prompted to record the logic recordfail

The logic here is relatively simple, but it is worth noting that since the recording method is executed in a thread other than the main thread, we need to use the handler to realize the effect of interface prompt. The mhander here is a local variable whose initialization is placed in the oncreate () method.

mHandler = new Handler(Looper.getMainLooper());

To sum up, all logic for starting recording has been fully implemented. The next step is to realize the logic of ending the recording: the logic of the topic is as follows:

The implementation logic of dostoprecord here is as follows:

In fact, the logic of stopping recording is also very simple. First, call mmediarecorder. Stop(); Stop recording, and then judge whether the recording time is greater than 3S. If it is greater than 3S, it indicates that the recording is valid and prompts the user that the recording is successful.

To sum up, all the codes of our file stream recording have been implemented. Next, we play it. If you need to refer to all the codes, please stamp here.

Overall, file based recording is relatively easy to implement. The following describes how to realize recording through byte stream mode.

Byte stream mode recording

Start recording: the main logic is the implementation of startrecord2()

Dostoprecord() implementation for stopping recording:

Name of relevant parameters:

Next, the implementation is to play the files recorded in byte stream mode: implementation of doplay2 () topic class:

Thousands of words are certainly not as straightforward as direct code, so the code implementation point is here

The results are as follows:

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
分享
二维码
< <上一篇
下一篇>>