@@ -4,7 +4,6 @@ import 'dart:io';
44import 'dart:math' ;
55
66import 'package:crypto/crypto.dart' ;
7- import 'package:flustars/flustars.dart' ;
87import 'package:just_audio/just_audio.dart' ;
98import 'package:path/path.dart' as p;
109import 'package:path_provider/path_provider.dart' ;
@@ -134,9 +133,6 @@ class AlistLockCachingAudioSource extends StreamAudioSource {
134133 final sourceLength =
135134 response.contentLength == - 1 ? null : response.contentLength;
136135 var mimeType = response.headers.contentType.toString ();
137- if (! mimeType.startsWith ("audio/" )){
138- mimeType = "audio/mpeg" ;
139- }
140136
141137 final acceptRanges = response.headers.value (HttpHeaders .acceptRangesHeader);
142138 final originSupportsRangeRequests =
@@ -229,7 +225,7 @@ class AlistLockCachingAudioSource extends StreamAudioSource {
229225 contentLength:
230226 effectiveEnd != null ? effectiveEnd - effectiveStart : null ,
231227 offset: start,
232- contentType: mimeType,
228+ contentType: _resetMimeType ( mimeType) ,
233229 stream: responseStream.asBroadcastStream (),
234230 ));
235231 }
@@ -256,7 +252,7 @@ class AlistLockCachingAudioSource extends StreamAudioSource {
256252 sourceLength: sourceLength,
257253 contentLength: end != null ? end - start : null ,
258254 offset: start,
259- contentType: mimeType,
255+ contentType: _resetMimeType ( mimeType) ,
260256 stream: response.asBroadcastStream (),
261257 ));
262258 }, onError: (dynamic e, StackTrace ? stackTrace) {
@@ -296,23 +292,41 @@ class AlistLockCachingAudioSource extends StreamAudioSource {
296292 return response;
297293 }
298294
295+ /// 在 iOS 上部分音频格式直接传入无法播放,但是实际上却可以播放,所以修改一下 mimeType
296+ String _resetMimeType (String mimeType) {
297+ if (! Platform .isIOS) return mimeType;
298+
299+ var supportedMimeTypeForIOS = [
300+ 'audio/aac' ,
301+ 'audio/mpeg' ,
302+ 'audio/ogg' ,
303+ 'audio/opus' ,
304+ 'audio/wav' ,
305+ 'audio/webm' ,
306+ 'audio/mp4' ,
307+ 'audio/x-aiff' ,
308+ 'audio/x-mpegurl'
309+ ];
310+ if (supportedMimeTypeForIOS.contains (mimeType)) {
311+ return mimeType;
312+ } else {
313+ return "audio/mpeg" ;
314+ }
315+ }
316+
299317 @override
300318 Future <StreamAudioResponse > request ([int ? start, int ? end]) async {
301319 final cacheFile = await this .cacheFile;
302320 if (cacheFile.existsSync ()) {
303321 final sourceLength = cacheFile.lengthSync ();
304322 var mimeType = await _readCachedMimeType ();
305- if (! mimeType.startsWith ("audio/" )){
306- mimeType = "audio/mpeg" ;
307- }
308- LogUtil .d ("mimeType=${mimeType }" );
309323
310324 return StreamAudioResponse (
311325 rangeRequestsSupported: true ,
312326 sourceLength: start != null ? sourceLength : null ,
313327 contentLength: (end ?? sourceLength) - (start ?? 0 ),
314328 offset: start,
315- contentType: mimeType,
329+ contentType: _resetMimeType ( mimeType) ,
316330 stream: cacheFile.openRead (start, end).asBroadcastStream (),
317331 );
318332 }
0 commit comments