Skip to content
This repository has been archived by the owner on Aug 21, 2020. It is now read-only.

Use 'Promise' to solve flow issue #348

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 51 additions & 25 deletions AudioExample/AudioExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class AudioExample extends Component {
hasPermission: undefined,
};

promiseIOSFinishRecording = null

prepareRecordingPath(audioPath){
AudioRecorder.prepareRecordingAtPath(audioPath, {
SampleRate: 22050,
Expand All @@ -47,12 +49,15 @@ class AudioExample extends Component {
this.setState({currentTime: Math.floor(data.currentTime)});
};

AudioRecorder.onFinished = (data) => {
// Android callback comes in the form of a promise instead.
if (Platform.OS === 'ios') {
this._finishRecording(data.status === "OK", data.audioFileURL, data.audioFileSize);
}
};
this.promiseIOSFinishRecording = new Promise((resolve,reject)=>{
AudioRecorder.onFinished = resolve;
});
// AudioRecorder.onFinished = (data) => {
// // Android callback comes in the form of a promise instead.
// if (Platform.OS === 'ios') {
// this._finishRecording(data.status === "OK", data.audioFileURL, data.audioFileSize);
// }
// };
});
}

Expand Down Expand Up @@ -117,10 +122,14 @@ class AudioExample extends Component {
this.setState({stoppedRecording: true, recording: false, paused: false});

try {
const filePath = await AudioRecorder.stopRecording();


//use Promise method to get both Android and IOS record data
if (Platform.OS === 'android') {
const filePath = await AudioRecorder.stopRecording();
this._finishRecording(true, filePath);
}else{
const iosData = await this.promiseIOSFinishRecording;
this._finishRecording(iosData.status === "OK", iosData.audioFileURL);
}
return filePath;
} catch (error) {
Expand All @@ -135,23 +144,40 @@ class AudioExample extends Component {

// These timeouts are a hacky workaround for some issues with react-native-sound.
// See https://github.com/zmxv/react-native-sound/issues/89.
setTimeout(() => {
var sound = new Sound(this.state.audioPath, '', (error) => {
if (error) {
console.log('failed to load the sound', error);
}
});

setTimeout(() => {
sound.play((success) => {
if (success) {
console.log('successfully finished playing');
} else {
console.log('playback failed due to audio decoding errors');
}
});
}, 100);
}, 100);
// setTimeout(() => {
// var sound = new Sound(this.state.audioPath, '', (error) => {
// if (error) {
// console.log('failed to load the sound', error);
// }
// });

// setTimeout(() => {
// sound.play((success) => {
// if (success) {
// console.log('successfully finished playing');
// } else {
// console.log('playback failed due to audio decoding errors');
// }
// });
// }, 100);
// }, 100);

//use Promise method to get both
var sound = null;
const error = await new Promise(function (resolve, reject) {
sound = new Sound(audiopath, '', resolve);
});
if (error) {
console.log('failed to load the sound', error);
return;
}
sound.play((success) => {
if (success) {
console.log('successfully finished playing');
} else {
console.log('playback failed due to audio decoding errors');
}
});
}

async _record() {
Expand Down