fix: change audio encoding to aac (#5224)
* fix: change audio encoding to aac * remove comment * fix: add support to old audios on iphone * fix: use MediaPlayer implementation * chore: add androidImplementation on loadAsync
This commit is contained in:
parent
7a7193d1ab
commit
4ccf815746
|
@ -20,26 +20,31 @@ interface IMessageBoxRecordAudioProps {
|
||||||
onStart: Function;
|
onStart: Function;
|
||||||
}
|
}
|
||||||
|
|
||||||
const RECORDING_EXTENSION = '.m4a';
|
const RECORDING_EXTENSION = '.aac';
|
||||||
const RECORDING_SETTINGS = {
|
const RECORDING_SETTINGS = {
|
||||||
android: {
|
android: {
|
||||||
|
// Settings related to audio encoding.
|
||||||
extension: RECORDING_EXTENSION,
|
extension: RECORDING_EXTENSION,
|
||||||
outputFormat: Audio.RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_MPEG_4,
|
outputFormat: Audio.RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_AAC_ADTS,
|
||||||
audioEncoder: Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AAC,
|
audioEncoder: Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AAC,
|
||||||
|
// Settings related to audio quality.
|
||||||
sampleRate: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.android.sampleRate,
|
sampleRate: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.android.sampleRate,
|
||||||
numberOfChannels: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.android.numberOfChannels,
|
numberOfChannels: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.android.numberOfChannels,
|
||||||
bitRate: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.android.bitRate
|
bitRate: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.android.bitRate
|
||||||
},
|
},
|
||||||
ios: {
|
ios: {
|
||||||
|
// Settings related to audio encoding.
|
||||||
extension: RECORDING_EXTENSION,
|
extension: RECORDING_EXTENSION,
|
||||||
audioQuality: Audio.RECORDING_OPTION_IOS_AUDIO_QUALITY_MIN,
|
audioQuality: Audio.RECORDING_OPTION_IOS_AUDIO_QUALITY_MEDIUM,
|
||||||
|
outputFormat: Audio.RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4AAC,
|
||||||
|
// Settings related to audio quality.
|
||||||
sampleRate: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.ios.sampleRate,
|
sampleRate: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.ios.sampleRate,
|
||||||
numberOfChannels: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.ios.numberOfChannels,
|
numberOfChannels: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.ios.numberOfChannels,
|
||||||
bitRate: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.ios.bitRate,
|
bitRate: Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY.ios.bitRate
|
||||||
outputFormat: Audio.RECORDING_OPTION_IOS_OUTPUT_FORMAT_MPEG4AAC
|
|
||||||
},
|
},
|
||||||
web: {}
|
web: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
const RECORDING_MODE = {
|
const RECORDING_MODE = {
|
||||||
allowsRecordingIOS: true,
|
allowsRecordingIOS: true,
|
||||||
playsInSilentModeIOS: true,
|
playsInSilentModeIOS: true,
|
||||||
|
@ -159,7 +164,7 @@ export default class RecordAudio extends React.PureComponent<IMessageBoxRecordAu
|
||||||
const fileURI = this.recording.getURI();
|
const fileURI = this.recording.getURI();
|
||||||
const fileData = await getInfoAsync(fileURI as string);
|
const fileData = await getInfoAsync(fileURI as string);
|
||||||
const fileInfo = {
|
const fileInfo = {
|
||||||
name: `${Date.now()}.m4a`,
|
name: `${Date.now()}.aac`,
|
||||||
mime: 'audio/aac',
|
mime: 'audio/aac',
|
||||||
type: 'audio/aac',
|
type: 'audio/aac',
|
||||||
store: 'Uploads',
|
store: 'Uploads',
|
||||||
|
|
|
@ -151,7 +151,7 @@ class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioStat
|
||||||
urlToCache: this.getUrl()
|
urlToCache: this.getUrl()
|
||||||
});
|
});
|
||||||
if (cachedAudioResult?.exists) {
|
if (cachedAudioResult?.exists) {
|
||||||
await this.sound.loadAsync({ uri: cachedAudioResult.uri });
|
await this.sound.loadAsync({ uri: cachedAudioResult.uri }, { androidImplementation: 'MediaPlayer' });
|
||||||
this.setState({ loading: false, cached: true });
|
this.setState({ loading: false, cached: true });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -302,7 +302,7 @@ class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioStat
|
||||||
type: 'audio',
|
type: 'audio',
|
||||||
mimeType: file.audio_type
|
mimeType: file.audio_type
|
||||||
});
|
});
|
||||||
await this.sound.loadAsync({ uri: audio });
|
await this.sound.loadAsync({ uri: audio }, { androidImplementation: 'MediaPlayer' });
|
||||||
this.setState({ loading: false, cached: true });
|
this.setState({ loading: false, cached: true });
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
|
@ -341,7 +341,6 @@ class MessageAudio extends React.Component<IMessageAudioProps, IMessageAudioStat
|
||||||
|
|
||||||
onValueChange = async (value: number) => {
|
onValueChange = async (value: number) => {
|
||||||
try {
|
try {
|
||||||
this.setState({ currentTime: value });
|
|
||||||
await this.sound.setPositionAsync(value * 1000);
|
await this.sound.setPositionAsync(value * 1000);
|
||||||
} catch {
|
} catch {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
|
|
|
@ -41,7 +41,7 @@ export const getFilename = ({
|
||||||
mimeType?: string;
|
mimeType?: string;
|
||||||
}) => {
|
}) => {
|
||||||
const isTitleTyped = mime.lookup(title);
|
const isTitleTyped = mime.lookup(title);
|
||||||
const extension = getExtension(type, mimeType);
|
const extension = getExtension(type, mimeType, url);
|
||||||
if (isTitleTyped && title) {
|
if (isTitleTyped && title) {
|
||||||
if (isTitleTyped === mimeType) {
|
if (isTitleTyped === mimeType) {
|
||||||
return title;
|
return title;
|
||||||
|
@ -65,18 +65,20 @@ export const getFilename = ({
|
||||||
return `${filenameFromUrl}.${extension}`;
|
return `${filenameFromUrl}.${extension}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getExtension = (type: MediaTypes, mimeType?: string) => {
|
const getExtension = (type: MediaTypes, mimeType?: string, url?: string) => {
|
||||||
if (!mimeType) {
|
if (!mimeType) {
|
||||||
return defaultType[type];
|
return defaultType[type];
|
||||||
}
|
}
|
||||||
|
// support audio from older versions
|
||||||
|
if (url?.split('.').pop() === 'm4a') {
|
||||||
|
return 'm4a';
|
||||||
|
}
|
||||||
// The library is returning mpag instead of mp3 for audio/mpeg
|
// The library is returning mpag instead of mp3 for audio/mpeg
|
||||||
if (mimeType === 'audio/mpeg') {
|
if (mimeType === 'audio/mpeg') {
|
||||||
return 'mp3';
|
return 'mp3';
|
||||||
}
|
}
|
||||||
// Audios sent by Android devices are in the audio/aac format, which cannot be converted to mp3 by iOS.
|
|
||||||
// However, both platforms support the m4a format, so they can maintain the same behavior.
|
|
||||||
if (mimeType === 'audio/aac') {
|
if (mimeType === 'audio/aac') {
|
||||||
return 'm4a';
|
return 'aac';
|
||||||
}
|
}
|
||||||
// The return of mime.extension('video/quicktime') is .qt,
|
// The return of mime.extension('video/quicktime') is .qt,
|
||||||
// this format the iOS isn't recognize and can't save on gallery
|
// this format the iOS isn't recognize and can't save on gallery
|
||||||
|
|
Loading…
Reference in New Issue