以编程方式在android的audio歌曲混音器

我想创build一个audio混音器(DJ音乐曲目)的一种应用程序,可以创build一个audio歌曲的Dj混音器。 用户可以select一个音乐歌曲轨道,可以混合两个或更多独立的节奏,低音或节拍轨道创build一个新的修改的DJ音乐。

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

如果有人对此有一些想法或参考url,请分享。

Solutions Collecting From Web of "以编程方式在android的audio歌曲混音器"

Android上没有支持audio混合的内置库(将两个audioinputstream合并为一个输出stream)。 支持audio混合的Java javax.sound库没有移植到Android–有关Google Groups与Google工程师Diane Hackborn关于不将javax.sound移植到Android的决定的一个有趣讨论 。

看起来你必须从头开始自己的解决scheme。 关于如何将两个audiostream合并为一个,有几个有用的答案:

混合audio文件

Android中的audio编辑

Android – 混合多个静态波形到一个单一的AudioTrack

听起来最难的部分是一次播放多首歌曲,剩下的事情可以通过用户界面来完成。 一个可能帮助你的链接是如何同时播放多个ogg或mp3 ..? SoundPool的文档可以让你一次播放多个声音,可以在这里find。

 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(); } }