This project now includes a complete setup for react-native-track-player to handle audio playback functionality with user-controlled audio settings.
- Handles remote control events (play, pause, stop, next, previous)
- Manages background playback controls
- Handles seek operations
TrackPlayerSetup()- Initializes the player with default configurationaddTrack(track)- Adds a track to the queueplayTrack()- Starts playbackpauseTrack()- Pauses playbackstopTrack()- Stops playbackresetPlayer()- Clears the queue
Provides an easy-to-use interface for components with audio setting support:
const {
isPlaying,
playbackState,
progress,
play,
pause,
stop,
reset,
addAndPlayTrack,
seekTo,
isAudioEnabled,
} = useTrackPlayer();- Redux State:
isAudio- Controls whether audio functionality is enabled - Settings Component:
src/Settings/components/audio.jsx- Toggle switch in settings - User Control: Users can enable/disable audio functionality from the app settings
A complete audio player component with:
- Play/pause controls
- Progress slider
- Time display
- Stop functionality
- Audio setting awareness (disabled state when audio is off)
Added to android/app/src/main/AndroidManifest.xml:
WAKE_LOCK- Keeps device awake during playbackFOREGROUND_SERVICE- Allows background playbackFOREGROUND_SERVICE_MEDIA_PLAYBACK- Specific media playback permission
The TrackPlayer service is automatically registered and configured for iOS background audio.
import useTrackPlayer from "../hooks/useTrackPlayer";
const MyComponent = () => {
const { isPlaying, play, pause, addAndPlayTrack, isAudioEnabled } = useTrackPlayer();
const playAudio = async () => {
if (!isAudioEnabled) {
alert("Audio is disabled. Please enable it in Settings.");
return;
}
const track = {
id: "1",
url: "https://example.com/audio.mp3",
title: "Song Title",
artist: "Artist Name",
};
await addAndPlayTrack(track);
};
return (
<Button
title={isPlaying ? "Pause" : "Play"}
onPress={isPlaying ? pause : playAudio}
disabled={!isAudioEnabled}
/>
);
};import AudioPlayer from "../components/AudioPlayer";
const MyScreen = () => {
return (
<AudioPlayer
audioUrl="https://example.com/audio.mp3"
title="Track Title"
artist="Artist Name"
/>
);
};import { useSelector } from "react-redux";
const MyAudioComponent = () => {
const isAudioEnabled = useSelector((state) => state.isAudio);
if (!isAudioEnabled) {
return (
<View>
<Text>Audio is disabled. Enable it in Settings to use audio features.</Text>
</View>
);
}
return (
// Your audio component here
);
};- User Control: Users can toggle audio on/off in the app settings
- Global State: The setting is stored in Redux state (
isAudio) - Hook Integration:
useTrackPlayerhook respects the audio setting - Component Awareness: Audio components show disabled state when audio is off
- Graceful Degradation: Audio functions return early when audio is disabled
- User Choice: Users can disable audio if they prefer text-only experience
- Performance: Prevents unnecessary audio processing when disabled
- Accessibility: Clear visual feedback when audio features are unavailable
- Battery Saving: Audio processing is skipped when disabled
const track = {
id: "unique-id", // Required: Unique identifier
url: "audio-url", // Required: Audio file URL
title: "Track Title", // Optional: Display title
artist: "Artist Name", // Optional: Artist name
album: "Album Name", // Optional: Album name
artwork: "image-url", // Optional: Album artwork URL
duration: 180, // Optional: Duration in seconds
};- Background audio playback
- Lock screen controls
- Notification controls on Android
- Control Center integration on iOS
- Seek functionality
- Progress tracking
- Multiple track support
- User-controlled audio settings
- Graceful degradation when audio is disabled
The package is already installed and configured. The TrackPlayer service is automatically initialized when the app starts, and the audio setting is available in the app settings.
- Use the
useTrackPlayerhook in your components - Check
isAudioEnabledbefore audio operations - Create track objects with your audio URLs
- Customize the
AudioPlayercomponent as needed - Add additional controls or features as required
For more advanced usage, refer to the react-native-track-player documentation.