ios - 使用 ionic 媒体插件录制语音笔记在 IOS 上不起作用
<p><p>我正在尝试使用 ionic cordova <strong>Media</strong> 和 <strong>File</strong> 插件在 ios 设备上录制语音笔记并将其推送到 firebase 存储。</p>
<blockquote>
<p>On android is working well.</p>
</blockquote>
<p>这是我的<strong>代码</strong>:</p>
<p><em>首先我创建了 <code>init()</code> 函数</em></p>
<pre><code>init(): Promise < any > {
this.date = moment().format('x');
return new Promise((resolve, reject) => {
let currentFile: File;
this.fileName = this.date + `-rnb.mp3`;
this.file.createFile(this.platform.is('ios') ? cordova.file.tempDirectory : cordova.file.dataDirectory, this.fileName, true).then((result) => {
this.current_file_playing = this.createAudioFile(this.storageDirectory, this.fileName);
resolve();
}, (e) => {
console.log(JSON.stringify(e, null, 2));
reject(e);
})
});
}
</code></pre>
<p><em><code>this.storageDirectory</code> 它是在提供者<code>constructor()</code> 中定义的变量,等于目录路径取决于平台。这是以下代码:</em></p>
<pre><code>this.platform.ready().then(() => {
if (this.platform.is('ios')) {
this.storageDirectory = this.file.tempDirectory;
} else if (this.platform.is('android')) {
this.storageDirectory = this.file.externalDataDirectory;
}
});
</code></pre>
<p><em>那么<code>record()</code>函数就是监听按钮录制按钮</em></p>
<pre><code>record(){
return new Promise((resolve,reject)=>{
this.init().then((media:Media) => {
try {
this.startRecording(media);
resolve(media);
} catch (e) {
console.log(e);
}
});
});
}
</code></pre>
<p><em>这是 <code>startRecording()</code> 函数:</em></p>
<pre><code>startRecording(mediaPlugin: any) {
this.current_file_playing.startRecord();
}
</code></pre>
<p><em>另外<code>stopRecording()</code>函数是一个停止按钮的监听器:</em></p>
<pre><code>stopRecording(mediaPlugin: any) {
return new Promise((resolve,reject)=>{
this.current_file_playing.stopRecord();
this.current_file_playing.play();
this.current_file_playing.setVolume(0.0); //trick
this.saveFirebase().then((downloadUrl) => {
resolve(downloadUrl);
});
});
}
</code></pre>
<p><em>最后,这就是我使用 <code>saveFirebase()</code> 函数</em></p> 插入 Firebase 的方式
<pre><code>saveFirebase() {
return new Promise((resolve, reject) => {
let storageRef = firebase.storage().ref();
let metadata = {
contentType: 'audio/mp3',
};
this.file.readAsDataURL(this.storageDirectory, this.fileName).then((file) => {
let voiceRef = storageRef.child(`voices/${this.fileName}`).putString(file, firebase.storage.StringFormat.DATA_URL);
voiceRef.on(firebase.storage.TaskEvent.STATE_CHANGED, (snapshot) => {
console.log("uploading");
}, (e) => {
console.log('inside the error');
reject(e);
console.log(JSON.stringify(e, null, 2),'this.error');
}, () => {
var downloadURL = voiceRef.snapshot.downloadURL;
resolve(downloadURL);
});
});
});
}
</code></pre>
<p><em><code>saveFirebase()</code>函数说明</em></p>
<p>首先我使用 <code>this.file.readAsDataURL(...)</code> 将文件转换为 <strong>base64</strong> 然后我使用 <code>putString</code> 推送 Firebase 存储方法。</p>
<blockquote>
<p>The audio file is successfully pushed to Firebase Storage, But with 0 Byte size. That is mean to pushing to Firebase is working well, but the recording voice to the file is not working.</p>
</blockquote>
<p> <a href="/image/UKvhF.jpg" rel="noreferrer noopener nofollow"><img src="/image/UKvhF.jpg" alt="This is my Firebase Storage"/></a> </p>
<blockquote>
<p>The audio files that have size is recorded fromandroid device.</p>
</blockquote>
<p>有人知道我的问题是什么吗?</p>
<p>谢谢。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>问题是:</p>
<p>音频文件应为 <strong>.wav</strong>。因此,当我将音频类型更改为 wav 时。</p>
<pre><code>let metadata = {
contentType: 'audio/wav',
};
this.fileName = this.date + `-rnb.mp3`;
</code></pre>
<p>它对我有用。</p>
<p>谢谢。</p></p>
<p style="font-size: 20px;">关于ios - 使用 ionic 媒体插件录制语音笔记在 IOS 上不起作用,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/48824291/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/48824291/
</a>
</p>
页:
[1]