我正在尝试基于一个名为冥想列表的函数动态播放声音。该函数的返回为我提供了它在播放器中工作的正确路径。
但是,如果我调用 URL 上的函数不起作用,则会出现以下错误:拒绝,找不到变量冥想列表。我还尝试使用 JSON.stringify 返回 adress.meditation.meditationPlayerAdress 但没有运气
export default class Player extends React.Component<Props> {
meditationList = () => {
const adress = this.props.route.params;
return adress.meditation.meditationPlayerAdress; //console.log returns ../../assets/track1.mp3
};
audio = () => {
TrackPlayer.setupPlayer().then(async () => {
// Adds a track to the queue
await TrackPlayer.add({
id: 'trackId',
url: require('../../assets/track1.mp3'), // this.require(meditationList())- warning that variable meditationList cannot be found
title: 'Track Title',
artist: 'Track Artist',
});
let trackId = await TrackPlayer.getCurrentTrack();
// Starts playing it
TrackPlayer.play();
});
};
pause = () => {
TrackPlayer.pause();
};
stop = () => {
TrackPlayer.stop();
};
render() {
return (
<SafeAreaView style={styles.container}>
<View style={{alignItems: 'center'}}>
<View style={{alignItems: 'center', marginTop: 24}}>
<Text style={[styles.textLight, {fontSize: 12}]} />
<Text
style={[
styles.text,
{fontSize: 15, fontWeight: '500', marginTop: 8},
]}>
{this.meditationList()}
</Text>
</View>
<View style={styles.coverContainer}>
<Image
source={require('../../../assets/coverArt.jpg')}
style={styles.cover}
/>
</View>
<View style={{alignItems: 'center', marginTop: 32}}>
<Text style={[styles.textDark, {fontSize: 20, fontWeight: '500'}]}>
Exhale
</Text>
<Text style={[styles.text, {fontSize: 16, marginTop: 8}]}>sal</Text>
</View>
</View>
<View
style={{
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
marginTop: 16,
}}>
<TouchableOpacity onPress={this.stop}>
<FontAwesomeIcon icon={faStop} size={32} color="#93A8B3" />
</TouchableOpacity>
<TouchableOpacity
style={styles.playButtonContainer}
onPress={this.audio}>
<FontAwesomeIcon
name="play"
icon={faPlay}
size={32}
color="#3D425C"
/>
</TouchableOpacity>
<TouchableOpacity onPress={this.pause}>
<FontAwesomeIcon icon={faPause} size={32} color="#93A8B3" />
</TouchableOpacity>
</View>
</SafeAreaView>
);
}
}