react-native-track-player: 我如何修复 TypeError: null is not an object (evaluating 'TrackPlayer.STATE_NONE') in android?

时间:2021-07-22 07:32:47

标签: android react-native react-native-track-player

<块引用>

我正在尝试实现 react - native - track - player,但我遇到了上述错误 请指导我如何解决此错误。我已经重新安装了该应用程序 两次,但在我的 案例错误不短。我在下面分享一段代码 供参考。

  1. 我制作了一个这样的 service.js 文件
    import TrackPlayer from 'react-native-track-player';
    module.exports = async function () {
    TrackPlayer.addEventListener("remote-play", () => TrackPlayer.play())
    TrackPlayer.addEventListener("remote-pause", () => TrackPlayer.pause())}
<块引用>
  1. 像这样在 index.js 中导入它
    import {AppRegistry}from 'react-native';
    import TrackPlayer from "react-native-track-player"
    import {App}from './App  
    AppRegistry.registerComponent(appName, () => App);
    TrackPlayer.registerPlaybackService(() => require("./service"))
<块引用>
  1. 最后这是我的设置文件代码,请仔细阅读。
     componentDidMount() {
            TrackPlayer.updateOptions({
                    stopWithApp: true
                    , capabilities: [TrackPlayer.CAPABILITY_PLAY, TrackPlayer
                            .CAPABILITY_PAUSE
                    ]
                    , compactCapabilities: [
                            TrackPlayer.CAPABILITY_PLAY
                            , TrackPlayer.CAPABILITY_PAUSE,
                            // TrackPlayer.CAPABILITY_SEEK_TO
                    ]
            , });
            this.setUpTrackPlayer()
    }
    componentWillUnmount() {
    TrackPlayer.destroy();
     }
    setUpTrackPlayer = async () => {
    try {
            await TrackPlayer.setupPlayer();
            await TrackPlayer.add({
                    id: '1'
                    , url: 'n/w url'
                    , title: 'Example'
                    , artist: 'ajay'
            , });
            console.log('Tracks added');
    }
      and  for play and pause i am using 
     TrackPlayer.play() & TrackPlayer.pause()

0 个答案:

没有答案