android播放本地的pcm

白小花同学 2014-09-30 04:49:02
我想播放科大讯飞的录音生成在本地的PCM,下面是我的方法,但是播放出来的全是噪音。求大神指导!
public void play() {

// Get the file we want to playback./storage/emulated/0/oral/pcm/14091714280386951041300.pcm
File file = new File("/storage/emulated/0/oral/pcm/14091009303394417878374.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()/2);
short[] music = new short[musicLength];
try {
// Create a DataInputStream to read the audio data back from the saved file.
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);

// Read the file into the music array.
int i = 0;
while (dis.available() > 0) {
music[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,
44100,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
musicLength*2,
AudioTrack.MODE_STREAM);
// Start playback
audioTrack.play();
// Write the music buffer to the AudioTrack object
audioTrack.write(music, 0, musicLength);
audioTrack.stop() ;

} catch (Throwable t) {
Log.e("AudioTrack","Playback Failed");
}
}
...全文
430 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ec_boy_HL 2015-08-22
  • 打赏
  • 举报
回复
AudioTrack的构造有问题,给你个例子: new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, wBufferSize, AudioTrack.MODE_STREAM); 1.讯飞合成的pcm音频只支持16k或者8k,sampleRate的值得看你用讯飞合成语音的时候设置的编码是16k还是8k; 2.wBufferSize的长度是有限制的,最小值=AudioTrack.getMinBufferSize(sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);这里面的参数跟你构造AudioTrack的参数必须一样。 audioTrack.write方法建议不要一次性全部写入,如果音频文件很大你就悲催了,输入源用InputStream就够了,封装那么多层纯属闲得蛋疼。如果播放的时长较长,stop之前必须要等到音频播放完成,可以通过setNotificationMarkerPosition和setPlaybackPositionUpdateListener监听播放进度判断是否播放完成
我是路吃 2015-08-20
  • 打赏
  • 举报
回复
哥们 ,最后如何解决了没?碰到同样的问题,解决了麻烦告知下
瓦塔系 2015-07-21
  • 打赏
  • 举报
回复
哥们 最后解决没 咋弄了 ?
白小花同学 2014-09-30
  • 打赏
  • 举报
回复
引用 3 楼 YKDSea 的回复:
// Start playback audioTrack.play(); // Write the music buffer to the AudioTrack object audioTrack.write(music, 0, musicLength); audioTrack.stop() ; 不应该是先write再play吗? 然后stop之前应该要wait一段时间让他播放完吧?
我用这个方法播放自己自己录制的可以,播放讯飞录制的就不行...
sky-Z 2014-09-30
  • 打赏
  • 举报
回复
// Start playback audioTrack.play(); // Write the music buffer to the AudioTrack object audioTrack.write(music, 0, musicLength); audioTrack.stop() ; 不应该是先write再play吗? 然后stop之前应该要wait一段时间让他播放完吧?
白小花同学 2014-09-30
  • 打赏
  • 举报
回复
忘了说了,讯飞的人说是16000或者8000的,我都试了,也都是噪音。而且播放是在listview的item中的,要有图片变换,用thread感觉不太好。
媒体盒子 2014-09-30
  • 打赏
  • 举报
回复
估计是声道或比特位数弄错了吧

80,356

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧