0
import React, {useEffect, useState} from 'react';
import TrackPlayer, {usePlaybackState} from 'react-native-track-player';
import IconButton from './IconButton';

function Player({url}) {
  const [iconName, setIconName] = useState('play-circle');
  const playbackState = usePlaybackState();

  useEffect(() => {
    setup();
  }, []);

  async function setup() {
    await TrackPlayer.setupPlayer({});
    await TrackPlayer.reset();

    await TrackPlayer.add({
      id: 'trackId',
      url: url,
      title: 'Example Track',
      artist: 'Example Artist',
    });
  }

  async function togglePlayback() {
    const currentTrack = await TrackPlayer.getActiveTrackIndex();
    if (currentTrack == null) {
      await setup();
    } else {
      console.log(playbackState, TrackPlayer.STATE_PLAYING);
      if (playbackState === TrackPlayer.STATE_PLAYING) {
        await TrackPlayer.pause();
        setIconName('play-circle');
      } else {
        await TrackPlayer.play();
        setIconName('pause-circle');
      }
    }
  }

  return (
    <IconButton
      pressEvent={togglePlayback}
      name={iconName}
      size={20}
      color="rgb(166, 166, 166)"
    />
  );
}

export default Player;

I passed a music link http://music.wufazhuce.com/FkX6nZfSWVRRGacxHWGSZ2-SpjtP to Player, The link is no problem, but here is an error,error: {message: "Source error", code: "android-io-file-not-found"}, It's very strange, I don't know why can't find this file and how to solve it, please help me.

I want to play the audio after I click the button

0 Answers0