从wav文件播放原始PCM字节

编程入门 行业动态 更新时间:2024-10-26 10:36:59
本文介绍了从wav文件播放原始PCM字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我这是带16位PCM codeD wav文件,我想将其转换为PCM短阵并播放audiotrack类原料数组。这是我做过什么:

I have a wav file which was encoded with 16-bit PCM, I want to convert it to a PCM short array and play the raw array with audiotrack class. It is what I did:

String fileName = "test.wav"; File f = new File(baseDir + File.separator + fileName); InputStream is = null; try { is = new FileInputStream (f); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } BufferedInputStream bis = new BufferedInputStream (is, 8000); DataInputStream dis = new DataInputStream (bis); // Create a DataInputStream to read the audio data from the saved file short[] music = new short[(int) f.length()]; int i = 0; // Read the file into the "music" array try { while (dis.available() > 0) { music[i] = dis.readShort(); // This assignment does not reverse the order i++; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.i("123",""+Arrays.toString(music)); try { dis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } at=new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, 8000 /* 1 second buffer */, AudioTrack.MODE_STREAM); at.write(music, 0, (int)f.length()); at.play();

该阵列可以播放,但问题是,当我摆弄原来的wav文件进行比较,我发现他们不输出相同的声音。有人可以帮我解决这个问题?谢谢你在前进!

The array can be played, but the problem is that when I compare it with playing the original wav file, I found that they don't output the same sound. Can someone help me with this issue? Thank you in advance!

推荐答案

您必须设法文件中的实际音频数据,以获得音频采样看到的 en.wikipedia/wiki/WAV 或的 ccrma​​.stanford.edu/courses/422/projects/WaveFor​​mat/ 。

You have to seek to the actual audio data in the file to get the audio sample see en.wikipedia/wiki/WAV or ccrma.stanford.edu/courses/422/projects/WaveFormat/.

16位单率8kHz的一秒将是8000 * 2 * 1字节= 16000字节

One second of 16bit mono @ 8khz will be 8000 * 2 * 1 bytes = 16000 bytes

更多推荐

从wav文件播放原始PCM字节

本文发布于:2023-10-24 09:25:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523573.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字节   原始   文件   wav   PCM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!