Database React native:如何在应用程序中的特定位置保存音频录制文件
我创建了一个项目来录制录音。现在我想将文件保存到一个文件夹中。之后,我必须将文件名存储在数据库中。知道怎么做吗Database React native:如何在应用程序中的特定位置保存音频录制文件,database,react-native,store,audio-recording,Database,React Native,Store,Audio Recording,我创建了一个项目来录制录音。现在我想将文件保存到一个文件夹中。之后,我必须将文件名存储在数据库中。知道怎么做吗 componentDidMount() { AudioRecorder.requestAuthorization().then((isAuthorised) => { this.setState({ hasPermission: isAuthorised }); if (!isAuthorised) return; this.prepareReco
componentDidMount() {
AudioRecorder.requestAuthorization().then((isAuthorised) => {
this.setState({ hasPermission: isAuthorised });
if (!isAuthorised) return;
this.prepareRecordingPath(this.state.audioPath);
AudioRecorder.onProgress = (data) => {
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);
}
};
});
}
_renderButton(title, onPress, active) {
var style = (active) ? styles.activeButtonText : styles.buttonText;
return (
<TouchableHighlight style={styles.button} onPress={onPress}>
<Text style={style}>
{title}
</Text>
</TouchableHighlight>
);
}
_renderPauseButton(onPress, active) {
var style = (active) ? styles.activeButtonText : styles.buttonText;
var title = this.state.paused ? "RESUME" : "PAUSE";
return (
<TouchableHighlight style={styles.button} onPress={onPress}>
<Text style={style}>
{title}
</Text>
</TouchableHighlight>
);
}
async _pause() {
if (!this.state.recording) {
console.warn('Can\'t pause, not recording!');
return;
}
try {
const filePath = await AudioRecorder.pauseRecording();
this.setState({paused: true});
} catch (error) {
console.error(error);
}
}
async _resume() {
if (!this.state.paused) {
console.warn('Can\'t resume, not paused!');
return;
}
try {
await AudioRecorder.resumeRecording();
this.setState({paused: false});
} catch (error) {
console.error(error);
}
}
async _stop() {
if (!this.state.recording) {
console.warn('Can\'t stop, not recording!');
return;
}
this.setState({stoppedRecording: true, recording: false, paused: false});
try {
const filePath = await AudioRecorder.stopRecording();
if (Platform.OS === 'android') {
this._finishRecording(true, filePath);
}
return filePath;
} catch (error) {
console.error(error);
}
}
async _play() {
if (this.state.recording) {
await this._stop();
}
// 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);
}
async _record() {
if (this.state.recording) {
console.warn('Already recording!');
return;
}
if (!this.state.hasPermission) {
console.warn('Can\'t record, no permission granted!');
return;
}
if(this.state.stoppedRecording){
this.prepareRecordingPath(this.state.audioPath);
}
this.setState({recording: true, paused: false});
try {
const filePath = await AudioRecorder.startRecording();
} catch (error) {
console.error(error);
}
}
_finishRecording(didSucceed, filePath, fileSize) {
this.setState({ finished: didSucceed });
console.warn(`Finished recording of duration ${this.state.currentTime} seconds at path: ${filePath} and size of ${fileSize || 0} bytes`);
}
componentDidMount(){
AudioRecorder.requestAuthorization().then((已授权)=>{
this.setState({hasPermission:isAuthorized});
如果(!isauthorized)返回;
this.prepareRecordingPath(this.state.audioPath);
AudioRecorder.onProgress=(数据)=>{
this.setState({currentTime:Math.floor(data.currentTime)});
};
AudioRecorder.onFinished=(数据)=>{
//Android回调以承诺的形式出现。
如果(Platform.OS==='ios'){
这._完成记录(data.status==“OK”,data.audioFileURL,data.audioFileSize);
}
};
});
}
_renderButton(标题,按,激活){
var style=(活动)?styles.activebuttonext:styles.buttonext;
返回(
{title}
);
}
_renderPauseButton(按,激活){
var style=(活动)?styles.activebuttonext:styles.buttonext;
var title=this.state.paused?“恢复”:“暂停”;
返回(
{title}
);
}
异步暂停(){
如果(!this.state.recording){
console.warn('不能暂停,不能录制!');
返回;
}
试一试{
const filePath=wait AudioRecorder.pauseRecording();
this.setState({暂停:true});
}捕获(错误){
控制台错误(error);
}
}
异步恢复(){
如果(!this.state.paused){
console.warn('无法恢复,未暂停!');
返回;
}
试一试{
等待录音机。恢复录音();
this.setState({暂停:false});
}捕获(错误){
控制台错误(error);
}
}
异步停止(){
如果(!this.state.recording){
console.warn('不能停止,不能录制!');
返回;
}
this.setState({stoppedRecording:true,recording:false,paused:false});
试一试{
const filePath=wait AudioRecorder.stopRecording();
如果(Platform.OS==='android'){
此.\u finishRecording(true,filePath);
}
返回文件路径;
}捕获(错误){
控制台错误(error);
}
}
异步播放(){
if(此.state.recording){
等着吧;
}
//对于react native sound的一些问题,这些超时是一个很难解决的问题。
//看https://github.com/zmxv/react-native-sound/issues/89.
设置超时(()=>{
var声音=新声音(this.state.audioPath“”,(错误)=>{
如果(错误){
console.log('加载声音失败',错误);
}
});
设置超时(()=>{
声音。播放((成功)=>{
如果(成功){
console.log('已成功完成播放');
}否则{
log('由于音频解码错误导致播放失败');
}
});
}, 100);
}, 100);
}
异步记录(){
if(此.state.recording){
console.warn('已录制!');
返回;
}
如果(!this.state.hasPermission){
console.warn('无法录制,未授予权限!');
返回;
}
if(此.state.stoppedRecording){
this.prepareRecordingPath(this.state.audioPath);
}
this.setState({记录:true,暂停:false});
试一试{
const filePath=await AudioRecorder.startRecording();
}捕获(错误){
控制台错误(error);
}
}
_finishRecording(成功、文件路径、文件大小){
this.setState({finished:didcessfuel});
console.warn(`在路径${filePath}和大小${fileSize| | 0}字节处完成持续时间${this.state.currentTime}秒的录制');
}
如果任何人有关于这个概念的分享任何示例博客链接,我已经搜索了很多博客,但没有得到回复,因为我是新手。请任何人给我回复,我对此一无所知。你需要更具体一些,例如:你有后端吗?正在使用哪个数据库?问题到底出在哪里(例如,传输文件、实现文件系统等)?@Mukeyii我使用的是sqlite3数据库,问题是我录制了一个音频,录制后的文件需要保存在应用程序的一个文件夹中。然后我需要保存在数据库中。请任何人给我回复,我对此一无所知。你需要更具体一些,例如:你有后端吗?正在使用哪个数据库?问题到底出在哪里(例如,传输文件、实现文件系统等)?@Mukeyii我使用的是sqlite3数据库,问题是我录制了一个音频,录制后该文件需要保存在应用程序的一个文件夹中。然后我需要从那里保存到数据库中。