2012-11-05 29 views
18

Tôi muốn tạo một bộ trộn âm thanh (bản nhạc DJ) loại ứng dụng có thể tạo bộ trộn Dj của một bài hát âm thanh. Người dùng có thể chọn một bản nhạc bài hát có thể được trộn lẫn với hai hoặc nhiều nhịp điệu riêng biệt, âm bass hoặc nhịp bài hát để tạo ra một Dj nhạc mới sửa đổi.Trình trộn bài hát âm thanh trong android lập trình

Tôi đã thực hiện rất nhiều nghiên cứu về điều này nhưng không thể tìm thấy bất kỳ ý tưởng hay đầu mối nào.

Nếu có ai đó có ý tưởng hoặc một số URL tham chiếu về vấn đề này, vui lòng chia sẻ nó.

+0

Tuy nhiên chờ đợi bất kỳ đầu mối ..... :( –

Trả lời

2

Có vẻ như phần khó nhất trong số này sẽ phát nhiều bản nhạc cùng một lúc và phần còn lại có thể được thực hiện với giao diện người dùng. Một liên kết có thể giúp bạn là How to play multiple ogg or mp3 at the same time..? Tài liệu cho SoundPool, cho phép bạn phát nhiều âm thanh cùng lúc, có thể tìm thấy here.

+0

Thanks Dare for the reply ... Nhưng tôi không nghĩ soundPool là lựa chọn đúng để tạo ra âm thanh hỗn hợp của nhiều bài hát ... vì chúng tôi cần trộn các bài hát, nhịp điệu, nhịp điệu, bass trong một bản nhạc (ý tôi là như một đầu ra đơn ck). Hãy cho tôi biết quan điểm của bạn về điều này .... !!! –

+1

Có vẻ như nếu bạn muốn có một bản nhạc đầu ra, bạn chỉ cần thêm các bản nhạc vào với nhau và sửa lại để cắt. Hãy xem http://stackoverflow.com/questions/5126169/programmatically-merging-two-pieces-of-audio – Techwolf

12

Không có thư viện cài sẵn trên Android hỗ trợ trộn âm thanh (kết hợp hai luồng đầu vào âm thanh vào một luồng đầu ra). Thư viện Java javax.sound hỗ trợ trộn âm thanh không được chuyển sang Android - có một thú vị discussion trên Google Groups với kỹ sư Google Diane Hackborn về quyết định không chuyển javax.sound sang Android.

Dường như bạn phải phát triển giải pháp của riêng mình từ đầu. Có một số câu trả lời hữu ích về SO về cách kết hợp hai dòng suối âm thanh vào một:

Mixing Audio Files

Audio editing in Android

Android - Mixing multiple static waveforms into a single AudioTrack

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

Xin chào, bạn nên thêm một số thông tin vào câu trả lời của mình, chẳng hạn như nơi bạn nhận được mã và nó là gì đang làm. – Cat

Các vấn đề liên quan