[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Low latency audio makes games feel more realistic and responsive.\n\nComplete the following checklist to enable low latency audio in your game on\nAndroid:\n\n1. [Use Oboe](#oboe_api)\n2. [Request performance mode \"low latency\"](#low_latency_mode)\n3. [Request sharing mode \"exclusive\"](#exclusive_mode)\n4. [Use 48000 Hz or the Oboe sample rate converter](#sample_rate_conversion)\n5. [Set usage to AAUDIO_USAGE_GAME](#use_case)\n6. [Use data callbacks](#callback_function)\n7. [Avoid blocking operations in the callback](#avoid_blocking)\n8. [Tune buffer size to \"double buffer\"](#tune_buffer_size)\n\n1. Use the Oboe API\n-------------------\n\nThe [Oboe](https://github.com/google/oboe) API is a C++ wrapper that calls\nAAudio on Android 8.1 (API level 27) or higher. On earlier Android versions,\nOboe uses OpenSL ES.\n\nOboe is available on [GitHub](https://github.com/google/oboe) or as\na [prebuilt binary](https://github.com/google/oboe/blob/main/docs/GettingStarted.md#option-1-using-pre-built-binaries-and-headers).\nOboe also has a QuirksManager that corrects for problems on specific devices,\nwhich makes your app compatible with more devices. If you cannot use Oboe, use\nAAudio directly.\n\n2. Request low latency mode\n---------------------------\n\nWith Oboe or AAudio, request low latency mode. Otherwise, you get a higher\nlatency mode by default. \n\n### Oboe\n\n```css+lasso\nbuilder.setPerformanceMode(oboe::PerformanceMode::LowLatency);\n```\n\n### AAudio\n\n```scdoc\nAAudioStreamBuilder_setPerformanceMode(builder, AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);\n```\n\n3. Request exclusive mode\n-------------------------\n\nYou can also request exclusive access to the MMAP buffer. Your app may not get\nexclusive access, but if it does, your app writes directly into a buffer that\nis read by the DSP, which gives your app the lowest possible latency. \n\n### Oboe\n\n```css+lasso\nbuilder.setSharingMode(oboe::SharingMode::Exclusive);\n```\n\n### AAudio\n\n```scdoc\nAAudioStreamBuilder_setSharingMode(builder, AAUDIO_SHARING_MODE_EXCLUSIVE);\n```\n\n4. Avoid sample rate conversion\n-------------------------------\n\nUse the natural sample rate of the device. You can do this by not specifying a\nsample rate, and you almost certainly get 48000 Hz. If you do specify a sample\nrate, the audio framework sends your data on a different path that can have\nmuch higher latency.\n\nIf you do need to use a different sample rate, use Oboe to do the sample rate\nconversion: \n\n builder-\u003esetSampleRateConversionQuality(oboe::SampleRateConversionQuality::Medium);\n\n5. Properly declare your use case\n---------------------------------\n\nSpecifying the reason your app plays audio is critical for the system to apply\nthe right routing, volume, and performance settings. For example, games should\nindicate usage `AAUDIO_USAGE_GAME` to take full advantage of latency\noptimizations, especially when connected to Bluetooth headsets. \n\n### Oboe\n\n```css+lasso\nbuilder.setUsage(oboe::Usage::Game);\n```\n\n### AAudio\n\n```scdoc\nAAudioStreamBuilder_setUsage(builder, AAUDIO_USAGE_GAME);\n```\n\n6. Use a callback function\n--------------------------\n\nUse a callback for the output stream. If you use blocking writes and you are on\na device that does not support the AAudio MMAP mode, latency might be much\nhigher. \n\n### Oboe\n\n```text\nbuilder.setDataCallback(&myCallbackObject);\n```\n\n### AAudio\n\n```scdoc\nAAudioStreamBuilder_setDataCallback(builder, &my_callback_proc);\n```\n\n7. Avoid blocking in the callback\n---------------------------------\n\nWhen you use a low latency stream, the time between callbacks can be very\nshort, just a few milliseconds. So it is very important that you don't do\nanything in the callback that could block for a long time. If the callback is\nblocked, the buffer underflows and glitches occur in the audio.\n\nAvoid doing the following in a callback:\n\n- Allocating or freeing memory\n- File or network I/O\n- Waiting on a mutex or lock\n- Sleep\n- Heavy one-time CPU calculations\n\nThe callbacks should do math at an even pace for smooth playback without\nglitches.\n\n8. Tune the buffer size\n-----------------------\n\nOnce your app opens the audio stream, you need to tune the usable buffer size\nfor optimal latency. Oboe automatically sets the buffer size to two bursts. But\nwith AAudio, the default is much higher. Use double buffering by setting the\nbuffer size to twice the burst size. The burst size is the maximum callback\nsize.\n\nAAudio: \n\n int32_t frames = AAudioStream_getFramesPerBurst() * 2;\n AAudioStream_setBufferSizeInFrames(stream, frames);\n\nIf the buffer size is too small, you might get glitches caused by buffer\nunderruns. You can get a count of the glitches by calling\n`AAudioStream_getXRunCount(stream)`. Increase the buffer size as needed.\n\nSee the\n[GitHub Oboe docs](https://github.com/google/oboe/wiki/TechNote_BufferTerminology)\nfor an explanation of buffer‑related terminology.\n\nOpenSL ES\n---------\n\nIf you are supporting versions of Android before 8.1, you have to use\nOpenSL ES. If you are using Oboe, you can configure your app to improve\nthe latency. See\n[Obtaining optimal latency](https://github.com/google/oboe/blob/main/docs/GettingStarted.md#obtaining-optimal-latency)\nin the GitHub docs.\n\nChecklist results\n-----------------\n\nThe following table contains\n[OboeTester](https://play.google.com/store/apps/details?id=com.mobileer.oboetester)\nmeasurements of round-trip (input to output) latency.\n\n| Configuration | Latency (ms) |\n|-----------------------------------------|--------------|\n| Follow all recommendations | 20 |\n| Performance mode not low latency | 205 |\n| Not EXCLUSIVE (SHARED) | 26 |\n| 44100 Hz (AAudio) | 160 |\n| 44100 Hz (Oboe SRC) | 23 |\n| Not using an output callback (MMAP) | 21 |\n| Not using an output callback (not MMAP) | 62 |\n| Buffer size set to maximum | 53 |\n\n| **Note:** Results can vary greatly between different devices."]]