@@ -55,23 +55,37 @@ export function mapToMediaPlayerStatus(
55
55
} ;
56
56
}
57
57
58
- // https://stackoverflow.com/a/7874175/8805150
59
- const BASE_64_REGEX =
60
- / ^ ( [ 0 - 9 a - z A - Z + / ] { 4 } ) * ( ( [ 0 - 9 a - z A - Z + / ] { 2 } = = ) | ( [ 0 - 9 a - z A - Z + / ] { 3 } = ) ) ? $ / ;
58
+ const URL_REGEX =
59
+ / [ - a - z A - Z 0 - 9 @ : % . _ \+ ~ # = ] { 1 , 256 } \. [ a - z A - Z 0 - 9 ( ) ] { 1 , 6 } \b ( [ - a - z A - Z 0 - 9 ( ) @ : % _ \+ . ~ # ? & / / = ] * ) / ;
61
60
62
61
/**
63
62
* Base64 strings are not playable on iOS and needs to be saved to a file before playing
64
63
*/
65
64
export async function normalizeBase64Source (
66
- source : AVPlaybackSource
65
+ source : AVPlaybackSource ,
66
+ type : "audio" | "video"
67
67
) : Promise < AVPlaybackSource > {
68
68
const uri : string | undefined = ( source as any ) ?. uri ;
69
69
70
- if ( Platform . OS === "ios" && uri && uri . match ( BASE_64_REGEX ) ) {
71
- const fileName = `${ FileSystem . cacheDirectory } ${ uuid ( ) } ` ;
72
- await FileSystem . writeAsStringAsync ( fileName , uri , {
73
- encoding : FileSystem . EncodingType . Base64 ,
74
- } ) ;
70
+ if ( Platform . OS === "ios" && uri && ! uri . match ( URL_REGEX ) ) {
71
+ const defaultMimeType = type === "audio" ? "wav" : "mp4" ;
72
+ const mimeType = uri . startsWith ( `data:${ type } /` )
73
+ ? uri . substring ( `data:${ type } /` . length , uri . indexOf ( ";" ) ) //Ex: extract 'mp4' from 'data:video/mp4;base64,....'
74
+ : defaultMimeType ;
75
+
76
+ const fileName = `${
77
+ FileSystem . cacheDirectory
78
+ } ${ uuid ( ) } .${ mimeType . toLowerCase ( ) } `;
79
+
80
+ await FileSystem . writeAsStringAsync (
81
+ fileName ,
82
+ uri . includes ( "base64," )
83
+ ? uri . substring ( uri . indexOf ( "base64," ) + "base64," . length ) // skip header portion of base64 string
84
+ : uri ,
85
+ {
86
+ encoding : FileSystem . EncodingType . Base64 ,
87
+ }
88
+ ) ;
75
89
return { uri : fileName } ;
76
90
}
77
91
0 commit comments