提供 API 以在应用中实现音频播放和录音的库。
本页记录了即将推出的音频库版本。Expo Audio 目前处于 alpha 阶段,可能会发生重大更改。
expo-audio
是一个跨平台音频库,用于访问设备的原生音频功能。
¥expo-audio
is a cross-platform audio library for accessing the native audio capabilities of the device.
请注意,如果耳机/蓝牙音频设备断开连接,音频会自动停止。
¥Note that audio automatically stops if headphones/bluetooth audio devices are disconnected.
¥Installation
-
npx expo install expo-audio
If you are installing this in an existing React Native app, start by installing expo
in your project. Then, follow the additional instructions as mentioned by the library's README under "Installation in bare React Native projects" section.
¥Configuration in app config
如果你在项目中使用配置插件(EAS 构建 或 npx expo run:[android|ios]
),则可以使用其内置的 配置插件 配置 expo-audio
。该插件允许你配置无法在运行时设置的各种属性,并且需要构建新的应用二进制文件才能生效。如果你的应用不使用 EAS Build,则你需要手动配置包。
¥You can configure expo-audio
using its built-in config plugin if you use config plugins in your project (EAS Build or npx expo run:[android|ios]
). The plugin allows you to configure various properties that cannot be set at runtime and require building a new app binary to take effect. If your app does not use EAS Build, then you'll need to manually configure the package.
{
"expo": {
"plugins": [
[
"expo-audio",
{
"microphonePermission": "Allow $(PRODUCT_NAME) to access your microphone."
}
]
]
}
}
Name | Default | Description |
---|---|---|
microphonePermission | "Allow $(PRODUCT_NAME) to access your microphone" | Only for: iOS A string to set the |
¥Usage
¥Playing sounds
import { useEffect, useState } from 'react';
import { View, StyleSheet, Button } from 'react-native';
import { useAudioPlayer } from 'expo-audio';
const audioSource = require('./assets/Hello.mp3');
export default function App() {
const player = useAudioPlayer(audioSource);
return (
<View style={styles.container}>
<Button title="Play Sound" onPress={() => player.play()} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#ecf0f1',
padding: 10,
},
});
¥Recording sounds
import { useState } from 'react';
import { View, StyleSheet, Button } from 'react-native';
import { useAudioRecorder, RecordingOptions, AudioModule, RecordingPresets } from 'expo-audio';
export default function App() {
const audioRecorder = useAudioRecorder(RecordingPresets.HIGH_QUALITY);
const record = async () => {
await audioRecorder.prepareToRecordAsync();
audioRecorder.record();
};
const stopRecording = async () => {
// The recording will be available on `audioRecorder.uri`.
await audioRecorder.stop();
};
useEffect(() => {
(async () => {
const status = await AudioModule.requestRecordingPermissionsAsync();
if (!status.granted) {
Alert.alert('Permission to access microphone was denied');
}
})();
}, []);
return (
<View style={styles.container}>
<Button
title={audioRecorder.isRecording ? 'Stop Recording' : 'Start Recording'}
onPress={audioRecorder.isRecording ? stopRecording : record}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#ecf0f1',
padding: 10,
},
});
¥Playing or recording audio in background
在 iOS 上,后台音频播放和录音只能在独立应用中使用,并且需要一些额外的配置。在 iOS 上,每个后台功能都需要 Info.plist 文件中 UIBackgroundModes
数组中的特殊键。在独立应用中,该数组默认为空,因此要使用后台功能,你需要向 app.json 配置添加适当的键。
¥On iOS, audio playback and recording in background is only available in standalone apps, and it requires some extra configuration.
On iOS, each background feature requires a special key in UIBackgroundModes
array in your Info.plist file.
In standalone apps this array is empty by default, so to use background features you will need to add appropriate keys to your app.json configuration.
请参阅启用后台音频播放的 app.json 示例:
¥See an example of app.json that enables audio playback in background:
{
"expo": {
...
"ios": {
...
"infoPlist": {
...
"UIBackgroundModes": [
"audio"
]
}
}
}
}
¥Notes on web usage
Chrome 上的 MediaRecorder 问题会生成缺少持续时间元数据的 WebM 文件。请参阅未解决的 Chromium 问题。
¥A MediaRecorder issue on Chrome produces WebM files missing the duration metadata. See the open Chromium issue.
MediaRecorder 编码选项和其他配置在浏览器之间不一致,在应用中使用 kbumsik/opus-media-recorder 或 ai/audio-recorder-polyfill 等 Polyfill 将改善你的体验。传递给 prepareToRecordAsync
的任何选项都将直接传递给 MediaRecorder API 以及 polyfill。
¥MediaRecorder encoding options and other configurations are inconsistent across browsers, utilizing a Polyfill such as kbumsik/opus-media-recorder or ai/audio-recorder-polyfill in your application will improve your experience. Any options passed to prepareToRecordAsync
will be passed directly to the MediaRecorder API and as such the polyfill.
Web 浏览器要求网站能够安全地提供服务,以便它们能够收听麦克风的声音。详细信息请参见 MediaDevices getUserMedia()
安全性。
¥Web browsers require sites to be served securely for them to listen to a mic. See MediaDevices getUserMedia()
security for more details.
import { useAudioPlayer, useAudioRecorder } from 'expo-audio';
Type: Record<string, RecordingOptions>
Constant which contains definitions of the two preset examples of RecordingOptions
, as implemented in the Audio SDK.
HIGH_QUALITY
RecordingPresets.HIGH_QUALITY = {
extension: '.m4a',
sampleRate: 44100,
numberOfChannels: 2,
bitRate: 128000,
android: {
outputFormat: 'mpeg4',
audioEncoder: 'aac',
},
ios: {
outputFormat: IOSOutputFormat.MPEG4AAC,
audioQuality: AudioQuality.MAX,
linearPCMBitDepth: 16,
linearPCMIsBigEndian: false,
linearPCMIsFloat: false,
},
web: {
mimeType: 'audio/webm',
bitsPerSecond: 128000,
},
};
LOW_QUALITY
RecordingPresets.LOW_QUALITY = {
extension: '.m4a',
sampleRate: 44100,
numberOfChannels: 2,
bitRate: 64000,
android: {
extension: '.3gp',
outputFormat: '3gp',
audioEncoder: 'amr_nb',
},
ios: {
audioQuality: AudioQuality.MIN,
outputFormat: IOSOutputFormat.MPEG4AAC,
linearPCMBitDepth: 16,
linearPCMIsBigEndian: false,
linearPCMIsFloat: false,
},
web: {
mimeType: 'audio/webm',
bitsPerSecond: 128000,
},
};
Parameter | Type |
---|---|
options | RecordingOptions |
statusListener(optional) | (status: RecordingStatus) => void |
AudioRecorder
Type: Class extends SharedObject<AudioEvents>
AudioPlayer Properties
boolean
Boolean value indicating whether audio sampling is supported on the platform.
boolean
A boolean describing if we are correcting the pitch for a changed rate.
AudioPlayer Methods
Parameter | Type | Description |
---|---|---|
seconds | number | The number of seconds to seek by. |
Seeks the playback by the given number of seconds.
Promise<void>
Parameter | Type | Description |
---|---|---|
rate | number | The playback rate of the audio. |
pitchCorrectionQuality(optional) | PitchCorrectionQuality | The quality of the pitch correction. |
Sets the current playback rate of the audio.
void
Type: Class extends SharedObject<RecordingEvents>
AudioRecorder Properties
AudioRecorder Methods
Returns a list of available recording inputs. This method can only be called if the Recording
has been prepared.
RecordingInput[]
A Promise
that is fulfilled with an array of RecordingInput
objects.
Returns the currently-selected recording input. This method can only be called if the Recording
has been prepared.
RecordingInput
A Promise
that is fulfilled with a RecordingInput
object.
Status of the current recording.
RecorderState
Parameter | Type |
---|---|
options(optional) | Partial<RecordingOptions> |
Prepares the recording for recording.
Promise<void>
Parameter | Type | Description |
---|---|---|
seconds | number | The time in seconds to stop recording at. |
Stops the recording once the specified time has elapsed.
void
Parameter | Type | Description |
---|---|---|
inputUid | string | The uid of a |
Sets the current recording input.
void
A Promise
that is resolved if successful or rejected if not.
Parameter | Type | Description |
---|---|---|
seconds | number | The time in seconds to start recording at. |
Starts the recording at the given time.
void
Stop the recording.
Promise<void>
Promise<PermissionResponse>
Promise<PermissionResponse>
An object obtained by permissions get and request functions.
Property | Type | Description |
---|---|---|
canAskAgain | boolean | Indicates if user can be asked again for specific permission. If not, one should be directed to the Settings app in order to enable/disable the permission. |
expires | PermissionExpiration | Determines time when the permission expires. |
granted | boolean | A convenience boolean that indicates if the permission is granted. |
status | PermissionStatus | Determines the status of the permission. |
Literal Type: string
Acceptable values are: 'default'
| 'amr_nb'
| 'amr_wb'
| 'aac'
| 'he_aac'
| 'aac_eld'
Literal Type: string
Acceptable values are: 'default'
| '3gp'
| 'mpeg4'
| 'amrnb'
| 'amrwb'
| 'aac_adts'
| 'mpeg2ts'
| 'webm'
Property | Type | Description |
---|---|---|
audioSampleUpdate | (data: AudioSample) => void | - |
playbackStatusUpdate | (status: AudioStatus) => void | - |
Property | Type | Description |
---|---|---|
allowsRecording | boolean | - |
interruptionMode | InterruptionMode | - |
playsInSilentMode | boolean | - |
shouldPlayInBackground | boolean | - |
shouldRouteThroughEarpiece | boolean | - |
Property | Type | Description |
---|---|---|
channels | AudioSampleChannel[] | - |
timestamp | number | - |
Type: string
or null
or object
shaped as below:
Property | Type | Description |
---|---|---|
headers(optional) | Record<string, string> | An object representing the HTTP headers to send along with the request for a remote audio source.
On web requires the |
uri(optional) | string | A string representing the resource identifier for the audio, which could be an HTTPS address, a local file path, or the name of a static audio file resource. |
Property | Type | Description |
---|---|---|
currentTime | number | - |
duration | number | - |
id | number | - |
isBuffering | boolean | - |
isLoaded | boolean | - |
loop | boolean | - |
mute | boolean | - |
playbackRate | number | - |
playbackState | string | - |
playing | boolean | - |
reasonForWaitingToPlay | string | - |
shouldCorrectPitch | boolean | - |
timeControlStatus | string | - |
Literal Type: string
Acceptable values are: 'constant'
| 'longTermAverage'
| 'variableConstrained'
| 'variable'
Literal Type: string
Acceptable values are: 'mixWithOthers'
| 'doNotMix'
| 'duckOthers'
Literal Type: multiple types
Permission expiration time. Currently, all permissions are granted permanently.
Acceptable values are: 'never'
| number
Literal Type: string
Acceptable values are: 'low'
| 'medium'
| 'high'
Property | Type | Description |
---|---|---|
canRecord | boolean | - |
durationMillis | number | - |
isRecording | boolean | - |
mediaServicesDidReset | boolean | - |
metering(optional) | number | - |
url | string | null | - |
Property | Type | Description |
---|---|---|
recordingStatusUpdate | (status: RecordingStatus) => void | -status: RecordingStatus |
Property | Type | Description |
---|---|---|
android | RecordingOptionsAndroid | Recording options for the Android platform. |
bitRate | number | The desired bit rate. Example
|
extension | string | The desired file extension. Example
|
ios | RecordingOptionsIos | Recording options for the iOS platform. |
numberOfChannels | number | The desired number of channels. Example
|
sampleRate | number | The desired sample rate. Example
|
web(optional) | RecordingOptionsWeb | Recording options for the Web platform. |
Property | Type | Description |
---|---|---|
audioEncoder | AndroidAudioEncoder | The desired audio encoder. See the |
extension(optional) | string | The desired file extension. Example
|
maxFileSize(optional) | number | The desired maximum file size in bytes, after which the recording will stop (but Example
|
outputFormat | AndroidOutputFormat | The desired file format. See the |
sampleRate(optional) | number | The desired sample rate. Example
|
Property | Type | Description |
---|---|---|
audioQuality | AudioQuality | number | The desired audio quality. See the |
bitDepthHint(optional) | number | The desired bit depth hint. Example
|
bitRateStrategy(optional) | number | The desired bit rate strategy. See the next section for an enumeration of all valid values of |
extension(optional) | string | The desired file extension. Example
|
linearPCMBitDepth(optional) | number | The desired PCM bit depth. Example
|
linearPCMIsBigEndian(optional) | boolean | A boolean describing if the PCM data should be formatted in big endian. |
linearPCMIsFloat(optional) | boolean | A boolean describing if the PCM data should be encoded in floating point or integral values. |
outputFormat(optional) | string | IOSOutputFormat | number | The desired file format. See the |
sampleRate(optional) | number | The desired sample rate. Example
|
Property | Type | Description |
---|---|---|
bitsPerSecond(optional) | number | - |
mimeType(optional) | string | - |
Property | Type | Description |
---|---|---|
error | string | null | - |
hasError | boolean | - |
id | number | - |
isFinished | boolean | - |
url | string | null | - |