Android的:如何搭配2个音频文件,并与重现的Soundpool他们 [英] Android: How to mix 2 audio files and reproduce them with soundPool

查看:179
本文介绍了Android的:如何搭配2个音频文件,并与重现的Soundpool他们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开发使用一些声音在Android aplication和他们创造的音乐作品混合在一起。在beggining我尝试了在同一时间重现声音,但是当我在做的是,他们得到了disinchronized,阅读论坛,几个小时后,我意识到,重现几个声音在循环模式在同一时间的最好办法就是到音频文件合并在一个,然后再现它们作为一个单一的文件。

I'm trying to develop an Android aplication that uses some sounds and mix them together for creating music compositions. At the beggining I tried just to reproduce sounds at the same time, but when I was doing that, they got disinchronized, and after several hours reading forums I realized that the best way to reproduce several sounds at the same time in loop mode is just to merge the audio files in one and then reproduce them as a single file.

下面是code我使用的是:

Here is the code I'm using:

        //Oppening file 1
        InputStream is = getAssets().open("sounds/sound_1.ogg");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        //End Oppening file 1

        //Oppening file 2
        InputStream is_2 = getAssets().open("sounds/sound_2.ogg");
        int size_2 = is_2.available();
        byte[] buffer_2 = new byte[size_2];
        is_2.read(buffer_2);
        is_2.close();
        //End Oppening file 2

        //Get Bigger size and Merge Files
        int total_size =0;

        byte[] buffer_final;
        if(buffer.length>buffer_2.length){
            total_size = buffer_2.length;   
        }else{
            total_size = buffer.length-1;
        }

        buffer_final= new byte[total_size];

        for(int i=30; i<total_size-30;i++){


            float a =buffer[i];
            float b =buffer_2[i];
            float c = (a+b)/2;
            buffer_final[i]=(byte) c;      

        }

        FileOutputStream fos = new FileOutputStream(f);        
        fos.write(buffer_final);
        fos.close();

在这之后,使用的Soundpool我读生成的文件,并尝试重现它。

And after that, using soundPool I read the resulting file and try to reproduce it.

如果我用这个公式不合并文件(这样,只用一个声音,创建一个新的文件,然后阅读它,它完美的作品,所以我认为这个问题是在文件合并,这也许不是那么容易,因为只需添加字节在一起,然后除以2。

If i use this formula without merging the files (so, using only one sound, creating a new file, and then reading it, it works perfectly, so I assume that the problem is in the file merging, that maybe is not as easy as just adding the bytes together and then divide by 2.

这样做后,日志告诉我:

After doing this, the log tells me this:

无法加载样品(空)。

和OFC,当我尝试重现日志说,样品未准备好。

And ofc, when I try to reproduce the log says that the sample is not ready.

这个请任何帮助吗?我已经googleing了好几个星期,我无法解决这个问题...

Any help about this please? I've been googleing for it for weeks and I couldn't solve it ...

这是我第一次编程的Andr​​oid,而且音频,所以我有点失落。

It's my first time programming Android, and also Audio so I'm a bit lost.

问题是,我该怎么搭配这听起来?我在我做这件事的正确方法?哪里有问题?

The question would be, how can I mix this sounds? I'm I in the right way for doing it? Where is the problem?

感谢您的阅读和帮助:)

Thanks for reading and helping :)

推荐答案

如果您是使用原始的PCM数据,场均样本将正常工作。你需要做的是OGG格式文件转换为PCM缓冲器。如果你想保持输出作为OGG,你需要转换回大功告成了。

If you were using raw PCM data, averaging the samples would work fine. What you need to do is convert the OGG file into a PCM buffer first. If you'd like to keep the output as OGG, you'll need to convert that back after you're done.

我的经验是多为MP3库,而不是OGG,但是我听到了关于 JOrbis 的好东西。

My experience is mostly with MP3 libraries rather than OGG, but I hear good things about JOrbis.

这篇关于Android的:如何搭配2个音频文件,并与重现的Soundpool他们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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