9

AudioTrack の 2 つのインスタンスを同時に実行する必要があります。異なる可変サンプルレートで再生しているため、これらは別々に実行する必要があります。それらを同じスレッドで実行すると、「順番に」実行されることがわかりました。それぞれ独自のスレッドで実行していますが、音声が途切れています。

2 つのインスタンスをうまくプレイさせるためのアイデアはありますか? そうでない場合は、異なるサンプルレートで再生したい場合でも、2 つの短いバッファーを 1 つに混合するためのヒントを教えてください。

4

1 に答える 1

12

一度に 4 つの audioTracks を再生していますが、正常に再生されているようです。HTC Desire 1.1ghz OC でのテスト。ただし、スレッドに問題が発生することがあります。4 人全員がプレイしている場合、スレッドに参加しようとしても 1 つが停止しないことがあります。さらにテストを行う必要があります。これは、特定のパスに記録されたwavファイルを再生するための私のクラスです

    package com.ron.audio.functions;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;

public class AudioPlayManager implements Runnable {

private File fileName;
private volatile boolean playing;

public AudioPlayManager() {
    super();
    setPlaying(false);
}

public void run(){
      // Get the length of the audio stored in the file (16 bit so 2 bytes per short)
      // and create a short array to store the recorded audio.
      int musicLength = (int)(fileName.length()/2);
      short[] music = new short[musicLength];

      try {
        // Create a DataInputStream to read the audio data back from the saved file.
        InputStream is = new FileInputStream(fileName);
        BufferedInputStream bis = new BufferedInputStream(is);
        DataInputStream dis = new DataInputStream(bis);

        // Read the file into the music array.
        int i = 0;
        while (dis.available() > 0) {
          music[i] = dis.readShort();
          i++;
        }

        // Close the input streams.
        dis.close();     

        // Create a new AudioTrack object using the same parameters as the AudioRecord
        // object used to create the file.
        AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 
                                                11025, 
                                               AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                               AudioFormat.ENCODING_PCM_16BIT, 
                                               musicLength, 
                                               AudioTrack.MODE_STREAM);
        // Start playback
        audioTrack.play();

        // Write the music buffer to the AudioTrack object
        while(playing){
            audioTrack.write(music, 0, musicLength);
        }

      }
      catch(Exception e){
          e.printStackTrace();
      }

}


public void setFileName(File fileName) {
    this.fileName = fileName;
}

public File getFileName() {
    return fileName;
}

public void setPlaying(boolean playing) {
    this.playing = playing;
}

public boolean isPlaying() {
    return playing;
}

}

于 2011-01-26T23:16:22.467 に答える