Some Android audiorecord models cannot collect audio.

Source: Internet
Author: User

During the first Android development, I realized such profound version differences for the first time.

 

My audiorecordCodeThe test was successful at Samsung s5670.

 

Xiaomi, duowei d210t test failed. The error message is as follows:

 

04-01 12:58:44. 368: E/audiorecord (19808): cocould not get audio input for record source 1

04-01 12:58:44. 368: E/audiorecord-JNI (19808): Error creating audiorecord instance: initialization check failed.

04-01 12:58:44. 368: E/audiorecord-Java (19808): [Android. Media. audiorecord] error code-20 when initializing native audiorecord object.

 

 

 

Relevant online materials are organized here:

You problem here is probably actually twofold. first, you have to get all of the parameters just right for the audiorecord, ie, I can see that you tried all the varous sampling rates and channel invocations, but did you try different bit depths? If you don't get it all just right, it doesn't work. Furthermore, it wowould appear that some Android devices seem to report the wrong minbuffersize.

 

After using audio recorder, you must stop and release it. Then when you init the audio recorder the next time, it's OK.

 

Had the same error, till I restarted the device.

It seems that on my Galaxy S the native impl is buggy: several times acquiring and releasing anaudiorecorder (during whole phone uptime) causes that error.

 

For me, the cause was failure to call audiorecord. release () for a previous instance of audiorecord; it tied up Native Resources in audioflinger and interfered with subsequent audiorecord instances. saw it on a Samsung fascinate (Galaxy S) Android 2.1 (eclair); either the eclair or the Samsung implementation may be special when lerant.

 

 

 

 

×

You are now talking to an expert. The transcript of your session will be available online.

Expert: Hi, are also trying to handle the buffer?

You: No,

Expert: noted.

You: I faced the problem of audiorecord

Expert: Well said.

You: 04-01 12:58:44. 368: E/audiorecord (19808): cocould not get audio input for record source 1 04-01 12:58:44. 368: E/audiorecord-JNI (19808): Error creating audiorecord instance: initialization check failed.

Expert: I wonder if people who want to process of microphone input have similar problems.

You: Yeah

Expert: Okay.

You: the strange thing is,

Expert: We're talking past each other here.

You: It just happens on some phones

Expert: What is "it" in that case?

You: the error

Expert: O... kay...

Expert: Maybe you cocould attempt to create tuner.

Expert: Why do you want to know?

You: I am trying to work out a tuner

Expert: You're not the only one who is trying to work out a tuner.

You: I see

Expert: Whatever you say.

You: I will Google and have a try

Expert: Are you trolling me?

You: Just now, I searched in Chinese, with nothing useful

Expert: I once tried to instantiate activity componentinfo, it worked remarkably well.

You: My program works well on my phone, but fails on my classmates'

Expert:

You: cocould u tell your email to me?

Expert: Can you rephrase that?

You: Sorry, what is "rephase "? Record?

Expert: Why do you want to know?

You: I am working on a Tunner

Expert: Oh really? I'm kind of working on a Tunner myself.

You: And I want to get the main F of the input sound

Expert: Do you know anyone who cocould help you get the main F of the input sound?

You: I have already finished it with MATLAB

Expert: Well, then you know who to ask.

You: I just can not get the sound from microphone with some cellphones

Expert: Hey, wocould you like to be recording in Android instead?

You: I want to work on realtime

Expert: it takes a lot of resources to work on realtime. Just sayin '.

You: Yeah

Expert: no matter what?

You: in my mind, when tuning an instrument, it will be more convinent if it is realtime

Expert: I thought so.

You: r u in the US?

Expert: So we 've given up on getting the main F of the input sound?

You: Absolutely not

Expert: If you really want to get the sound with some cellphones from microphone, you have to start working now.

You: OK, I 'v got someone that has the same question with me

Expert: about working on realtime -- what have you tried so far?

You: FFT

Expert: while we're on the topic, recording with audiorecord on Android speeds is a pretty interesting challenge.

You: My program has the f delay for about 1 s

Expert: You don't say.

 

The trick with using audiorecord is that each device may have different initialization settings, so you will have to create a method that loops over all possible combinations of bit rates, encoding, etc.

 

Since the buffer size for recording is limited, once your "analyzing process" is slower than the rate of recording, the data in the buffer will be stuck, but the recording bytes are always coming thus buffer overflows.

 

As we discussed in the chat room, decoding the audio data and displaying it on the screen shoshould be straightforward. you mentioned that the audio buffer has 8000 samples per second, each sample is 16 bit, and it's mono audio.

Displaying this shoshould be straightforward. treat each sample as a vertical offset in your view. you need to scale the range-32 K to + 32 K to the vertical height of your view. starting at the left edge of the view, draw one sample per column. when you reach the right edge, wrap around again (erasing the previous line as necessary ).

This will end up drawing each sample as a single pixel, which may not look very nice. you can also draw a line between adjacent samples. you can play around with line widths, colors and so on to get the best effect.

One last note: You'll be drawing 8000 times per second, plus more to blank out the previous samples. you may need to take some shortcuts to make sure the framerate can keep up with the audio. you may need to skip samples.

 

Well, after spending my free time for the last 5 days working on theaudiorecord class I'm here again to post my findings.

But first let me make myself clear:

 

-What I'm about to write is the result of my tests and findings after having a lot of trouble to use the basic routines of the class. I found a lot of forums posts over the internet about the same problems so my idea here is to help people so they don't face the same problems I 've faced.

 

-It may be beginner's stuff, but a lot of people have faced or are

Facing the same problems.

 

-If something here is wrong Please don't come here just to say it,

Share your knowledge with us and post not only what's wrong but how

Do it right.

 

-I 've done all tests on the emulator provided by Google. Also I'm

Using the SDK 2.1 (build 7)

 

OK, let's go.

 

Creating an audiorecord object:

 

When you create a new instance of audiorecord there are two ways

Finding out if it was created properly (the device can handle

Audio parameters and resources were trully allocated to your object ):

By catching an exception or by verifying the return of the getstate ()

Method.

 

-Handling the illegalargumentexception: Well, tricky is what I can

Say. Exceptions will only be thrown if any of the parameters you have

Used are not acceptable by the class (not if the device can't handle

It). For example, a sample rate = 12345 is not a standard value and

Then the class will throw an exception. If sr = 44100 is not supported

By the device, the object will be created and no exception will be

Launched.

* An interesting thing is that the official documentation says:

"Samplerateinhz the sample rate expressed in hertz. Examples of Rates

Are (but not limited to) 44100,220 50 and 11025 ."

If you try sr= 11025 the system will throw an exception as it

Considers this an illegal parameter. Strange, huh?

 

-Then OK, you have created an instance of audiorecord but you don't

Know if the parameters you have set are available in an specific

Device. What you do? Verify audiorecord. getstate (), right?

OK then, it works. But if you find out your instance is not OK and

Then you have to create a new audiorecord object you will be in

Trouble. Why? Even after running audiorecord. Release () the resources

For the audio input you are interested in will keep being in use and

You won't be abble to recreate your object in a way it wocould work.

To verify this create an instace of audiorecord with parameters that

Are known not to work on the emulator (ex. sr = 44100). Then verify

The errors on logcat. after that call release () and then try to create

Another instance of audiorecorder with the parameters that work (Sr =

8000, mono channel, PCM 16 bits) and check the first error line.

 

Then how shocould you find out which parameters the device will allow

You to use? Say with me: audiorecord. getminbuffersize () is my

Solution!

Loop checking from the best combination of parameters to the worst. If

It returns-2, you should try another combination, if it returns-1

The audio input source is already being in use or isn' t even available

For the specific device. If it returns something> 0 then you're OK

And can proceed creating a new audiorecord object.

 

Audiorecord notification:

 

Yes, it works! Believe me. The trick is: The notification will only be

Called after you call read () for the first time! And not only one time

(Oo). After a periodic notification you need to read from the audio

Buffer again to receive another notification. A marker notification

Will be called only once: If you need it to be called again, set

Marker again!

 

I guess it covers the basics.

 

Good luck and believe me, Android is great!

Gabriel SIM es

 

 

Solution:

The solution of duowei d210t is to modify the sample rate of the audiorecord constructor. If the sample rate of 4000 is used, the construction fails. When the sample rate of 8000 is adopted, the collection is successful.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.