React Native WARN没有为键TrackPlayer注册任务

siv3szwd  于 2023-02-05  发布在  React
关注(0)|答案(4)|浏览(458)

我正在使用react-native-track-player (2.1.3)在一个react-native应用程序中实现一个音频播放器,当我在项目中初始化音轨播放器时,我收到了以下警告消息。
WARN没有为键TrackPlayer注册任务

我遵循了在类似的closed GitHub issue中找到的解决方案之一,但它是很久以前打开的。无论如何,它也不起作用。

    • 以下是GitHub中的解决方案:**(将以下代码添加到index.js(应用的入口文件))
import TrackPlayer from 'react-native-track-player';

TrackPlayer.setupPlayer({}).then(async () => {
})

如果有人使用react-native-track-player解决了这个问题,你能提供一个答案吗?

ipakzgxi

ipakzgxi1#

嗨,我也在我的项目中使用跟踪播放器。我的解决方案是在index.js文件中添加服务,你可以试试这个

import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import TrackPlayer from 'react-native-track-player';

AppRegistry.registerComponent(appName, () => App);
TrackPlayer.registerPlaybackService(() => require('./service.js'));

我已经在根文件夹中创建了service.js文件

import TrackPlayer from 'react-native-track-player';

module.exports = async function () {

  try {
    TrackPlayer.addEventListener('remote-play', () => {
      TrackPlayer.play()
    })

    TrackPlayer.addEventListener('remote-pause', () => {
      TrackPlayer.pause()
    });

    TrackPlayer.addEventListener('remote-next', () => {
      TrackPlayer.skipToNext()
    });

    TrackPlayer.addEventListener('remote-previous', () => {
      TrackPlayer.skipToPrevious()
    });

    TrackPlayer.addEventListener('remote-stop', () => {
      TrackPlayer.destroy()
    });
  } catch (error) { }

};
wa7juj8i

wa7juj8i2#

1.我是随着这个教程从Youtube Mobile App - Make Your Own Music Player
1.这个链接来自react原生文档:React Native Track Player
1.然后,我不再将文件导入index.js,而是在index.js文件中创建一个函数。Playback Service docs page
我受够了。希望能有所帮助!

import App from './App';
import {name as appName} from './app.json';

import TrackPlayer from 'react-native-track-player';

export const onRegisterPlayback = async() => {
    
     TrackPlayer.addEventListener('remote-play', () => TrackPlayer.play());

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

    TrackPlayer.addEventListener('remote-stop', () => TrackPlayer.destroy())
}

AppRegistry.registerComponent(appName, () => App);
TrackPlayer.registerPlaybackService(() => onRegisterPlayback)
x759pob2

x759pob23#

如根文件夹中的Image.AddService.js文件所示

复制并粘贴下面的代码。

import TrackPlayer from 'react-native-track-player';

module.exports = async function () {

try {
TrackPlayer.addEventListener('remote-play', () => {
  TrackPlayer.play()
})

TrackPlayer.addEventListener('remote-pause', () => {
  TrackPlayer.pause()
});

TrackPlayer.addEventListener('remote-next', () => {
  TrackPlayer.skipToNext()
});

TrackPlayer.addEventListener('remote-previous', () => 
{
  TrackPlayer.skipToPrevious()
});

TrackPlayer.addEventListener('remote-stop', () => {
  TrackPlayer.destroy()
});
 } catch (error) { }

};

aor9mmx1

aor9mmx14#

在我的例子中,错误在于我将 Bootstrap 放在了**/src/index.ts**中

(错误)

AppRegistry.registerRunnable(appName, async initialProps => {...})
TrackPlayer.registerPlaybackService(() => tpService);

但此 Bootstrap 需要位于**/index.js**中

(右)

AppRegistry.registerComponent(appName, () => App);
TrackPlayer.registerPlaybackService(() => tpService);

相关问题