android - 同时播放两种声音-Android

标签 android audio merge android-mediaplayer mixing

是否可以同时播放两个声音(mp3)文件?我尝试使用两个不同的MediaPlayer对象-

MediaPlayer mediaPlayer;
MediaPlayer mediaPlayer2;

播放声音,但这不起作用。我不能使用SoundPool,因为每个声音文件都在10MB左右(因为SoundPool在3MB以上的声音文件中不能很好地工作)。

这是一些代码来熟悉我的情况-
@Override
public void onResume() {
    super.onResume();
    if(mediaPlayer == null)
    {
    mediaPlayer = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.song1);
    }
    if(mediaPlayer2 == null)
    {
    mediaPlayer2 = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.song2);
    }
}
private void startPlaying() {
mediaPlayer.setLooping(true);
mediaPlayer.start();
mediaPlayer2.start();
}

有什么建议么?有什么方法可以使这两个MediaPlayer对象起作用?如果没有,那么还有哪些其他选择?代码会有所帮助!

最佳答案

因此,我想,同时播放两个音频文件绝对是一个问题,我想,解决该问题的另一种方法是以编程方式将两个音频文件合并为一个,然后播放。事实证明,使用WAV文件实现起来很简单(MP3和其他压缩格式是否需要首先解压缩?)。无论如何,这是我的做法:

InputStream is = getResources().openRawResource(R.raw.emokylotheme); // Name of file 1

byte [] bytesTemp2 = fullyReadFileToBytes(new File(
Environment.getExternalStorageDirectory().getAbsolutePath()+
"/Kylo Ren/"+filename+"_morphed.wav")); // Name of file 2

byte [] sample2 = convertInputStreamToByteArray(is);
byte[] temp2 = bytesTemp2.clone();

RandomAccessFile randomAccessFile2 = new RandomAccessFile(new File(
Environment.getExternalStorageDirectory().getAbsolutePath()+
"/Kylo Ren/"+filename+"_morphed.wav"), "rw");

//seek to skip 44 bytes for WAV formats
randomAccessFile2.seek(44);
for (int n = 0; n < bytesTemp2.length; n++)
{
    bytesTemp2[n] = (byte) ((temp2[n] + (sample2[n])));
} 

randomAccessFile2.write(bytesTemp2);
randomAccessFile2.close();

以下是支持功能:
public byte[] convertInputStreamToByteArray(InputStream inputStream)
     {
     byte[] bytes= null;

     try
     {
     ByteArrayOutputStream bos = new ByteArrayOutputStream();

     byte data[] = new byte[1024];
     int count;

     while ((count = inputStream.read(data)) != -1)
     {
     bos.write(data, 0, count);
     }

    bos.flush();
     bos.close();
     inputStream.close();

    bytes = bos.toByteArray();
     }
     catch (IOException e)
     {
     e.printStackTrace();
     }
     return bytes;
     }


byte[] fullyReadFileToBytes(File f) throws IOException {
        int size = (int) f.length();
        byte bytes[] = new byte[size];
        byte tmpBuff[] = new byte[size];
        FileInputStream fis= new FileInputStream(f);
        try { 

            int read = fis.read(bytes, 0, size);
            if (read < size) {
                int remain = size - read;
                while (remain > 0) {
                    read = fis.read(tmpBuff, 0, remain);
                    System.arraycopy(tmpBuff, 0, bytes, size - remain, read);
                    remain -= read;
                } 
            } 
        }  catch (IOException e){
            throw e;
        } finally { 
            fis.close();
        } 

        return bytes;
    } 

从本质上讲,代码的作用是:它获取两个音频文件的字节(一个位于R.raw中的应用程序中,另一个位于外部存储目录中),对其求和,然后将新字节写入另一个文件。

此代码的问题在于它会产生一定数量的背景噪声。数量不多,但我相信某些点(也许是极值?)的字节求和会导致噪音。如果有人知道如何解决此问题,您可以编辑答案并告诉我。

附言这是用于具有戏剧性背景噪音效果的开源语音转换器应用程序,称为Kylo Ren语音转换器(https://github.com/advaitsaravade/Kylo-Ren-Voice-Changer)

关于android - 同时播放两种声音-Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25525330/

相关文章:

Android:通过 http 加载多个位图/缩略图的最快方法是什么?

java - 使用堆栈交换 API

r - 通过引用两列的条件组合 R 中的两个数据表

java - 如何使用两个主题?

android - 为什么tomcat依赖wi-fi

javascript - 模拟伪随机 LFO 信号建模(Javascript 中的低通滤波)

user-interface - 商业游戏软件中使用的库(SDL的替代品)

iphone - 确定 aurioTouch 示例应用程序中哪些频率对应于 x 轴

php - 需要在一个 JSON 中合并多行

python - 根据 pandas 索引范围合并行