从机器人的mp3文件中获取PCM数据 [英] get pcm data from mp3 file in android

查看:442
本文介绍了从机器人的mp3文件中获取PCM数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单。如何从MP3文件得到PCM数据?

My question is simple. How do I get PCM data from the mp3 file?

现在我发现只有东北的方式从歌曲得到PCM字节,我加载和的MediaPlayer 发挥MP3,并试图通过<$ C $听听海浪的变化C> Visualizer.OnDataCaptureListener 。这个接口的方法传递一个字节[]字节参数。我想所有的这些字节保存到一个字节数组,然后我通过这个数组 AudioTrack.write 方法和 AudioTrack.play <玩了/ code>方法。事情是这样的:

Now I found only ne way to get PCM bytes from the song, I loaded and played mp3 by MediaPlayer and tried listen to the change of waves by Visualizer.OnDataCaptureListener . This interface's method passes a byte[] bytes parameter. I tried to save all these bytes into the one byte array and then I passed this array to AudioTrack.write method and played it by AudioTrack.play method. Something like this:

    mVisualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
                public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,
                        int samplingRate) {
                       // allBytes += bytes
                }
        public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {
               }
, Visualizer.getMaxCaptureRate() / 2, true, false);
     }

然后

AudioTrack tr = new AudioTrack(AudioManager.STREAM_MUSIC, 22050, 
                              AudioFormat.CHANNEL_OUT_STEREO,   
                              AudioFormat.ENCODING_PCM_16BIT,
                              allBytes.length, AudioTrack.MODE_STREAM);
tr.play();
tr.write(barr, 0, allBytes.length);

但遗憾的是我没有得到预期的效果。播放声音时,但也有很大的噪音中的声音,质量这么差,你不能真正确定哪些是唱歌那里。

But unfortunately I didn't get expected results. The sound is playing but there are a lot of noise in the sound, quality is so bad, you can't actually identify what is singing there.

但它是一些有点黑客方式获取PCM数据。你可以说,如果有一个正常的方法从我的MP3获得PCM数据?

But it was some kinda of "hack method" to get PCM data. Can you say if there is a normal method to get PCM data from my mp3?

推荐答案

我现在用的是mpg123的机库为我的MP3解码。据德coded到PCM数据短裤而不是字节。我相信你可以做转换。去codeR是非常快的,因为它使用NDK。

I am using the mpg123 native library for decoding my MP3. It decoded into PCM data in shorts not bytes. I am sure you can do the conversions. The decoder is very fast since it uses the NDK.

http://www.badlogicgames.com/word$p$pss / P = 446

此外,

我不知道你的文件有多大,但我会流,而不是把整首歌在一个大阵列中的字节到AudioTrack。

I am not sure how big your files are but I would stream the bytes into the AudioTrack instead of putting the whole song in one big array.

这篇关于从机器人的mp3文件中获取PCM数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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