即时音频输入和放大器;输出的Android [英] Immediate Audio Input & Output Android
问题描述
在我的Android应用程序,我想借此从智能手机的麦克风一些音频,并立即播放,生活,像一个麦克风,没有滞后。我目前想使用 AudioRecord
和 AudioTrack
类(从我读过),但我不是很的知道如何继续。
In my Android App, I would like to take in some audio from the mic of the smartphone and play it immediately, live, like a microphone, with no lag. I am currently thinking of using AudioRecord
and AudioTrack
classes (from what I have read), but I'm not quite sure how to proceed.
我检查了堆栈溢出一些其他问题,但他们并不确切地回答,我想做些什么。而且大多数是从2012年开始。
I checked out some other questions on Stack Overflow but they don't exactly answer what I would like to do. And most are from 2012.
所以,我怎么能使用这些类的输入和输出音频同步?
So how can I use these classes to input and output audio simultaneously?
同时我来看看 MediaRecorder API
,但是从我读,需要你的音频保存到文件,我不想做的事。它可以被tweeked,以满足我的要求?还是我最好只使用 AudioRecord
?
ALSO: I had a look at the MediaRecorder API
, but from what I read, that requires you to save the audio to a file, which I don't want to do. Can it be tweeked to meet my requirements? Or am I better off just using AudioRecord
?
感谢
编辑:
下面是我更新code以下为@Pradip Pramanick建议:
Here is my updated code below as @Pradip Pramanick suggested:
final Thread record = new Thread(new Runnable() {
@Override
public void run() {
while (!Thread.interrupted()) {
MediaRecorder microphone = new MediaRecorder();
microphone.setAudioSource(MediaRecorder.AudioSource.MIC);
microphone.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
microphone.setOutputFile(filename);
microphone.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
microphone.prepare();
} catch (IOException e) {
e.printStackTrace();
}
microphone.start();
}
}
});
final Thread play = new Thread(new Runnable() {
@Override
public void run() {
while (!Thread.interrupted()) {
player = new MediaPlayer();
try {
player.setDataSource(filename);
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
我收到一个非法状态异常|启动失败:-38
。但我打电话 microphone.start
在 prepare麦克风。
...似乎是什么问题?我搜索其他线程其中说有可能是使用麦克风等后台应用。我搜索我的设备:极限摩托播放(第3代),并没有发现。 (我甚至关掉确定谷歌语音识别,但错误不断涌现)。
I am getting an Illegal State Exception | Start failed: -38
. But I am calling microphone.start
after microphone.prepare
... What seems to be the problem? I searched other threads which said there might be other background apps using the microphone. I searched my device: Moto X Play (3rd Gen) and found none. (I even turned off "Ok Google" voice recognition, but the error kept coming).
错误日志:
下面是日志猫显示最新的错误:
Here is the log-cat showing the most recent errors:
01-31 09:37:21.064 344-3339/? E/MediaPlayerService: offset error
01-31 09:37:21.065 1835-1922/com.synerflow.testapp E/MediaPlayer: Unable to create media player
01-31 09:37:21.065 1835-1922/com.synerflow.testapp I/Player: player.prepare() has failed
01-31 09:37:21.065 1835-1922/com.synerflow.testapp W/System.err: java.io.IOException: setDataSourceFD failed.: status=0x80000000
的IO异常,似乎是在 player.setDataSource(文件名)
,文件名变量是一个字符串: Environment.getExternalStorageDirectory()getAbsolutePath ()+\\ voice.3gp
The IO Exception seems to be at player.setDataSource(filename)
, the filename variable is a string: Environment.getExternalStorageDirectory().getAbsolutePath() + "\voice.3gp"
推荐答案
至于我能想到它可以在一个非常简单的方式来完成。我还没有尝试过,但你试试吧。我认为它会工作:
As far as I can think it can be done in a very simple way. I haven't tried it,but you try it. I think it'll work:
创建两个线程一个用于录制另一个播放。
说线程TRecord和TPlay。
Create two threads one for recording another for playing. Say the threads are TRecord and TPlay.
在TRecord的run方法做到这一点:
In TRecord's run method do this :
public void run(){
MediaRecorder mRecorder = null;
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
//todo
}
mRecorder.start();
}
和它TPlay的run方法做到这一点:
And it TPlay's run method do this :
public void run() {
MediaPlayer mPlayer = null;
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
//todo
}
}
现在的mainactivity简单地创建两个线程。首先启动TRecord线程然后Tplay。试试吧。
Now on mainactivity simply create two threads. First start the TRecord thread then Tplay . Try it.
这里是code文件扩展名:
here is the code for file extension:
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
这篇关于即时音频输入和放大器;输出的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!