我一直试图解决这个问题已经好几天了,并且在这里经历了大量的线程而没有运气。我正在尝试在单击网格上的按钮时记录到文件,保存它,然后播放它。
当程序启动时,我得到了这个:
09-11 18:00:15.357 1076-1076/com.spookyapps.lollatone E/AudioRecord﹕ Could not get audio input for record source 1
09-11 18:00:15.357 1076-1076/com.spookyapps.lollatone E/AudioRecord-JNI﹕ Error creating AudioRecord instance: initialization check failed.
09-11 18:00:15.357 1076-1076/com.spookyapps.lollatone E/android.media.AudioRecord﹕ Error code -20 when initializing native AudioRecord object.
09-11 18:00:15.367 1076-1076/com.spookyapps.lollatone D/d﹕ Attempting rate 8000Hz, bits: 3, channel: 16
09-11 18:00:15.367 1076-1076/com.spookyapps.lollatone E/android.media.AudioRecord﹕ getMinBufferSize(): Invalid audio format.
09-11 18:00:15.367 1076-1076/com.spookyapps.lollatone D/d﹕ Attempting rate 8000Hz, bits: 3, channel: 12
09-11 18:00:15.367 1076-1076/com.spookyapps.lollatone E/android.media.AudioRecord﹕ getMinBufferSize(): Invalid audio format.
09-11 18:00:15.367 1076-1076/com.spookyapps.lollatone D/d﹕ Attempting rate 8000Hz, bits: 2, channel: 16
09-11 18:00:15.567 1076-1076/com.spookyapps.lollatone D/﹕ HostConnection::get() New Host Connection established 0xb743ab50, tid 1076
09-11 18:00:15.647 1076-1076/com.spookyapps.lollatone W/EGL_emulation﹕ eglSurfaceAttrib not implemented
当我尝试点击按钮启动所有内容时,我得到了这个:
09-11 18:03:59.287 1141-1141/com.spookyapps.lollatone E/android.media.AudioRecord﹕ getMinBufferSize(): Invalid audio format.
09-11 18:03:59.287 1141-1141/com.spookyapps.lollatone D/Sounds﹕ Attempting rate 8000Hz, bits: 2, channel: 16
09-11 18:03:59.287 1141-1141/com.spookyapps.lollatone E/AudioRecord﹕ Could not get audio input for record source 1
09-11 18:03:59.287 1141-1141/com.spookyapps.lollatone E/AudioRecord-JNI﹕ Error creating AudioRecord instance: initialization check failed.
09-11 18:03:59.287 1141-1141/com.spookyapps.lollatone E/android.media.AudioRecord﹕ Error code -20 when initializing native AudioRecord object.
09-11 18:03:59.297 1141-1141/com.spookyapps.lollatone D/Sounds﹕ Attempting rate 8000Hz, bits: 2, channel: 12
09-11 18:03:59.297 1141-1141/com.spookyapps.lollatone E/AudioRecord﹕ Could not get audio input for record source 1
09-11 18:03:59.297 1141-1141/com.spookyapps.lollatone E/AudioRecord-JNI﹕ Error creating AudioRecord instance: initialization check failed.
09-11 18:03:59.297 1141-1141/com.spookyapps.lollatone E/android.media.AudioRecord﹕ Error code -20 when initializing native AudioRecord object.
09-11 18:03:59.297 1141-1141/com.spookyapps.lollatone D/Sounds﹕ Attempting rate 11025Hz, bits: 3, channel: 16
09-11 18:03:59.297 1141-1141/com.spookyapps.lollatone E/android.media.AudioRecord﹕ getMinBufferSize(): Invalid audio format.
09-11 18:03:59.297 1141-1141/com.spookyapps.lollatone D/Sounds﹕ Attempting rate 11025Hz, bits: 3, channel: 12
09-11 18:03:59.297 1141-1141/com.spookyapps.lollatone E/android.media.AudioRecord﹕ getMinBufferSize(): Invalid audio format.
09-11 18:03:59.297 1141-1141/com.spookyapps.lollatone D/Sounds﹕ Attempting rate 11025Hz, bits: 2, channel: 16
09-11 18:03:59.297 1141-1141/com.spookyapps.lollatone E/AudioRecord﹕ Could not get audio input for record source 1
09-11 18:03:59.297 1141-1141/com.spookyapps.lollatone E/AudioRecord-JNI﹕ Error creating AudioRecord instance: initialization check failed.
09-11 18:03:59.297 1141-1141/com.spookyapps.lollatone E/android.media.AudioRecord﹕ Error code -20 when initializing native AudioRecord object.
09-11 18:03:59.297 1141-1141/com.spookyapps.lollatone D/Sounds﹕ Attempting rate 11025Hz, bits: 2, channel: 12
09-11 18:03:59.297 1141-1141/com.spookyapps.lollatone E/AudioRecord﹕ Could not get audio input for record source 1
09-11 18:03:59.297 1141-1141/com.spookyapps.lollatone E/AudioRecord-JNI﹕ Error creating AudioRecord instance: initialization check failed.
09-11 18:03:59.297 1141-1141/com.spookyapps.lollatone E/android.media.AudioRecord﹕ Error code -20 when initializing native AudioRecord object.
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone D/Sounds﹕ Attempting rate 22050Hz, bits: 3, channel: 16
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone E/android.media.AudioRecord﹕ getMinBufferSize(): Invalid audio format.
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone D/Sounds﹕ Attempting rate 22050Hz, bits: 3, channel: 12
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone E/android.media.AudioRecord﹕ getMinBufferSize(): Invalid audio format.
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone D/Sounds﹕ Attempting rate 22050Hz, bits: 2, channel: 16
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone E/AudioRecord﹕ Could not get audio input for record source 1
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone E/AudioRecord-JNI﹕ Error creating AudioRecord instance: initialization check failed.
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone E/android.media.AudioRecord﹕ Error code -20 when initializing native AudioRecord object.
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone D/Sounds﹕ Attempting rate 22050Hz, bits: 2, channel: 12
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone E/AudioRecord﹕ Could not get audio input for record source 1
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone E/AudioRecord-JNI﹕ Error creating AudioRecord instance: initialization check failed.
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone E/android.media.AudioRecord﹕ Error code -20 when initializing native AudioRecord object.
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone D/Sounds﹕ Attempting rate 44100Hz, bits: 3, channel: 16
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone E/android.media.AudioRecord﹕ getMinBufferSize(): Invalid audio format.
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone D/Sounds﹕ Attempting rate 44100Hz, bits: 3, channel: 12
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone E/android.media.AudioRecord﹕ getMinBufferSize(): Invalid audio format.
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone D/Sounds﹕ Attempting rate 44100Hz, bits: 2, channel: 16
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone E/AudioRecord﹕ Could not get audio input for record source 1
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone E/AudioRecord-JNI﹕ Error creating AudioRecord instance: initialization check failed.
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone E/android.media.AudioRecord﹕ Error code -20 when initializing native AudioRecord object.
09-11 18:03:59.317 1141-1141/com.spookyapps.lollatone D/Sounds﹕ Attempting rate 44100Hz, bits: 2, channel: 12
09-11 18:03:59.337 1141-1141/com.spookyapps.lollatone E/AudioRecord﹕ Could not get audio input for record source 1
09-11 18:03:59.337 1141-1141/com.spookyapps.lollatone E/AudioRecord-JNI﹕ Error creating AudioRecord instance: initialization check failed.
09-11 18:03:59.337 1141-1141/com.spookyapps.lollatone E/android.media.AudioRecord﹕ Error code -20 when initializing native AudioRecord object.
09-11 18:03:59.347 1141-1141/com.spookyapps.lollatone E/AudioRecord﹕ Could not get audio input for record source 1
09-11 18:03:59.347 1141-1141/com.spookyapps.lollatone E/AudioRecord-JNI﹕ Error creating AudioRecord instance: initialization check failed.
09-11 18:03:59.347 1141-1141/com.spookyapps.lollatone E/android.media.AudioRecord﹕ Error code -20 when initializing native AudioRecord object.
09-11 18:03:59.357 1141-1141/com.spookyapps.lollatone E/AudioRecord﹕ Recording Failed
09-11 18:03:59.367 1141-1141/com.spookyapps.lollatone E/AudioTrack﹕ Playback Failed
这是我的主要活动:
package com.spookyapps.lollatone;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Sounds mSound = new Sounds();
GridView gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View v, int position, long id) {
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_LONG).show();
mSound.findAudioRecord();
mSound.record();
mSound.play();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
和我的声音课:我一直试图通过黑客攻击我在这里找到的其他“修复”来修复它但没有任何工作。它现在首先尝试找到适当的采样率和东西,但仍然没有解决问题。
package com.spookyapps.lollatone;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by abc on 9/7/2014.
*/
public class Sounds {
private static final String TAG = Sounds.class.getSimpleName();
public boolean mIsRecording = false;
int frequency = 11025;
private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };
int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
String mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(mFileName);
InputStream is;
BufferedInputStream bis;
DataInputStream dis;
AudioRecord record = new AudioRecord(MediaRecorder.AudioSource.MIC,
frequency, channelConfiguration,
audioEncoding, bufferSize);
OutputStream os;
BufferedOutputStream bos;
DataOutputStream dos;
public AudioRecord findAudioRecord() {
for (int rate : mSampleRates) {
for (short audioFormat : new short[] { AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT }) {
for (short channelConfig : new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO }) {
try {
Log.d(TAG, "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: "
+ channelConfig);
int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);
if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
// check if we can instantiate and have a success
AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);
if (recorder.getState() == AudioRecord.STATE_INITIALIZED)
return recorder;
}
} catch (Exception e) {
Log.e(TAG, rate + "Exception, keep trying.",e);
}
}
}
}
return null;
}
AudioRecord recorder = findAudioRecord();
public void record() {
String mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audioTest/" + "reverseme.pcm";
File file = new File(mFileName);
if (file.exists()) {
file.delete();
} else {
//create the new file
file.mkdirs();
}
try {
// Create a DataOutputStream to write the audio data into the saved file
OutputStream os = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
// Create a new AudioRecord object to record the audio
bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
frequency, channelConfiguration,
audioEncoding, bufferSize);
short[] buffer = new short[bufferSize];
recorder.startRecording();
mIsRecording = true;
new Timer().schedule(stop(), 5000); //time in miliseconds
while (mIsRecording) {
int bufferReadResult = recorder.read(buffer, 0, bufferSize);
for (int i = 0; i < bufferReadResult; i++) {
dos.writeShort(buffer[i]);
}
}
} catch (Throwable t) {
Log.e("AudioRecord", "Recording Failed");
}
}
public void play() {
// get the file to play back
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/reverseme.pcm");
//Get the length of the audio stored in the file (16 bit so 2 bytes per short)
//and create a short array to store the recorded audio
int musicLength = (int) (file.length());
short[] music = new short[musicLength];
try {
//Create a DataInputStream to read the audio data back from the saved file
//Read the file into the music array
int i = 0;
while (dis.available() > 0) {
music[musicLength - 1 - i] = dis.readShort();
i++;
}
// close the input streams
dis.close();
// Create a new AudioTrack object using the same parameters as the AudioRecord
// object used to create the file
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
11025,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
musicLength,
AudioTrack.MODE_STREAM);
// Start playback
audioTrack.play();
//Write the music buffer to the AudioTrack object
audioTrack.write(music, 0, musicLength);
} catch (Throwable t) {
Log.e("AudioTrack", "Playback Failed");
}
}
public TimerTask stop() {
TimerTask t = new TimerTask() {
@Override
public void run() {
mIsRecording = false;
record.stop();
record.release();
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
};
return t;
}
}