Ric*_*ich 24 android wav audio-streaming
我正在使用Android,试图让我的AudioTrack应用程序播放Windows .wav文件(Tada.wav).坦率地说,它应该不是那么难,但我听到很多奇怪的东西.该文件保存在手机的迷你SD卡上并且读取内容似乎不是问题,但是当我播放文件时(带参数我只是PRETTY SURE是正确的),我得到几秒钟的白噪声在声音似乎将自己解决为可能正确的事物之前.
我已经成功录制并在手机上播放了自己的声音 - 我根据此示例中的说明创建了一个.pcm文件:
http://emeadev.blogspot.com/2009/09/raw-audio-manipulation-in-android.html
(没有向后掩盖)......
任何人都有一些建议或意识到网上的一个例子在Android上播放.wav文件?
谢谢,R.
Ric*_*ich 26
我偶然发现了答案(坦率地说,通过尝试&^ @!我认为没有用),以防任何人感兴趣...在我的原始代码中(源自原帖中链接中的示例),从文件中读取数据,如下所示:
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
Run Code Online (Sandbox Code Playgroud)
在这个版本中,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
Run Code Online (Sandbox Code Playgroud)
在这个版本中,music []是一个BYTES数组.我还在告诉AudioTrack它是16位PCM数据,我的Android似乎没有将字节数组写入AudioTrack这样配置的问题......无论如何,它最终听起来是正确的,所以如果有人否则他们想在他们的Android上播放Windows声音,出于某种原因,这就是解决方案.啊,恩典......
R.
我在这个问题上找到了很多很长的答案.我的最终解决方案,即所有切割和粘贴都不是我的,归结为:
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;
}
Run Code Online (Sandbox Code Playgroud)
STOPPED只是作为重置暂停/播放按钮的信号发回的"真实".并在类初始化程序中:
public Mp3Track(Context context) {
mContext = context;
minBufferSize = AudioTrack.getMinBufferSize(44100,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
}
Run Code Online (Sandbox Code Playgroud)
上下文只是来自调用活动的"this".你可以在SD卡等上使用FileInputStream.我的文件是res/raw
在将剩余的文件数据转储到缓冲区之前,您是否正在跳过文件的前44个字节?前44个字节是WAVE标头,如果您尝试播放它们,它们听起来像随机噪音.
此外,您确定要创建的AudioTrack具有与您尝试播放的WAVE相同的属性(采样率,比特率,通道数等)吗?Windows实际上可以很好地在"文件属性"页面中向您提供此信息:
