如何知道什么时候AudioTrack对象已播放完毕? [英] How to tell when AudioTrack object has finished playing?

查看:3829
本文介绍了如何知道什么时候AudioTrack对象已播放完毕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用AudioTrack类在Android中发挥PCM文件。我可以打只找到该文件,但我不能可靠地知道什么时候播放结束。 AudioTrack.getPlayState说,播放停止时,它也没有。我有同样的问题与AudioTrack.setNotificationMarkerPosition,我pretty的肯定,我的标记设置到文件的末尾(虽然我不能完全肯定,我这样做是正确)。同样地,继续播放时getPlaybackHeadPosition是在文件的结尾,并已停止递增。任何人都可以帮忙吗?

I'm trying to play a PCM file in Android using the AudioTrack class. I can get the file to play just find but I cannot reliably tell when playback has finished. AudioTrack.getPlayState says playback has stopped when it hasn't. I'm having the same problem with AudioTrack.setNotificationMarkerPosition, and I'm pretty sure my marker is set to the end of the file (although I'm not completely sure I'm doing it right). Likewise, playback continues when getPlaybackHeadPosition is at the end of the file and has stopped incrementing. Can anyone help?

推荐答案

我发现,使用audioTrack.setNotificationMarkerPosition(audioLength)和audioTrack.setPlaybackPositionUpdateListener为我工作。请参见下面的code:

I found that using audioTrack.setNotificationMarkerPosition(audioLength) and audioTrack.setPlaybackPositionUpdateListener worked for me. See the following code:

    // 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 audioLength = (int) (pcmFile.length() / 2);
    short[] audioData = new short[audioLength];
    DataInputStream dis = null;

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

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

        // Create a new AudioTrack using the same parameters as the AudioRecord.
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, RECORDER_SAMPLE_RATE, RECORDER_CHANNEL_OUT,
                                    RECORDER_AUDIO_ENCODING, audioLength, AudioTrack.MODE_STREAM);
        audioTrack.setNotificationMarkerPosition(audioLength);
        audioTrack.setPlaybackPositionUpdateListener(new OnPlaybackPositionUpdateListener() {
            @Override
            public void onPeriodicNotification(AudioTrack track) {
                // nothing to do
            }
            @Override
            public void onMarkerReached(AudioTrack track) {
                Log.d(LOG_TAG, "Audio track end of file reached...");
                messageHandler.sendMessage(messageHandler.obtainMessage(PLAYBACK_END_REACHED));
            }
        });

        // Start playback
        audioTrack.play();

        // Write the music buffer to the AudioTrack object
        audioTrack.write(audioData, 0, audioLength);

    } catch (Exception e) {
        Log.e(LOG_TAG, "Error playing audio.", e);
    } finally {
        if (dis != null) {
            try {
                dis.close();
            } catch (IOException e) {
                // don't care
            }
        }
    }

这篇关于如何知道什么时候AudioTrack对象已播放完毕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆