【问题标题】:Audio song mixer in android programmaticallyandroid中的音频歌曲混合器以编程方式
【发布时间】:2012-11-05 08:09:56
【问题描述】:

我想创建一个混音器(DJ 音乐曲目)类的应用程序,它可以创建音频歌曲的 Dj 混音器。用户可以选择一个音乐歌曲轨道,该轨道可以与两个或多个单独的节奏、贝司或节拍轨道混合,以创建新的修改 Dj 音乐。

我对此进行了大量研究,但找不到任何想法或线索。

如果有人对此有任何想法或参考网址,请分享。

【问题讨论】:

  • 仍在等待任何线索..... :(

标签: android audio audio-player


【解决方案1】:

Android 上没有支持音频混合(将两个音频输入流合并为一个输出流)的内置库。支持音频混合的 Java javax.sound 库未移植到 Android - Google 论坛上有一个有趣的 discussion 与 Google 工程师 Diane Hackborn 关于不将 javax.sound 移植到 Android 的决定。

看来您必须从头开始开发自己的解决方案。关于如何将两个音频流合二为一的 SO,有几个有用的答案:

Mixing Audio Files

Audio editing in Android

Android - Mixing multiple static waveforms into a single AudioTrack

【讨论】:

    【解决方案2】:

    听起来最难的部分是一次播放多首曲目,其余的可以通过 UI 完成。 How to play multiple ogg or mp3 at the same time..? 可能对您有所帮助的一个链接可以在 here 找到 SoundPool 的文档,它可以让您一次播放多个声音。

    【讨论】:

    • 感谢 Dare 的回复...但我不认为 soundPool 是创建多轨混合声音的正确选择...因为我们需要混合歌曲、节拍、节奏、贝司在单个轨道中(我的意思是作为单个输出轨道)。让我知道你对此的看法......!!!
    • 看来,如果您想要单个输出轨道,您只需将轨道添加在一起并纠正剪辑即可。看看stackoverflow.com/questions/5126169/…
    【解决方案3】:

    虽然晚了,但如果有人需要,可以使用AudioMixer-android。

    【讨论】:

    • 这是否支持在混音时在单个轨道上设置音量平衡(平移)?
    【解决方案4】:
    File dir;
    dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
    dir.mkdirs();
    
    //Audio Mixer for two .raw file into single .wav file...
    void AudioMixer() {
        File file_play1 = new File(dir, "Neww.raw");
        int shortSizeInBytes = Short.SIZE / Byte.SIZE;
        int bufferSizeInBytes = (int) (file_play1.length() / shortSizeInBytes);
        short[] audioData = new short[bufferSizeInBytes];
    
        try {
            InputStream inputStream = new FileInputStream(file_play1);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);
    
            InputStream inputStream1 = getResources().openRawResource(R.raw.trainss); //Play form raw folder
            BufferedInputStream bufferedInputStream1 = new BufferedInputStream(inputStream1);
            DataInputStream dataInputStream1 = new DataInputStream(bufferedInputStream1);
    
            int i = 0;
            while (dataInputStream.available() > 0 && dataInputStream1.available() > 0) {
                audioData[i] = (short) (dataInputStream.readShort() + dataInputStream1.readShort());
                i++;
            }
    
            dataInputStream.close();
            dataInputStream1.close();
            AudioTrack audioTrack = new AudioTrack(
                    AudioManager.STREAM_MUSIC,
                    11025,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT,
                    bufferSizeInBytes,
                    AudioTrack.MODE_STREAM);
            audioTrack.write(audioData, 0, bufferSizeInBytes);
    
            //merge two .raw files in  single .raw file...
            File file_record = new File(dir, "testing.raw");
            try {
                OutputStream outputStream = new FileOutputStream(file_record);
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
                DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);
    
                for (int j = 0; j < audioData.length; j++) {
                    dataOutputStream.writeShort(audioData[j]);
                }
                dataOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            //Convert that .raw (testing.raw) file into .wav (testingNew.wav) file 
            File des = new File(dir, "testingNew.wav");
            try {
                rawToWave(file_record, des);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }
    
    //convert .raw file to .wav File...
    private void rawToWave(final File rawFile, final File waveFile) throws IOException {
        byte[] rawData = new byte[(int) rawFile.length()];
        DataInputStream input = null;
        try {
            input = new DataInputStream(new FileInputStream(rawFile));
            input.read(rawData);
        } finally {
            if (input != null) {
                input.close();
            }
        }
    
        DataOutputStream output = null;
        try {
            output = new DataOutputStream(new FileOutputStream(waveFile));
            // WAVE header
            writeString(output, "RIFF"); // chunk id
            writeInt(output, 36 + rawData.length); // chunk size
            writeString(output, "WAVE"); // format
            writeString(output, "fmt "); // subchunk 1 id
            writeInt(output, 16); // subchunk 1 size
            writeShort(output, (short) 1); // audio format (1 = PCM)
            writeShort(output, (short) 1); // number of channels
            writeInt(output, SAMPLE_RATE); // sample rate
            writeInt(output, SAMPLE_RATE * 2); // byte rate
            writeShort(output, (short) 2); // block align
            writeShort(output, (short) 16); // bits per sample
            writeString(output, "data"); // subchunk 2 id
            writeInt(output, rawData.length); // subchunk 2 size
            // Audio data (conversion big endian -> little endian)
            short[] shorts = new short[rawData.length / 2];
            ByteBuffer.wrap(rawData).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
            ByteBuffer bytes = ByteBuffer.allocate(shorts.length * 2);
            for (short s : shorts) {
                bytes.putShort(s);
            }
            output.write(bytes.array());
        } finally {
            if (output != null) {
                output.close();
            }
        }
    }
    
    private void writeInt(final DataOutputStream output, final int value) throws IOException {
        output.write(value >> 0);
        output.write(value >> 8);
        output.write(value >> 16);
        output.write(value >> 24);
    }
    
    private void writeShort(final DataOutputStream output, final short value) throws IOException {
        output.write(value >> 0);
        output.write(value >> 8);
    }
    
    private void writeString(final DataOutputStream output, final String value) throws IOException {
        for (int i = 0; i < value.length(); i++) {
            output.write(value.charAt(i));
        }
    }
    
    
    //playing merged file...
    private void playWavFile() {
        MediaPlayer recorded_audio_in_sounds = new MediaPlayer();
        String outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/testingNew.wav";
        try {
            if (recorded_audio_in_sounds != null) {
                if (recorded_audio_in_sounds.isPlaying()) {
                    recorded_audio_in_sounds.pause();
                    recorded_audio_in_sounds.stop();
                    recorded_audio_in_sounds.reset();
                    recorded_audio_in_sounds.setDataSource(outputFile);
                    recorded_audio_in_sounds.prepare();
                    recorded_audio_in_sounds.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    recorded_audio_in_sounds.start();
                    recorded_audio_in_sounds.start();
                } else {
                    recorded_audio_in_sounds.reset();
                    recorded_audio_in_sounds.setDataSource(outputFile);
                    recorded_audio_in_sounds.prepare();
                    recorded_audio_in_sounds.start();
                    recorded_audio_in_sounds.setVolume(2.0f, 2.0f);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

    • 您好,您应该在答案中添加一些信息,例如您从哪里获得代码以及它在做什么。
    猜你喜欢
    • 2012-01-28
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    相关资源
    最近更新 更多