简体   繁体   English

播放wav文件中的原始PCM字节

[英]play a raw PCM byte from a wav file

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. 我有一个用16位PCM编码的wav文件,我想将其转换为PCM短数组,并使用audiotrack类播放原始数组。 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();

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. 可以播放该数组,但是问题是,当我将其与播放原始的wav文件进行比较时,我发现它们不会输出相同的声音。 Can someone help me with this issue? 有人可以帮我解决这个问题吗? Thank you in advance! 先感谢您!

You have to seek to the actual audio data in the file to get the audio sample see http://en.wikipedia.org/wiki/WAV or https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ . 您必须查找文件中的实际音频数据以获取音频样本,请参见http://en.wikipedia.org/wiki/WAVhttps://ccrma.stanford.edu/courses/422/projects/WaveFormat/

One second of 16bit mono @ 8khz will be 8000 * 2 * 1 bytes = 16000 bytes 16位单声道@ 8khz的一秒将是8000 * 2 * 1字节= 16000字节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM