0

文件:DataList.json

[
  {
    "id": "abcd-001",
    "title": "Push Ups",
    "image": "imageOne",
    "description": "There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. ",
    "duration": 10,
    "sets": "",
    "url" : "../assets/track1.mp3"
    "break": 40
  },
  {
    "id": "abcd-002",
    "title": "Pull Ups",
    "image": "imageTwo",
    "description": "There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. ",
    "duration": 30,
    "sets": "",
    "url" : "../assets/track2.mp3"
    "break": 40
  },
]

组件Track.js

useEffect(() => {
    TrackPlayer.setupPlayer();
    TrackPlayer.updateOptions({
      stopWithApp: true,
      capabilities: [
        TrackPlayer.CAPABILITY_PLAY,
        TrackPlayer.CAPABILITY_PAUSE,
        TrackPlayer.CAPABILITY_STOP,
        TrackPlayer.CAPABILITY_SKIP_TO_NEXT,
      ],
      compactCapabilities: [
        TrackPlayer.CAPABILITY_PLAY,
        TrackPlayer.CAPABILITY_PAUSE,
      ],
    });
  }, []);

  const togglePlayBack = async () => {
    await TrackPlayer.add({
      title: title,
      id: id,
      url: require('../assets/audio/track1.mp3'),
      artist: 'Imran Sayed',
      duration: duration,
    });
  };

     <TouchableOpacity style={styles.main}>
        <View style={styles.imgContainer}>
          <Image source={Images[`${image}`]} style={styles.image} />
        </View>
        <View style={styles.info}>
          <Text style={styles.text}> {title} </Text>
          <Text style={styles.description}> {description} </Text>
          <Text style={styles.duration}> Duration - {duration} </Text>
          <Button
            color="darkslateblue"
            title="Start"
            onPress={togglePlayBack}
          />
        </View>
      </TouchableOpacity>

当音乐持续时间等于曲目的 DataList.json 持续时间时,我找不到任何暂停音乐的方法。

音乐应暂停,并应在指定的持续时间后恢复。任何帮助,将不胜感激!

4

1 回答 1

0

这是解决方案

const progress = TrackPlayer.useTrackPlayerProgress();

const getSectionDone = () => {
  if (progress.position > 5) {
    // to ensure that music is playing
    if (progress.position === progress.duration) {
      // finished
    }
  }
};
于 2020-04-25T20:40:03.093 回答