在Android中使用AudioTrack播放WAV文件 [英] Using AudioTrack in Android to play a WAV file

查看:246
本文介绍了在Android中使用AudioTrack播放WAV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作与Android,试图让我的AudioTrack应用程序播放Windows .wav文件(Tada.wav)。坦率地说,它不应该是这样的辛苦,但我听到了很多奇怪的东西。该文件被保存在手机上的mini SD卡和阅读的内容似乎并不成为问题,但是当我播放文件(参数我只有pretty的SURE是正确的),我收到了几秒钟白噪声前的声音似乎自己解决到的东西,只可能是正确的。

I'm working with Android, trying to make my AudioTrack application play a Windows .wav file (Tada.wav). Frankly, it shouldn't be this hard, but I'm hearing a lot of strange stuff. The file is saved on my phone's mini SD card and reading the contents doesn't seem to be a problem, but when I play the file (with parameters I'm only PRETTY SURE are right), I get a few seconds of white noise before the sound seems to resolve itself into something that just may be right.

我已经成功记录和播放您自己的话音手机背面上 - 我根据本例中的指示创建了一个.PCM文件:

I have successfully recorded and played my own voice back on the phone -- I created a .pcm file according to the directions in this example:

<一个href="http://emeadev.blogspot.com/2009/09/raw-audio-manipulation-in-android.html">http://emeadev.blogspot.com/2009/09/raw-audio-manipulation-in-android.html

(没有向后掩蔽)...

(without the backwards masking)...

任何人都有在网络上的一些建议或例子意识在Android ??

Anybody got some suggestions or awareness of an example on the web for playing a .wav file on an Android??

谢谢, R上。

推荐答案

我无意中发现了答案(坦白地说,通过尝试和放大器; ^ @我没想到会工作!),如果任何人的兴趣。在我原来的code(这是从这个例子得出在原来的文章的链接),该数据从像这样的文件读取:

I stumbled on the answer (frankly, by trying &^@! I didn't think would work), in case anybody's interested... In my original code (which is derived from the example in the link in the original post), the data is read from the file like so:

    InputStream             is  = new FileInputStream       (file);
    BufferedInputStream     bis = new BufferedInputStream   (is, 8000);
    DataInputStream         dis = new DataInputStream       (bis);      //  Create a DataInputStream to read the audio data from the saved file

    int i = 0;                                                          //  Read the file into the "music" array
    while (dis.available() > 0)
    {
        music[i] = dis.readShort();                                     //  This assignment does not reverse the order
        i++;
    }

    dis.close();                                                        //  Close the input stream

在这个版本中,乐[]是空头排列。因此,readShort()方法似乎有意义这里,由于数据是16位PCM ...但是,这似乎是该问题的机器人。我改变了code以下内容:

In this version, music[] is array of SHORTS. So, the readShort() method would seem to make sense here, since the data is 16-bit PCM... However, on the Android that seems to be the problem. I changed that code to the following:

     music=new byte[(int) file.length()];//size & length of the file
    InputStream             is  = new FileInputStream       (file);
    BufferedInputStream     bis = new BufferedInputStream   (is, 8000);
    DataInputStream         dis = new DataInputStream       (bis);      //  Create a DataInputStream to read the audio data from the saved file

    int i = 0;                                                          //  Read the file into the "music" array
    while (dis.available() > 0)
    {
        music[i] = dis.readByte();                                      //  This assignment does not reverse the order
        i++;
    }

    dis.close();                                                        //  Close the input stream

在这个版本中,乐[]是一个字节数组。我还告诉AudioTrack它的16位PCM数据,和我的机器人似乎没有有一个问题,写字节数组到这样构成的......反正一个AudioTrack,它终于听起来是正确的,所以如果有人别人想打的Windows声音在他们的Andr​​oid,出于某种原因,这就是解决方案。啊,字节序......

In this version, music[] is an array of BYTES. I'm still telling the AudioTrack that it's 16-bit PCM data, and my Android doesn't seem to have a problem with writing an array of bytes into an AudioTrack thus configured... Anyway, it finally sounds right, so if anyone else wants to play Windows sounds on their Android, for some reason, that's the solution. Ah, Endianness......

R上。

这篇关于在Android中使用AudioTrack播放WAV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆