public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int minBufferSize = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);
playfilesound();
}
private void playfilesound() throws IOException
{
int count = 512 * 1024; // 512 kb
//Reading the file..
byte[] byteData = null;
File file = null;
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+"recordsound"); //filePath
byteData = new byte[(int)count];
FileInputStream in = null;
try {
in = new FileInputStream( file );
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int bytesread = 0, ret = 0;
int size = (int) file.length();
audioTrack.play();
while (bytesread < size) { // Write the byte array to the track
ret = in.read( byteData,0, count); //ret =size in bytes
if (ret != -1) {
audioTrack.write(byteData,0, ret);
bytesread += ret; } //ret
else break;
} //while
in.close();
audioTrack.stop(); audioTrack.release();
} 我使用调试器遍历代码并将鼠标悬停在audioTrack上,它是分配和初始化的。该文件也存在。
但是当它遇到audioTrack.play()时,它会抛出一个错误,说明它的状态异常是非法的,未使用AudioTrack。
我附上了项目,其中包括录音文件的一部分。http://www.mediafire.com/?6i2r3whg7e7rs79
发布于 2017-02-02 08:24:18
您使用的频道配置已停止,取而代之的是AudioFormat.CHANNEL_CONFIGURATION_MONO使用AudioFormat.CHANNEL_IN_MONO录制,AudioFormat.CHANNEL_OUT_MONO播放...
发布于 2013-08-19 23:21:07
看起来你在写之前调用了play!试试这个。
int bytesread = 0, ret = 0;
int size = (int) file.length();
//audioTrack.play(); <---- play called prematurely
while (bytesread < size) { // Write the byte array to the track
ret = in.read( byteData,0, count); //ret =size in bytes
if (ret != -1) {
audioTrack.write(byteData,0, ret);
bytesread += ret;
audioTrack.play(); //<--- try calling it here!
} //ret
else break;
} //while发布于 2020-03-23 09:30:10
您在这里有多个问题:
请同时格式化您的代码。
编辑:我还添加了
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO);以使播放更加柔和。然后,您可以再次将其设置为默认值:
android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);https://stackoverflow.com/questions/15145229
复制相似问题