保存音频的ByteArray成音频文件 [英] Saving ByteArray of audio into an Audio File

查看:267
本文介绍了保存音频的ByteArray成音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个测试应用程序在Android soundtouch(一个开源的音频处理库)的集成。

I'm working on a test app to integrate soundtouch (an open source audio processing library) on Android.

我的测试程序已经可以从麦克风接收输入,通过音频直通soundtouch和输出处理音频的AudioTrack实例。

My test app already can receive input from the mic, pass the audio thru soundtouch and output the processed audio to an AudioTrack instance.

我的问题是,我怎么可以改变从AudioTrack到一个新的文件输出设备上?

My question is, how can I change the output from AudioTrack to a new File on my device?

下面是我的应用程序相关的code(在那里我处理soundtouch的输出到输入的AudioTrack)

Here's the relevant code in my app (where I'm processing the output of soundtouch, into the input for AudioTrack)

// the following code is a stripped down version of my code
// in no way its supposed to compile or work.  Its here for reference purposes
// pre-conditions 
// parameters -  input : byte[]
soundTouchJNIInstance.putButes(input);
int bytesReceived = soundTouchJNIInstance.getBytes(input);
audioTrackInstance.write(input, 0, bytesReceived);

对如何处理这个问题的任何想法?谢谢!

Any ideas on how to approach this problem? Thanks!

推荐答案

您可以使用使用的SequenceInputStream的方法,在我的应用我刚刚合并的MP3文件于一体,并使用JNI库MPG123播放它,但我使用测试文件媒体播放器没有问题。

You can use the method using SequenceInputStream, in my app I just merge MP3 files in one and play it using the JNI Library MPG123, but I tested the file using MediaPlayer without problems.

这code是不是最好的,但它的工作原理...

This code isn't the best, but it works...

private void mergeSongs(File mergedFile,File...mp3Files){
        FileInputStream fisToFinal = null;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(mergedFile);
            fisToFinal = new FileInputStream(mergedFile);
            for(File mp3File:mp3Files){
                if(!mp3File.exists())
                    continue;
                FileInputStream fisSong = new FileInputStream(mp3File);
                SequenceInputStream sis = new SequenceInputStream(fisToFinal, fisSong);
                byte[] buf = new byte[1024];
                try {
                    for (int readNum; (readNum = fisSong.read(buf)) != -1;)
                        fos.write(buf, 0, readNum);
                } finally {
                    if(fisSong!=null){
                        fisSong.close();
                    }
                    if(sis!=null){
                        sis.close();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(fos!=null){
                    fos.flush();
                    fos.close();
                }
                if(fisToFinal!=null){
                    fisToFinal.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } 

这篇关于保存音频的ByteArray成音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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