This document outlines the changes made to the NowPlayingTest project to migrate from expo-av to react-native-track-player, and the implications for migrating the BalearicFmRadio app.
-
Added Dependencies
- Added
react-native-track-playerpackage
- Added
-
New Files Created
utils/trackPlayerService.js: Service for handling playback eventsutils/trackPlayerUtils.js: Utility functions for track player operationscontext/TrackPlayerContext.tsx: React context for track player state
-
Modified Files
app/_layout.tsx: Updated to use TrackPlayerProvider and register the playback serviceapp/(tabs)/index.tsx: Updated to use the TrackPlayer context
expo-av:
// No explicit initialization required
const sound = new Audio.Sound();react-native-track-player:
// Setup required before use
await TrackPlayer.setupPlayer();
await TrackPlayer.updateOptions({
capabilities: [Capability.Play, Capability.Pause, Capability.Stop],
// ...other options
});expo-av:
await sound.loadAsync({ uri: STREAM_URL });
await sound.playAsync();react-native-track-player:
await TrackPlayer.reset();
await TrackPlayer.add({
id: 'stream',
url: STREAM_URL,
title: 'Title',
artist: 'Artist',
// ...other metadata
});
await TrackPlayer.play();expo-av:
await sound.stopAsync();
await sound.unloadAsync();react-native-track-player:
await TrackPlayer.stop();
await TrackPlayer.reset(); // Optional: clear the queueexpo-av:
sound.setOnPlaybackStatusUpdate((status) => {
// Handle status updates
});react-native-track-player:
// In a separate service file
TrackPlayer.addEventListener(Event.PlaybackState, (state) => {
// Handle state changes
});
TrackPlayer.addEventListener(Event.PlaybackMetadataReceived, (metadata) => {
// Handle metadata updates
});expo-av:
// Limited support for Now Playing metadata
await sound.setStatusAsync({
progressUpdateIntervalMillis: 1000,
shouldPlay: true,
// ...other status options
});react-native-track-player:
// Full support for Now Playing metadata
await TrackPlayer.updateNowPlayingMetadata({
title: 'Track Title',
artist: 'Artist Name',
artwork: 'https://example.com/artwork.jpg',
// ...other metadata
});-
AudioCore Class
- The
AudioCoreclass would be replaced with react-native-track-player's API - The
configureAudioSessionmethod would be replaced with TrackPlayer's setup and options
- The
-
PlaybackDispatcher
- The
PlaybackDispatcherwould need significant refactoring to use track-player's event system - The state management would be simplified as track-player handles more internally
- The
-
Queue Management
- The custom queue system would be replaced with track-player's built-in queue
-
Install Dependencies
npm install react-native-track-player
-
Create Service File
- Create a service file similar to
utils/trackPlayerService.js - Register the service in the app entry point
- Create a service file similar to
-
Create Track Player Utilities
- Create utility functions for common operations
- Map the current dispatcher actions to track player functions
-
Update PlaybackContext
- Modify the context to use track player instead of expo-av
- Update the state management to use track player events
-
Update UI Components
- Update any components that use the PlaybackContext to work with the new API
-
Full Now Playing Widget Support
- Proper integration with iOS Now Playing widget
- Better background playback handling
-
Simplified Code
- Less custom code for audio handling
- Better error handling and recovery
-
Better User Experience
- More reliable playback
- Better integration with system controls
-
API Differences
- Different approach to queue management
- Different event system
-
Custom Features
- May need to reimplement some custom features
- Ensure podcast playback position tracking works similarly
-
Error Handling
- Need to ensure robust error handling is maintained
- Test basic playback functionality
- Test background playback
- Test Now Playing widget integration
- Test error recovery
- Test with different network conditions
Migrating from expo-av to react-native-track-player will provide better Now Playing widget support and a more robust audio playback experience. The migration will require significant changes to the audio handling code, but the benefits in terms of user experience and code maintainability will be worth the effort.