0

我正在使用 AudioTrack 播放 .wav 文件。我有个问题。我将 setLoopPoints 设置为循环我的 .wav 文件,但它不起作用。

这是我的示例代码。

public class PleaseActivity extends Activity implements Runnable{
AudioTrack audioTrack;
public static final String MEDIA_PATH    = Environment.getExternalStorageDirectory().getAbsolutePath()+"/TEST";
/** Called when the activity is first created. */
  Button play_button, stop_button;
  File file = null;
  byte[] byteData = null;
  Boolean playing = false;
  int bufSize;
  AudioTrack myAT = null;
  Thread play_thread = null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    play_button = (Button) findViewById(R.id.btn1);
    stop_button = (Button) findViewById(R.id.btn2);


    file = new File(MEDIA_PATH+"/untitled1.wav");
    byteData = new byte[(int) file.length()];   
    FileInputStream in = null;

    try {
      in = new FileInputStream(file);
      in.read(byteData);
      in.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }


    initialize();


    play_button.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {

        play_thread.start();
      }
    });

    //
    stop_button.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        //
        if (myAT.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
          myAT.stop();
          play_thread = null;
          initialize();
        }
      }
    });

  }


  void initialize() {

    bufSize = android.media.AudioTrack.getMinBufferSize(44100,
            AudioFormat.CHANNEL_CONFIGURATION_STEREO,
        AudioFormat.ENCODING_PCM_16BIT);

    myAT = new AudioTrack(AudioManager.STREAM_MUSIC,
        44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
        AudioFormat.ENCODING_PCM_16BIT, bufSize,
        AudioTrack.MODE_STREAM);



    play_thread = new Thread(this);
  }


  public void run() {
    if (myAT != null) {

      myAT.play();
      myAT.setLoopPoints(0, byteData.length, 2);
      myAT.write(byteData, 0, byteData.length);


    }
  }


}

我可以很好地播放我的波形文件,但是 setLoopPoints 不起作用!任何人都可以帮助我..


我解决了这样的循环问题。我还有一个问题。每当我将数据写入音轨时,我的意思是每当重复音轨时,都会在第一部分添加一些诸如“滴答声”之类的噪音。我不知道如何消除这种噪音..有人知道如何解决吗?

class DLThread extends Thread
    {
        public void run() 
        {
            while(!DLThread.interrupted())
            {
                if (myAT != null) {
                      //

                      myAT.play();
                      myAT.flush();
                      myAT.write(byteData, 0, byteData.length);
                    }
            }


        }

    }
4

2 回答 2

1

公共 int setLoopPoints (int startInFrames, int endInFrames, int loopCount)

设置循环点和循环计数。循环可以是无限的。与 setPlaybackHeadPosition 类似,轨道必须停止或暂停才能更改位置,并且*必须使用 MODE_STATIC 模式*

于 2013-08-29T07:25:50.543 回答
1

勾号可能是 wav 文件头。尝试将播放偏移 44 个字节。

于 2014-11-16T02:07:54.860 回答