Android AudioTrack错误:未初始化AudioTrack public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); int minBufferSize=AudioTrack.getMinBufferSize(44100,AudioFormat.CHANNEL\u配置\u MONO, 音频格式。编码(PCM(16位); audioTrack=新的audioTrack(AudioManager.STREAM\u MUSIC,44100,AudioFormat.CHANNEL\u CONFIGURATION\u MONO, AudioFormat.ENCODING_PCM_16位、minBufferSize、AudioTrack.MODE_STREAM); 播放文件(); } 私有void playfilesound()引发IOException { int count=512*1024;//512 kb //正在读取文件。。 字节[]字节数据=null; File=null; file=新文件(Environment.getExternalStorageDirectory().getAbsolutePath()+“/”+“recordsound”);//文件路径 byteData=新字节[(int)计数]; FileInputStream in=null; 试一试{ in=新文件输入流(文件); }catch(filenotfounde异常){ //TODO自动生成的捕捉块 e、 printStackTrace(); } int字节读取=0,ret=0; int size=(int)file.length(); 音轨播放(); 而(bytesread
我使用调试器一步一步地完成代码并将鼠标悬停在音频轨道上,它被分配并初始化。该文件也存在 但当它点击audioTrack.play()时,它抛出一个错误,表示其非法状态异常为未使用的audioTrack 我附上的项目,其中包括记录文件部分。Android AudioTrack错误:未初始化AudioTrack public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); int minBufferSize=AudioTrack.getMinBufferSize(44100,AudioFormat.CHANNEL\u配置\u MONO, 音频格式。编码(PCM(16位); audioTrack=新的audioTrack(AudioManager.STREAM\u MUSIC,44100,AudioFormat.CHANNEL\u CONFIGURATION\u MONO, AudioFormat.ENCODING_PCM_16位、minBufferSize、AudioTrack.MODE_STREAM); 播放文件(); } 私有void playfilesound()引发IOException { int count=512*1024;//512 kb //正在读取文件。。 字节[]字节数据=null; File=null; file=新文件(Environment.getExternalStorageDirectory().getAbsolutePath()+“/”+“recordsound”);//文件路径 byteData=新字节[(int)计数]; FileInputStream in=null; 试一试{ in=新文件输入流(文件); }catch(filenotfounde异常){ //TODO自动生成的捕捉块 e、 printStackTrace(); } int字节读取=0,ret=0; int size=(int)file.length(); 音轨播放(); 而(bytesread,android,audiotrack,Android,Audiotrack,我使用调试器一步一步地完成代码并将鼠标悬停在音频轨道上,它被分配并初始化。该文件也存在 但当它点击audioTrack.play()时,它抛出一个错误,表示其非法状态异常为未使用的audioTrack 我附上的项目,其中包括记录文件部分。 看起来你在写之前就已经打电话给play了!试试这个 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setConte
看起来你在写之前就已经打电话给play了!试试这个
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();
}
int字节读取=0,ret=0;
int size=(int)file.length();
//音轨播放() 您使用的频道配置已停止,取而代之的是音频格式。频道配置\u MONO
使用音频格式。频道\u in \u MONO
录制和音频格式。频道\u OUT \u MONO
播放…您遇到了不止一个问题:
- 已中断的频道部分尚未应答
- 不要在UI线程上播放音频李>
请同时格式化您的代码
编辑:
我还补充说
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
使播放更柔和。
然后可以再次将其设置为默认值:
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO);
请使用英语,在这里只使用英语。请帮我一个忙,实用英语。
android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);