我正在开发一个 android 应用程序,因为我想录制音频并保存到一个文件夹中。
我已经尝试过这个示例代码,它从音频对象(AudioRecord)读取数据,但相同的应用程序在SAMSUNG GALAXY S5(USB 3.0)设备中仅读取0 个字节
这是我的代码..
private int min_buff;
private int len=0;
private AudioRecord record;
private DataOutputStream dos;
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/demo.mp4");
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button buttonStart = (Button) findViewById(R.id.button1);
final Button buttonStop = (Button) findViewById(R.id.button2);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
buttonStop.setEnabled(false);
buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonStart.setEnabled(false);
buttonStop.setEnabled(true);
startDownload();
}
});
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonStart.setEnabled(true);
buttonStop.setEnabled(false);
stopDownload();
}
});
}
public void startDownload(){
OutputStream os = null;
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedOutputStream bos = new BufferedOutputStream(os);
dos = new DataOutputStream(bos);
try {
// Create a DataOuputStream to write the audio data into the saved file.
min_buff = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
record = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, min_buff);
byte[] buffer = new byte[min_buff];
record.startRecording();
while (len < min_buff) {
int bufferReadResult = record.read(buffer, 0, min_buff);
for (int i = 0; i < bufferReadResult; i++){
Log.e("Reading", "Buffer Value :"+buffer[i]);
dos.writeShort(buffer[i]);
}
len += min_buff;
}
record.stop();
dos.close();
}catch (Exception e) {
Log.d("AptioudioRecord","Recording Failed " +e);
}
}
public void stopDownload(){
System.exit(0);
}
我还在清单文件中添加了权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
读取 record.read(buffer, 0, min_buff);
buffer
时仅填充 0
请帮助解决这个问题。
谢谢