简体   繁体   English

如何使用 Android MediaPlayer 按字节块播放音频数据

[英]how to use Android MediaPlayer to play audio data by chunks of bytes

I'm new to audio format.我是音频格式的新手。

I'm trying to play audio data on device A as I receive it from device B , chunk by chunk.我正在尝试在设备 A 上播放音频数据,因为我从设备 B 接收到它,一个块一个块。 The problem is simply playing chunk by chunk does not produce any sound.问题是简单地逐块播放不会产生任何声音。 If I combine all the chunks and then play, it works fine.如果我组合所有块然后播放,它工作正常。 But I want to make the application play real-time data, meaning I want to play the partial audio data the moment I receive it, even though the compete audio file has not been sent.但是我想让应用程序播放实时数据,这意味着我想在收到它的那一刻播放部分音频数据,即使完整的音频文件尚未发送。 I guess the chunk has to be in certain format to be played.我想块必须采用某种格式才能播放。

I searched on the net and learnt that MPEG_4 can be played in chunks as long as every chunk has a header.我在网上搜索并了解到只要每个块都有一个标题,就可以分块播放 MPEG_4。 I wonder if it's possible that I add a header to each chunk manually.我想知道是否可以手动为每个块添加一个标题。

I tried to record multiple samples and noticed they all have common bytes at the beginning as below:我尝试记录多个样本,并注意到它们在开头都有共同的字节,如下所示:

0000 0018 6674 7970 6d70 3432 0000 0000
6973 6f6d 6d70 3432 0000 ....the rest..

Are these common bytes the header ? I wonder if there is a certain way that device A can tell if a certain chunk can be played so I can play the chunk as soon as I receive it ?这些公共字节是头吗?我想知道设备A是否可以通过某种方式判断某个块是否可以播放,以便我一收到它就可以播放该块?

Any thought on this question would help!关于这个问题的任何想法都会有所帮助! Thanks in advance!提前致谢!

I would like to answer the question myself.我想自己回答这个问题。

First of all, do not stick with MediaPlayer as it can not record PCM file.首先,不要坚持使用 MediaPlayer,因为它不能录制 PCM 文件。

Actually a PCM file satisfies all the requirement since it applies a raw file format (meaning it is not compressed).实际上,PCM 文件满足所有要求,因为它应用原始文件格式(意味着它没有被压缩)。 One can play the PCM file frame by frame.可以逐帧播放PCM文件。 The size of the frame(eg 640 bytes) is fixed once the sampling rate and other params are defined in the recorder and player.一旦在记录器和播放器中定义了采样率和其他参数,帧的大小(例如 640 字节)就固定了。

Also checkout AudioRecord and AudioTrack class as it can record and play PCM files.还要检查AudioRecordAudioTrack类,因为它可以记录和播放 PCM 文件。

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

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