I am trying to build an Android application that records X number of seconds and saves the file in .wav format
The problem is that if I input more than 47 seconds ( 48 and above) I get the following errors:
11-29 10:58:39.315 399-1548/? V/audio_hw_primary: disable_audio_route: exit
11-29 10:58:39.315 399-1548/? D/audio_hw_primary: disable_snd_device: enter 2
11-29 10:58:39.315 399-1548/? D/hardware_info: hw_info_append_hw_type : device_name = speaker
11-29 10:58:39.315 399-1548/? D/audio_hw_primary: disable_snd_device: snd_device(2: speaker)
11-29 10:58:39.316 399-1548/? V/audio_hw_primary: stop_output_stream: exit: status(0)
11-29 10:58:39.316 399-1548/? V/audio_hw_primary: out_standby: exit
11-29 10:58:39.316 399-1548/? V/AudioFlinger_Threads: thread 0xb186a008 type 0 TID 1548 going to sleep
11-29 10:58:42.112 414-616/? I/ThermalEngine: Sensor:pa_therm0:34000 mC
11-29 10:58:42.445 2107-2873/? D/InputDispatcher: notifyMotion - action=ACTION_DOWN(0), id=0, tooltype=1, deviceId=5, source=0x1002, policyFlags=0x0, flags=0x0, metaState=0x0, buttonState=0x0, edgeFlags=0x0, eventTime=19045273719000, downTime=19045273719000,
11-29 10:58:42.446 2107-2872/? D/InputTransport: channel 'ea7a32b com.audiorecorder.company.audiorecorder/com.audiorecorder.company.audiorecorder.mainActivity (server)' : action=ACTION_DOWN(0), id=0, tooltype=1, deviceId=5, source=0x1002, flags=0x0, edgeFlags=0x0, metaState=0x0, buttonState=0x0, downTime=19045273719000, eventTime=19045273719000, pointerCount=1
11-29 10:58:42.446 29103-29103/com.audiorecorder.company.audiorecorder I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
11-29 10:58:42.511 2107-2873/? D/InputDispatcher: notifyMotion - action=ACTION_UP(0), id=0, tooltype=1, deviceId=5, source=0x1002, policyFlags=0x0, flags=0x0, metaState=0x0, buttonState=0x0, edgeFlags=0x0, eventTime=19045339132000, downTime=19045273719000,
11-29 10:58:42.511 2107-2872/? D/InputTransport: channel 'ea7a32b com.audiorecorder.company.audiorecorder/com.audiorecorder.company.audiorecorder.mainActivity (server)' : action=ACTION_UP(0), id=0, tooltype=1, deviceId=5, source=0x1002, flags=0x0, edgeFlags=0x0, metaState=0x0, buttonState=0x0, downTime=19045273719000, eventTime=19045339132000, pointerCount=1
11-29 10:58:42.512 29103-29103/com.audiorecorder.company.audiorecorder I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
11-29 10:58:42.534 399-2955/? V/AudioFlinger: registerClient() client 0xb87ca990, pid 29103
11-29 10:58:42.534 399-2955/? V/AudioFlinger_Threads: sendConfigEvent_l() num events 1 event 0
11-29 10:58:42.534 399-1548/? V/AudioFlinger_Threads: thread 0xb186a008 type 0 TID 1548 waking up
11-29 10:58:42.534 399-1548/? V/AudioFlinger_Threads: processConfigEvents_l() remaining events 1
11-29 10:58:42.535 399-1548/? V/AudioFlinger_Threads: PlaybackThread::ioConfigChanged, thread 0xb186a008, event 0
11-29 10:58:42.535 399-1548/? V/AudioFlinger_Threads: processConfigEvents_l() DONE thread 0xb186a008
11-29 10:58:42.536 29103-29114/com.audiorecorder.company.audiorecorder D/AudioSystem: getIoDescriptor: ioHandle = 2, index = -2, mIoDescriptors = 0xb99fbd70
11-29 10:58:42.536 29103-29114/com.audiorecorder.company.audiorecorder V/AudioSystem: ioConfigChanged: [Update mIoDescriptors] add ioHandle = 2 -> descriptor = 0xa9e3077c
11-29 10:58:42.536 29103-29114/com.audiorecorder.company.audiorecorder V/AudioSystem: ioConfigChanged() new output opened 2 samplingRate 48000, format 0x1 channel mask 0x3 frameCount 960 deviceId 2
11-29 10:58:42.537 29103-29103/com.audiorecorder.company.audiorecorder D/AudioRecord: getMinFrameCount 1792
11-29 10:58:42.551 29103-29103/com.audiorecorder.company.audiorecorder D/AudioRecord: set(): inputSource 1, sampleRate 44100, format 0x1, channelMask 0x10, frameCount 2116800, notificationFrames 0, sessionId 0, transferType 0, flags 0, opPackageName com.audiorecorder.company.audiorecorder uid -1, pid -1
11-29 10:58:42.551 29103-29103/com.audiorecorder.company.audiorecorder V/AudioRecord: Building AudioRecord with attributes: source=1 flags=0x0 tags=[]
11-29 10:58:42.552 29103-29103/com.audiorecorder.company.audiorecorder V/AudioRecord: set(): mSessionId 437
11-29 10:58:42.552 29103-29103/com.audiorecorder.company.audiorecorder D/AudioRecord: set: Create AudioRecordThread
11-29 10:58:42.553 399-2955/? V/AudioPolicyService: registerClient() client 0xb8744b28, uid 10099
11-29 10:58:42.553 399-3483/? V/APM::AudioPolicyManager: getInputForAttr() source 1, samplingRate 44100, format 1, channelMask 10,session 437, flags 0
11-29 10:58:42.553 399-3483/? V/APM::AudioPolicyEngine: getDeviceForInputSource()input source 1, device 80000004
11-29 10:58:42.554 399-3483/? V/AudioFlinger: openInput(), module 1 Device 80000004, SamplingRate 44100, Format 0x000001, Channels 10, flags 0
11-29 10:58:42.554 399-3483/? V/audio_hw_primary: adev_open_input_stream: enter
11-29 10:58:42.554 399-3483/? D/audio_hw_primary: adev_open_input_stream: enter: sample_rate(44100) channel_mask(0x10) devices(0x80000004) stream_handle(0xb87c58f0) io_handle(438)
11-29 10:58:42.554 399-3483/? V/audio_hw_primary: adev_open_input_stream: exit
11-29 10:58:42.554 399-3483/? V/AudioFlinger: openInput_l() openInputStream returned input 0xb87c58f0, SamplingRate 44100, Format 0x1, Channels 10, flags 0, status 0 addr
11-29 10:58:42.554 399-3483/? V/AudioFlinger: openInput_l() created record thread: ID 438 thread 0xb874d850
11-29 10:58:42.555 399-3483/? D/AudioSystem: getIoDescriptor: ioHandle = 438, index = -2, mIoDescriptors = 0xb86a44f0
11-29 10:58:42.555 399-3483/? V/AudioSystem: ioConfigChanged: [Update mIoDescriptors] add ioHandle = 438 -> descriptor = 0xb13d68dc
11-29 10:58:42.555 399-3483/? V/AudioSystem: ioConfigChanged() new input opened 438 samplingRate 44100, format 0x1 channel mask 0x10 frameCount 896 deviceId 0
11-29 10:58:42.555 399-29325/? I/AudioFlinger_Threads: AudioFlinger's thread 0xb874d850 ready to run
11-29 10:58:42.555 2107-3615/? D/AudioSystem: getIoDescriptor: ioHandle = 438, index = -2, mIoDescriptors = 0xb9b82ca0
11-29 10:58:42.555 2107-3615/? V/AudioSystem: ioConfigChanged: [Update mIoDescriptors] add ioHandle = 438 -> descriptor = 0x9b31e77c
11-29 10:58:42.555 2107-3615/? V/AudioSystem: ioConfigChanged() new input opened 438 samplingRate 44100, format 0x1 channel mask 0x10 frameCount 896 deviceId 0
11-29 10:58:42.555 3049-19038/? D/AudioSystem: getIoDescriptor: ioHandle = 438, index = -2, mIoDescriptors = 0xb9e5fea0
11-29 10:58:42.555 3049-19038/? V/AudioSystem: ioConfigChanged: [Update mIoDescriptors] add ioHandle = 438 -> descriptor = 0x97e4977c
11-29 10:58:42.555 399-3483/? V/APM::AudioPolicyManager: getInputForAttr() returns input type = 0
11-29 10:58:42.555 3049-19038/? V/AudioSystem: ioConfigChanged() new input opened 438 samplingRate 44100, format 0x1 channel mask 0x10 frameCount 896 deviceId 0
11-29 10:58:42.555 399-3483/? V/AudioPolicyService: AudioCommandThread() adding update audio port list
11-29 10:58:42.555 29103-29115/com.audiorecorder.company.audiorecorder D/AudioSystem: getIoDescriptor: ioHandle = 438, index = -2, mIoDescriptors = 0xb99fbd70
11-29 10:58:42.555 399-3483/? V/AudioPolicyService: inserting command: 9 at index 0, num commands 0
11-29 10:58:42.555 29103-29115/com.audiorecorder.company.audiorecorder V/AudioSystem: ioConfigChanged: [Update mIoDescriptors] add ioHandle = 438 -> descriptor = 0xa989777c
11-29 10:58:42.556 29103-29115/com.audiorecorder.company.audiorecorder V/AudioSystem: ioConfigChanged() new input opened 438 samplingRate 44100, format 0x1 channel mask 0x10 frameCount 896 deviceId 0
11-29 10:58:42.556 3854-4064/? D/AudioSystem: getIoDescriptor: ioHandle = 438, index = -2, mIoDescriptors = 0xb8c7ce18
11-29 10:58:42.556 3854-4064/? V/AudioSystem: ioConfigChanged: [Update mIoDescriptors] add ioHandle = 438 -> descriptor = 0xa905577c
11-29 10:58:42.556 3854-4064/? V/AudioSystem: ioConfigChanged() new input opened 438 samplingRate 44100, format 0x1 channel mask 0x10 frameCount 896 deviceId 0
11-29 10:58:42.555 399-29325/? D/audio_hw_primary: in_standby: enter: stream (0xb87c58f0) usecase(7: audio-record)
11-29 10:58:42.556 399-29325/? V/audio_hw_primary: in_standby: exit: status(0)
11-29 10:58:42.556 399-1482/? V/AudioPolicyService: AudioCommandThread() processing update audio port list
11-29 10:58:42.556 399-1482/? V/AudioPolicyService: -AudioCommandThread 9
11-29 10:58:42.556 399-1482/? V/AudioPolicyService: AudioCommandThread() going to sleep
11-29 10:58:42.558 399-29325/? D/audio_hw_primary: in_standby: enter: stream (0xb87c58f0) usecase(7: audio-record)
11-29 10:58:42.558 399-29325/? V/audio_hw_primary: in_standby: exit: status(0)
11-29 10:58:42.558 399-29325/? V/AudioFlinger_Threads: RecordThread: loop stopping
11-29 10:58:42.561 399-1558/? V/AudioFlinger: openRecord() lSessionId: 437 input 438
11-29 10:58:42.561 399-1558/? E/AudioFlinger_Tracks: not enough memory for AudioTrack size=8388712
11-29 10:58:42.561 399-1558/? D/MemoryDealer: AudioTrack (0xb89782f8, size=4198400)
0: 0xb8973200 | 0x00000000 | 0x00401000 | F
size allocated: 0 (0 KB)
11-29 10:58:42.561 399-1558/? E/AudioFlinger_Threads: createRecordTrack_l() initCheck failed -12; no control block?
11-29 10:58:42.561 399-1558/? V/AudioFlinger_Tracks: virtual android::AudioFlinger::RecordThread::RecordTrack::~RecordTrack()
11-29 10:58:42.561 399-1558/? V/AudioFlinger: removeClient_l() pid 29103, calling pid 29103
11-29 10:58:42.561 29103-29103/com.audiorecorder.company.audiorecorder E/AudioRecord: AudioFlinger could not create record track, status: -12
11-29 10:58:42.562 399-2955/? V/APM::AudioPolicyManager: releaseInput() 438
11-29 10:58:42.562 399-2955/? V/APM::AudioPolicyManager: closeInput(438)
11-29 10:58:42.562 399-2955/? V/AudioFlinger: closeInput() 438
11-29 10:58:42.562 399-2955/? D/AudioSystem: getIoDescriptor: ioHandle = 438, index = 1, mIoDescriptors = 0xb86a44f0
11-29 10:58:42.562 399-2955/? V/AudioSystem: ioConfigChanged() input 438 closed
11-29 10:58:42.562 399-2955/? V/AudioSystem: ioConfigChanged: [Update mIoDescriptors] remove ioHandle = 438
11-29 10:58:42.562 399-2955/? V/AudioFlinger_Threads: ThreadBase::exit
11-29 10:58:42.562 2107-3562/? D/AudioSystem: getIoDescriptor: ioHandle = 438, index = 1, mIoDescriptors = 0xb9b82ca0
11-29 10:58:42.562 29103-29114/com.audiorecorder.company.audiorecorder D/AudioSystem: getIoDescriptor: ioHandle = 438, index = 1, mIoDescriptors = 0xb99fbd70
11-29 10:58:42.562 2107-3562/? V/AudioSystem: ioConfigChanged() input 438 closed
11-29 10:58:42.562 2107-3562/? V/AudioSystem: ioConfigChanged: [Update mIoDescriptors] remove ioHandle = 438
11-29 10:58:42.562 29103-29114/com.audiorecorder.company.audiorecorder V/AudioSystem: ioConfigChanged() input 438 closed
11-29 10:58:42.563 29103-29114/com.audiorecorder.company.audiorecorder V/AudioSystem: ioConfigChanged: [Update mIoDescriptors] remove ioHandle = 438
11-29 10:58:42.563 3049-3078/? D/AudioSystem: getIoDescriptor: ioHandle = 438, index = 1, mIoDescriptors = 0xb9e5fea0
11-29 10:58:42.563 3049-3078/? V/AudioSystem: ioConfigChanged() input 438 closed
11-29 10:58:42.563 3049-3078/? V/AudioSystem: ioConfigChanged: [Update mIoDescriptors] remove ioHandle = 438
11-29 10:58:42.563 399-29325/? V/AudioFlinger_Threads: RecordThread: loop starting
11-29 10:58:42.564 399-29325/? V/AudioFlinger_Threads: RecordThread 0xb874d850 exiting
11-29 10:58:42.564 399-2955/? D/audio_hw_primary: adev_close_input_stream: enter:stream_handle(0xb87c58f0)
11-29 10:58:42.564 399-2955/? D/audio_hw_primary: in_standby: enter: stream (0xb87c58f0) usecase(7: audio-record)
11-29 10:58:42.564 399-2955/? V/audio_hw_primary: in_standby: exit: status(0)
11-29 10:58:42.564 3854-3864/? D/AudioSystem: getIoDescriptor: ioHandle = 438, index = 1, mIoDescriptors = 0xb8c7ce18
11-29 10:58:42.564 399-2955/? V/AudioPolicyService: AudioCommandThread() adding update audio port list
11-29 10:58:42.564 3854-3864/? V/AudioSystem: ioConfigChanged() input 438 closed
11-29 10:58:42.564 3854-3864/? V/AudioSystem: ioConfigChanged: [Update mIoDescriptors] remove ioHandle = 438
11-29 10:58:42.564 399-2955/? V/AudioPolicyService: inserting command: 9 at index 0, num commands 0
11-29 10:58:42.564 399-2955/? V/APM::AudioPolicyManager: releaseInput() exit
11-29 10:58:42.564 399-1482/? V/AudioPolicyService: AudioCommandThread() processing update audio port list
11-29 10:58:42.564 399-1482/? V/AudioPolicyService: -AudioCommandThread 9
11-29 10:58:42.565 399-1482/? V/AudioPolicyService: AudioCommandThread() going to sleep
11-29 10:58:42.566 29103-29103/com.audiorecorder.company.audiorecorder E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
11-29 10:58:42.566 29103-29103/com.audiorecorder.company.audiorecorder E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
11-29 10:58:42.607 2107-3097/? D/PocketDetection: updateScreenWakelockState: mScreenWakeLockList.add(SCREEN_BRIGHT_WAKE_LOCK 'WindowManager' ON_AFTER_RELEASE android (uid=1000, pid=2107, ws=WorkSource{10099}) uptime = 0), mIsScreenWakelockAcquired = true
11-29 10:58:42.645 29103-29103/com.audiorecorder.company.audiorecorder V/ViewRootImpl: Contents drawing finished : Toast
11-29 10:58:44.567 2107-3573/? D/InputDispatcher: Window went away: Window{3e54ba2 u0 Toast}
11-29 10:58:45.538 399-1548/? V/AudioFlinger_Threads: thread 0xb186a008 type 0 TID 1548 going to sleep
11-29 10:58:46.564 2107-3616/? D/InputDispatcher: Window went away: Window{af07fee u0 Toast}
11-29 10:58:47.096 2107-2334/? D/PocketDetection: updateScreenWakelockState: mScreenWakeLockList.remove(0)
11-29 10:58:47.096 2107-2334/? D/PocketDetection: updateScreenWakelockState: mIsScreenWakelockAcquired = false
11-29 10:58:47.114 414-616/? I/ThermalEngine: Sensor:pa_therm0:34000 mC
To be more precise:
11-29 10:58:42.561 399-1558/? E/AudioFlinger_Tracks: not enough memory for AudioTrack size=8388712
11-29 10:58:42.561 399-1558/? D/MemoryDealer: AudioTrack (0xb89782f8, size=4198400)
0: 0xb8973200 | 0x00000000 | 0x00401000 | F
size allocated: 0 (0 KB)
11-29 10:58:42.561 399-1558/? E/AudioFlinger_Threads: createRecordTrack_l() initCheck failed -12; no control block?
11-29 10:58:42.561 29103-29103/com.audiorecorder.siemens.audiorecorder E/AudioRecord: AudioFlinger could not create record track, status: -12
11-29 10:58:42.566 29103-29103/com.audiorecorder.siemens.audiorecorder E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
11-29 10:58:42.566 29103-29103/com.audiorecorder.siemens.audiorecorder E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
My code works well with any number of seconds that is lower than 48 seconds. What am I doing wrong?
Here's the class that does the recording:
public class RecordingClass {
private int lengthInMilliseconds;
private AudioRecord recorder;
private Thread recordingThread;
private Thread timerThread;
private static final int RECORDER_SAMPLERATE = 44100;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_BPP = 16;
private static final String AUDIO_RECORDER_FILE_EXT_WAV = ".wav";
private static final int BYTES_PER_SAMPLE = 2;
private static final int CHANNELS_MONO = 1;
private boolean isRecording = false;
private int bufferSize = 0;
private static final String TAG = "RecordingClass";
public RecordingClass(int nrOfSeconds) {
this.lengthInMilliseconds = nrOfSeconds * 1000; //to get total milliseconds time because the time from UI is in seconds
// initialises bufferSize variable
getBufferSize();
}
public void startRecording() {
Toast.makeText(parentActivity, "Recording started for " + lengthInMilliseconds /1000 + " seconds in file '"+fileName+"'", Toast.LENGTH_SHORT).show();
recorder = new AudioRecord(
MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE,
RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING,
bufferSize);
int recorderStatusCode = recorder.getState();
if (recorderStatusCode == AudioRecord.STATE_INITIALIZED) {
timerThread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(lengthInMilliseconds);
stopRecording();
isRecording = false;
} catch (InterruptedException e) {
return;
}
}
}, "Timer Thread");
//starting recording thread
recordingThread = new Thread(new Runnable() {
@Override
public void run() {
while(isRecording)
writeAudioDataToFile();
}
}, "AudioRecorder Thread");
recorder.startRecording();
recordingThread.start();
timerThread.start();
}
else
{
Toast.makeText(parentActivity, "The AudioRecorder was not initialized successfully", Toast.LENGTH_SHORT).show();
}
}
public void stopRecording() {
if(recorder != null)
{
isRecording = false;
if(recorder.getState() == AudioRecord.STATE_INITIALIZED) {
recorder.stop();
isRecording = false;
}
recorder.release();
recorder = null;
recordingThread = null;
}
//initiatePopup();
}
/**
* Method used to calculate the appropriate buffer size based on the desired audio length in milliseconds
*
*/
private void getBufferSize() {
float percentOfASecond = (float) lengthInMilliseconds / 1000.0f;
int numSamplesRequired = (int) ((float) RECORDER_SAMPLERATE * percentOfASecond);
int bufferSize = 0;
int minBufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
// each sample takes two bytes, thus we need a bigger buffer
if (RECORDER_AUDIO_ENCODING == AudioFormat.ENCODING_PCM_16BIT)
{
bufferSize = numSamplesRequired * BYTES_PER_SAMPLE;
}
else
{
bufferSize = numSamplesRequired;
}
if (bufferSize < minBufferSize)
{
bufferSize = minBufferSize;
}
}
private void writeAudioDataToFile() {
//writes audio to file
}
}
private void initial() {
mRecorder = new MediaRecorder();
File captureDir = new File(FileUtil.getDirpath(), "Audio");
if (!captureDir.exists()){
captureDir.mkdir();
}
String filename = getCharacterAndNumber() + ".amr";
File file = new File(captureDir, filename);
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(file.getAbsolutePath());
mAudioPath.add(file.getAbsolutePath());
}
public void startRecorder() {
initial();
try {
mRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mRecorder.start();
}
public void stopRecorder() {
if (mRecorder != null) {
mRecorder.stop();
mRecorder.release();
}
mRecorder = null;
}
private String getCharacterAndNumber() {
String rel = "";
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
Date curDate = new Date(System.currentTimeMillis());
rel = formatter.format(curDate);
return rel;
}
I`ve usually used this way in my project, were you call prepare() or release() in the lifecycle;