Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
蓝牙延迟和Android音频提示_Android_Audio_Bluetooth - Fatal编程技术网

蓝牙延迟和Android音频提示

蓝牙延迟和Android音频提示,android,audio,bluetooth,Android,Audio,Bluetooth,我的Android应用程序使用SoundPool播放简短的wav语音片段。对于某些已连接的蓝牙扬声器,初始播放可能会被截断,其余部分可以听到。通过本机电话扬声器输出良好 声音池的延迟很低。某些蓝牙扬声器可能会因电源管理中的睡眠而延迟。我主要考虑的是Bluetooth 4.x,目前使用的是Android 7.0,不过用户将使用其他版本 是否有最佳实践来弥补这种情况?我是否应该在语音剪辑中添加一个无噪音的引导(例如“X”毫秒),让蓝牙有时间唤醒,这样重要的部分就可以在之后正常听到?是否首选某些声音文

我的Android应用程序使用SoundPool播放简短的wav语音片段。对于某些已连接的蓝牙扬声器,初始播放可能会被截断,其余部分可以听到。通过本机电话扬声器输出良好

声音池的延迟很低。某些蓝牙扬声器可能会因电源管理中的睡眠而延迟。我主要考虑的是Bluetooth 4.x,目前使用的是Android 7.0,不过用户将使用其他版本

是否有最佳实践来弥补这种情况?我是否应该在语音剪辑中添加一个无噪音的引导(例如“X”毫秒),让蓝牙有时间唤醒,这样重要的部分就可以在之后正常听到?是否首选某些声音文件规格?游戏程序员有什么见解吗

谢谢

下面是初始化soundPool(使用声音ID的hashmap)和播放声音以及释放和停止soundPool的代码。在我的主程序中对playSound的一个示例调用是SoundPoolManager.getInstance().playSound(R.raw.\u whistle)

}

之后,我的应用程序运行的日志输出为:

03-09 13:08:34.160 2615-2615/com.foobar.foo E/SCOBroadcastReceiver:SCO_音频_状态_已连接 03-09 13:08:39.404 2615-2615/com.foobar.foo W/AudioTrack:AUDIO\u OUTPUT\u FLAG\u FAST被服务器拒绝;帧数70229 03-09 13:09:09.278 2615-2615/com.foobar.foo W/AudioTrack:AUDIO\u OUTPUT\u FLAG\u FAST被服务器拒绝;帧数78728 03-09 13:09:21.312 2615-2615/com.foobar.foo W/AudioTrack:AUDIO\u OUTPUT\u FLAG\u FAST被服务器拒绝;帧数79623 03-09 13:09:33.345 2615-2615/com.foobar.foo W/AudioTrack:AUDIO\u OUTPUT\u FLAG\u FAST被服务器拒绝;帧数73808 03-09 13:09:45.377 2615-2615/com.foobar.foo W/AudioTrack:AUDIO\u OUTPUT\u FLAG\u FAST被服务器拒绝;帧数43391 03-09 13:09:57.400 2615-2615/com.foobar.foo W/AudioTrack:AUDIO\u OUTPUT\u FLAG\u FAST被服务器拒绝;帧数46074 03-09 13:10:09.425 2615-2615/com.foobar.foo W/AudioTrack:AUDIO\u OUTPUT\u FLAG\u FAST被服务器拒绝;帧数46075


您应该先启动Bluetooth SCO,然后再播放。这样会及时唤醒扬声器。

嗨,Neo,Bluetooth SCO看起来非常有用,我在我的原始帖子中提供了代码,以便您可以显示插入位置。我已经将“修改音频设置”权限添加到我的清单中。我不在电脑附近。我认为这是一个异步调用。因此可能需要先调用它,然后获取回调结果。在那之后,开始游戏,在这方面努力工作,非常感谢你的帮助。。。我添加了一个SCOBroadcastReceiver,并在我的日志中看到SCO_AUDIO_STATE_CONNECTED,然后播放带有持续截断问题的声音。SCO_音频_状态_已断开连接未显示。还有其他想法吗?服务器是否拒绝音频输出?是否有人能提供帮助?问题还没有解决。你解决了吗?
public void InitializeSoundPool(Activity activity, final ISoundPoolLoaded callback) throws Exception {
    if (sounds == null || sounds.size() == 0) {
        throw new Exception("Sounds not set");
    }

    int maxStreams = 1;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        soundPool = new SoundPool.Builder()
                .setMaxStreams(maxStreams)
                .build();
    } else {
        soundPool = new SoundPool(maxStreams, AudioManager.STREAM_MUSIC, 0);
    }

    soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId,
                                   int status) {
            SoundSampleEntity entity = getEntity(sampleId);
            if (entity != null) {
                entity.setLoaded(status == 0);
            }

            if (sampleId == maxSampleId()) {
                callback.onSuccess();
            }
        }
    });
    int length = sounds.size();
    hashMap = new HashMap<Integer, SoundSampleEntity>();
    int index;
    for (index = 0; index < length; index++) {
        hashMap.put(sounds.get(index), new SoundSampleEntity(0, false));
    }
    index = 0;
    for (Map.Entry<Integer, SoundSampleEntity> entry : hashMap.entrySet()) {
        index++;
        entry.getValue().setSampleId(soundPool.load(activity, entry.getKey(), index));
    }
}

public void playSound(int resourceId) {
    if (isPlaySound()) {
        SoundSampleEntity entity = hashMap.get(resourceId);
        if (entity.getSampleId() > 0 && entity.isLoaded()) {
            soundPool.play(entity.getSampleId(), .99f, .99f, 1, 0, 1f);
        }
    }
}

public void release() {
    if (soundPool != null) {
        soundPool.release();
    }
}

public void stop() {
    if (soundPool != null) {
        for (Map.Entry<Integer, SoundSampleEntity> entry : hashMap.entrySet()) {
            SoundSampleEntity entity = entry.getValue();
            soundPool.stop(entity.getSampleId());
        }
    }
}
public class SCOBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1) ==
            AudioManager.SCO_AUDIO_STATE_CONNECTED) {
        // SCO now connected
        SoundPoolManager.isPlaySound = true;
        Log.e("SCOBroadcastReceiver", "SCO_AUDIO_STATE_CONNECTED");
    } else if (intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1) ==
            AudioManager.SCO_AUDIO_STATE_DISCONNECTED) {
        // SCO now disconnected
        SoundPoolManager.isPlaySound = false;
        Log.e("SCOBroadcastReceiver", "SCO_AUDIO_STATE_DISCONNECTED");
    }
}