0

我在使用 react-native-track-player 将曲目更改为下一个时遇到问题。

我需要在后台播放音乐时更改曲目,并且我在带有控件的 iOS 锁定屏幕上。

根据 react-native-track-player 文档(http://react-native-track-player.js.org/documentation/)我做了一个实现。

我有一个 service.js 文件:

import TrackPlayer, {Event} from 'react-native-track-player';
import {tracks} from './src/components/audioPlayer/tracks';

module.exports = async function () {
  TrackPlayer.addEventListener('remote-play' as Event, () =>
    TrackPlayer.play(),
  );

  TrackPlayer.addEventListener('remote-pause' as Event, () =>
    TrackPlayer.pause(),
  );

  TrackPlayer.addEventListener('remote-stop' as Event, () =>
    TrackPlayer.destroy(),
  );

  TrackPlayer.addEventListener(Event.RemotePrevious, async (e: any) => {
    const currentTrackIndex = await TrackPlayer.getCurrentTrack();
    if (currentTrackIndex > 0) {
      try {
        await TrackPlayer.skipToPrevious();
        console.log('remote previous succeeded');
      } catch {
        console.log('remote previous error');
        console.log(e);
      }
    }
  });

  TrackPlayer.addEventListener(Event.RemoteNext, async (e: any) => {
    const currentTrackIndex = await TrackPlayer.getCurrentTrack();
    if (currentTrackIndex < tracks.length - 1) {
      try {
        await TrackPlayer.skipToNext();
        console.log('remote next succeeded');
      } catch {
        console.log('remote next error');
        console.log(e);
      }
    }
  });

  TrackPlayer.addEventListener('remote-seek' as Event, async (position) =>
    TrackPlayer.seekTo(position.position),
  );

我在组件文件中进行了功能配置。

曲目播放器在应用程序中运行良好,我可以暂停、播放、skipToNext、skipToPrevious。

我的问题是:当我在后台播放的锁定屏幕上播放时,播放器也能正常工作,我播放、暂停,但是当我在这里执行 skipToNext 或 skipToPrevious 时,它进入暂停状态(我无法从锁定屏幕开始- 但如果我打开设备,我会看到轨道已更改)。

这是我在 xCode 中的日志:

2021-10-06 12:10:24.148543+0200 [52842:2962646] [javascript] 'New event: ', { state: 'playing', type: 'playback-state' }
2021-10-06 12:10:24.148677+0200 [52842:2962646] [javascript] state play
2021-10-06 12:10:27.719001+0200 [52842:2962646] [javascript] 'New event: ', { type: 'remote-next' }
2021-10-06 12:10:27.719153+0200 [52842:2962646] [javascript] Event remote next state
Skipping to next track
2021-10-06 12:10:27.930207+0200 [52842:2962646] [javascript] remote next succeeded
2021-10-06 12:10:27.950990+0200 [52842:2962646] [javascript] 'New event: ', { track: 1,
  nextTrack: 2,
  position: 3.49197814,
  type: 'playback-track-changed' }
2021-10-06 12:10:27.951244+0200 [52842:2962646] [javascript] Event track changed
2021-10-06 12:10:28.019813+0200 [52842:2962646] [javascript] CURRENT TRACK useeffect:
2021-10-06 12:10:28.030528+0200 [52842:2962646] [javascript] { id: 2,
  url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3',
  title: 'Kalkbass',
  artist: 'Bar Horse',
  album: '',
  duration: 344 }
2021-10-06 12:10:28.428465+0200 [52842:2962646] [javascript] 'New event: ', { state: 'paused', type: 'playback-state' }
2021-10-06 12:10:28.428566+0200 [52842:2962646] [javascript] state paused
Starting/Resuming playback

所以问题是:会有什么问题?您是否有从锁定屏幕 iOS 更改后台轨道的正确实现?任何帮助,将不胜感激。

版本:“react”:“17.0.2”,“react-native”:“0.65.1”,“react-native-track-player”:“^2.1.1”,xCode:13.0 iOS(真实设备测试) : 15.0

4

0 回答 0