Ret*_*ner 5 typescript react-native union-types expo expo-av
我是 TypeScript 新手,正在尝试使用expo-av进行音频播放。
以下代码给我一个错误:TS2339: Property 'error' does not exist on type 'AVPlaybackStatus'.
const { sound, status } = await Audio.Sound.createAsync(track.mp3);
if (status.isLoaded) {
console.info(status.durationMillis);
}
else {
console.error("track not loaded", status.error);
}
Run Code Online (Sandbox Code Playgroud)
我查看了定义status并发现:
export type AVPlaybackStatus =
| {
isLoaded: false;
androidImplementation?: string;
error?: string; // populated exactly once when an error forces the object to unload
}
| {
isLoaded: true;
androidImplementation?: string;
uri: string;
progressUpdateIntervalMillis: number;
durationMillis?: number;
positionMillis: number;
playableDurationMillis?: number;
seekMillisToleranceBefore?: number;
seekMillisToleranceAfter?: number;
shouldPlay: boolean;
isPlaying: boolean;
isBuffering: boolean;
rate: number;
shouldCorrectPitch: boolean;
volume: number;
isMuted: boolean;
isLooping: boolean;
didJustFinish: boolean; // true exactly once when the track plays to finish
};
Run Code Online (Sandbox Code Playgroud)
我以前从未见过这个构造,但我猜测这是一个并集或交集类型(?)。
我如何访问status.error?处理此类类型的典型习惯用法/结构是什么?
| 归档时间: |
|
| 查看次数: |
1639 次 |
| 最近记录: |