Description
Describe the Bug
I am experiencing an issue where I can successfully play a remote MP3 file using react-native-track-player, but local audio files saved in DocumentDirectoryPath are not playing.
I get the following error when trying to play a local file:
error: {message: 'The track could not be played', code: 'ios_track_unplayable'}
However, a remote URL works fine:
await TrackPlayer.add({
id: 'test-url',
url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3',
title: 'Test Audio',
artist: 'Unknown',
});
await TrackPlayer.play();
Steps To Reproduce
1-Copy an MP3 file from local storage to the app's document directory:
const simplePath = ${DocumentDirectoryPath}/audio.mp3
;
await copyFile(tracker, simplePath);
=> the track is a document picked by the user inside the application
2-Try to play the copied file:
await TrackPlayer.add({
id: track_${Date.now()}
,
url: file://${simplePath}
,
title: 'Local Track',
artist: 'Unknown',
});
await TrackPlayer.play();
3-It throws the error: ios_track_unplayable.
Code To Reproduce
const setupPlayer = async () => {
try {
const simplePath = ${DocumentDirectoryPath}/audio.mp3
;
// Check if the file exists before copying
const fileExists = await stat(tracker).catch(() => null);
if (!fileExists) {
console.log('Original file does not exist:', tracker);
return;
}
// Delete existing file if present
const newFileExists = await stat(simplePath).catch(() => null);
if (newFileExists) {
await unlink(simplePath);
}
// Copy file and verify
await copyFile(tracker, simplePath);
const copiedFileExists = await stat(simplePath).catch(() => null);
if (!copiedFileExists) {
console.log('Copied file does not exist:', simplePath);
return;
}
console.log('File copied successfully:', simplePath);
// Add track to player
await TrackPlayer.add({
id: `track_${Date.now()}`,
url: `file://${simplePath}`,
// url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3',
title: `track_${Date.now()}`,
artist: `track_${Date.now()}`,
});
console.log('Track added successfully');
} catch (error) {
console.log('Error in setupPlayer:', error);
}
};
Environment Info:
System:
OS: macOS 13.5.2
CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
Memory: 91.71 MB / 16.00 GB
Shell:
version: "5.9"
path: /bin/zsh
Binaries:
Node:
version: 20.11.0
path: /usr/local/bin/node
Yarn:
version: 1.22.10
path: /usr/local/bin/yarn
npm:
version: 10.9.2
path: ~/Desktop/projects/audittab/node_modules/.bin/npm
Watchman:
version: 2023.01.16.00
path: /usr/local/bin/watchman
Managers:
CocoaPods:
version: 1.13.0
path: /Users/macbookpro/.rvm/gems/ruby-2.7.1/bin/pod
SDKs:
iOS SDK:
Platforms:
- DriverKit 23.2
- iOS 17.2
- macOS 14.2
- tvOS 17.2
- watchOS 10.2
react:
installed: 18.2.0
wanted: 18.2.0
react-native:
installed: 0.72.6
wanted: 0.72.6
iOS:
hermesEnabled: false
newArchEnabled: false
"react-native-track-player": "^4.1.1",
Real device
IOS 17.6.1
Activity