我正在使用Android,试图让我的AudioTrack应用程序播放Windows .wav文件(Tada.wav)。坦率地说,这不应该这么难,但我听到了很多奇怪的事情。文件被保存在我手机的迷你SD卡上,读取文件内容似乎不是问题,但当我播放文件(参数我只确定是正确的)时,我得到了几秒钟的白噪声,然后声音似乎自动解析为可能是正确的东西。
我已经成功地在电话上录制并播放了我自己的声音--我根据本例中的说明创建了一个.pcm文件:
http://emeadev.blogspot.com/2009/09/raw-audio-manipulation-in-android.html
(没有向后掩码)...
有没有人对在安卓系统上播放.wav文件的网络例子有什么建议?
谢谢,R。
发布于 2010-10-15 01:01:04
我偶然发现了答案(坦率地说,通过尝试&^@!我没想到会成功),如果有人感兴趣的话...在我的原始代码(源自原始post中链接中的示例)中,数据是从文件中读取的,如下所示:
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
在这个版本中,music[]是SHORTS的数组。因此,readShort()方法在这里似乎是有意义的,因为数据是16位的PCM…然而,在Android上,这似乎是问题所在。我将该代码更改为以下代码:
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
在这个版本中,music[]是一个字节数组。我仍然告诉AudioTrack它是16位的PCM数据,而且我的安卓系统似乎不存在将字节数组写入这样配置的AudioTrack的问题……不管怎样,这听起来是对的,所以如果其他人想在他们的Android上播放Windows的声音,出于某种原因,这就是解决方案。啊,Endianness......
R.
发布于 2013-05-29 02:36:11
对于这个问题,我找到了很多长长的答案。我的最终解决方案,考虑到所有的剪切和粘贴都不是我的,归结为:
public boolean play() {
int i = 0;
byte[] music = null;
InputStream is = mContext.getResources().openRawResource(R.raw.noise);
at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,
minBufferSize, AudioTrack.MODE_STREAM);
try{
music = new byte[512];
at.play();
while((i = is.read(music)) != -1)
at.write(music, 0, i);
} catch (IOException e) {
e.printStackTrace();
}
at.stop();
at.release();
return STOPPED;
}
STOPPED只是作为重置暂停/播放按钮的信号返回的"true“。在类初始化器中:
public Mp3Track(Context context) {
mContext = context;
minBufferSize = AudioTrack.getMinBufferSize(44100,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
}
上下文就是来自调用活动的"this“。您可以在SD卡上使用FileInputStream,等等。我的文件在res/raw中
发布于 2010-10-14 01:18:43
在将文件的其余数据转储到缓冲区之前,是否跳过了文件的前44个字节?前44个字节是波头,如果你想播放它们,它们听起来像是随机噪声。
此外,您确定您正在创建的AudioTrack与您尝试播放的WAVE具有相同的属性(采样率、比特率、通道数等)?Windows实际上很好地在文件属性页面中为您提供了此信息:
https://stackoverflow.com/questions/3925030
复制相似问题