audioplayers
audioplayers copied to clipboard
fix: Audio not working in iOS 17 for UrlSource
Description
On iOS17, playing audio data via a UrlSource is broken. This is because Apple have modified the AVPlayer and now expect the AVPlayer instance to be initialised with an AVURLAsset housing the data and making use of the newly-introduced AVURLAssetOverrideMIMETypeKey global variable (https://developer.apple.com/documentation/avfoundation/avurlassetoverridemimetypekey?changes=latest_minor&language=objc)
This PR introduces a fix that initialises the AVPlayer instance using the aforementioned method if the user is attempting to load in a data source. If the user instead uses a URL (http://internet.com/file.mp3), it will use the original approach of initialising AVPlayer with the url
property.
Original:
playerItem = AVPlayerItem(url: parsedUrl)
New:
let asset = AVURLAsset(url: parsedUrl, options: [AVURLAssetOverrideMIMETypeKey: fileType])
playerItem = AVPlayerItem(asset: asset)
Checklist
- [x] The title of my PR starts with a Conventional Commit prefix (
fix:
,feat:
,refactor:
,docs:
,chore:
,test:
,ci:
etc). - [ ] I have read the Contributor Guide and followed the process outlined for submitting PRs.
- [ ] I have updated/added tests for ALL new/updated/fixed functionality.
- [x] I have updated/added relevant documentation and added dartdoc comments with
///
, where necessary. - [x] I have updated/added relevant examples in example.
Breaking Change
- [ ] Yes, this is a breaking change.
- [x] No, this is not a breaking change.
Related Issues
Fixes #1663
This PR may be can connected with #803 and tested by enabling these tests: https://github.com/bluefireteam/audioplayers/blob/87f3cb7e47e2103d2079a3dfe6aebe80c8a76c3d/packages/audioplayers/example/integration_test/lib_test.dart#L82
Hello @andyshephard
I have tested your code but I still have the same error 😔
PlatformException(DarwinAudioError, Failed to set source. For troubleshooting, see https://github.com/bluefireteam/audioplayers/blob/main/troubleshooting.md, AVPlayerItem.Status.failed on setSourceUrl, null)
Here is my code, do you have an idea why it still occurs ?
AudioCache? cache;
Future<void> initialize() async {
cache = AudioCache(prefix: 'res/audio/');
await Future.wait(Sounds.values.map((sound) async {
final pool = await AudioPool.createFromAsset(path: sound.fileName, maxPlayers: 5, audioCache: cache);
_pools.putIfAbsent(sound.name, () => pool);
}));
}
Finally instead of create every sound pools at launch, I create it when I need it and it fix my issue 👌 Thanks for your code
Future<AudioPool?> _getSoundPool({required Sounds sound}) async {
try {
if (!isInitialized) return null;
if (!_pools.containsKey(sound.name)) {
final pool = await AudioPool.createFromAsset(
path: sound.fileName,
maxPlayers: 3,
audioCache: cache!,
);
_pools.putIfAbsent(sound.name, () => pool);
}
return _pools[sound.name];
} catch (error, trace) {
log.e('Failed to create sound pool', error, trace);
}
}
Any progress here? I have encountered the same problem on IOS 17, i need to release the app in a very short time so desperately need a fix for IOS 17.