2

我正在我现有的应用程序中构建一个在 MODE_STATIC 中使用 AudioTrack 类的播放器,因为我想实现时间拉伸和循环点功能。start() 和 stop() 的代码是可以的,但是当暂停时,如果我尝试恢复,再次调用 play(),状态栏保持固定并且不播放音频。

现在,来自文档:

public void pause()暂停音频数据的播放。未播放的数据不会被丢弃。对 play() 的后续调用将播放此数据。请参阅 >flush() 以丢弃此数据。

这似乎很容易理解,但有些东西让我无法理解。有人能帮我吗?

是否有必要创建布尔变量,如开始、播放、暂停、停止音频等?如果是,从 AudioTrack 类继承的方法的实用性在哪里?

在 MODE_STREAM 中,我使用上述布尔变量实现了该项目。但我需要 MODE_STATIC。

这是代码,谢谢:

Button playpause, stop;
SeekBar posBar;
int sliderval=0;
int headerOffset = 0x2C;
File file =new File(Environment.getExternalStorageDirectory(), "raw.pcm");
int fileSize = (int) file.length();
int dataSize = fileSize-headerOffset ;
byte[] dataArray = new byte[dataSize];
int posValue;
int dataBytesRead = initializeTrack();
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, 
        AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, dataBytesRead , AudioTrack.MODE_STATIC);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    playpause= (Button)(findViewById(R.id.playpause));
    stop= (Button)(findViewById(R.id.stop));

    posBar=(SeekBar)findViewById(R.id.posBar);

    // create a listener for the slider bar;
    OnSeekBarChangeListener listener = new OnSeekBarChangeListener() {
      public void onStopTrackingTouch(SeekBar seekBar) { }
      public void onStartTrackingTouch(SeekBar seekBar) { }
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (fromUser) { sliderval = progress;}
       }
    };

    // set the listener on the slider
    posBar.setOnSeekBarChangeListener(listener); }


public void toggleButtonSound(View button)
{
    switch (button.getId())
    {
        case R.id.playpause:
            play();
            break;

        case R.id.stop:
            stop();

            break;
    }

}

    private void stop() {
        if(audioTrack.getState()==AudioTrack.PLAYSTATE_PLAYING ||
                audioTrack.getState()==AudioTrack.PLAYSTATE_PAUSED || audioTrack.getState()==AudioTrack.PLAYSTATE_STOPPED)
         { audioTrack.stop();
        resetPlayer();}

}


    Context context;
    private double actualPos=0;

    public void pause() {}

    public void play()
    {   


        if (audioTrack.getPlayState()==AudioTrack.PLAYSTATE_PLAYING) 
        {   //Log.i("", "Play pressed in state "+audioTrack.getPlayState());
            audioTrack.pause();
            }
        else if (audioTrack.getPlayState()==AudioTrack.PLAYSTATE_PAUSED)
        {   //Log.i("", "Play pressed in state "+audioTrack.getPlayState());
            audioTrack.play(); 
        }
     else if (audioTrack.getPlayState()==AudioTrack.PLAYSTATE_STOPPED)
     {      //Log.i("", "Play pressed in state "+audioTrack.getPlayState());
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, dataSize, AudioTrack.MODE_STATIC);
        audioTrack.write(dataArray, 0, dataBytesRead);
        audioTrack.play();

        }
        posBar.setMax((int) (dataBytesRead/2));     // Set the Maximum range of the

            audioTrack.setNotificationMarkerPosition((int) (dataSize/2));
            audioTrack.setPositionNotificationPeriod(1000);
            audioTrack.setPlaybackPositionUpdateListener(new OnPlaybackPositionUpdateListener() {
          @Override
          public void onPeriodicNotification(AudioTrack track) { 
              posBar.setProgress(audioTrack.getPlaybackHeadPosition());
              Log.i("", " " + audioTrack.getPlaybackHeadPosition() + " " + dataBytesRead/2);
          }
            @Override
          public void onMarkerReached(AudioTrack track) {
                Log.i("", " End reached ");

                audioTrack.pause();
                audioTrack.flush();
                audioTrack.release();
                posBar.setProgress(0);
                resetPlayer();}
       });
    }



        private int initializeTrack() {
            InputStream is;
            BufferedInputStream bis;
            DataInputStream dis;
            int temp = 0;
            try {
                is = new FileInputStream(file);
                bis = new BufferedInputStream(is);
                dis = new DataInputStream(bis);
                temp = dis.read(dataArray, 0, dataSize);
                dis.close();
                bis.close();
                is.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return temp;
        }

        public void resetPlayer() {
            audioTrack.flush();
            audioTrack.release();
            posBar.setProgress(0);
            sliderval=0;
            }
4

1 回答 1

0

你看,你确实实现了 AudioTrack,所以即使它暂停文件的内容仍然上传到 AudioTrack:

我不知道它是如何管理它的,但就我而言,我也暂停将数据上传到 AT。喜欢:

            while (byteOffset < fileLengh) {
                if(isPaused)
                    continue;
                ret = in.read(byteData, 0, byteCount);
                if (ret != -1) { // Write the byte array to the track
                    audioTrack.write(byteData, 0, ret);
                    byteOffset += ret;
                } else
                    break;
            }

然后我在循环恢复时取消暂停文件上传。我想就是这样。另外我不得不提一下,即使 AT 正在播放以下内容:

if (audioTrack.getPlayState()==AudioTrack.PLAYSTATE_PLAYING)

if (audioTrack.getPlayState()==AudioTrack.PLAYSTATE_PAUSED)

对我不起作用,getPlayState() 总是为我返回 1 (AudioTrack.PLAYSTATE_STOPPED),无论它是播放还是暂停。

于 2014-08-25T20:01:59.917 回答