菜鸟教程小白 发表于 2022-12-11 19:45:46

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 &lt; any &gt; {
    this.date = moment().format(&#39;x&#39;);
    return new Promise((resolve, reject) =&gt; {
      let currentFile: File;
      this.fileName = this.date + `-rnb.mp3`;
      this.file.createFile(this.platform.is(&#39;ios&#39;) ? cordova.file.tempDirectory : cordova.file.dataDirectory, this.fileName, true).then((result) =&gt; {
      this.current_file_playing = this.createAudioFile(this.storageDirectory, this.fileName);
      resolve();
      }, (e) =&gt; {
      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(() =&gt; {
if (this.platform.is(&#39;ios&#39;)) {
    this.storageDirectory = this.file.tempDirectory;
} else if (this.platform.is(&#39;android&#39;)) {
    this.storageDirectory = this.file.externalDataDirectory;
}
});
</code></pre>

<p><em>那么<code>record()</code>函数就是监听按钮录制按钮</em></p>

<pre><code>record(){
    return new Promise((resolve,reject)=&gt;{
      this.init().then((media:Media) =&gt; {
      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)=&gt;{
      this.current_file_playing.stopRecord();
      this.current_file_playing.play();
      this.current_file_playing.setVolume(0.0); //trick
      this.saveFirebase().then((downloadUrl) =&gt; {
      resolve(downloadUrl);
      });
    });
}
</code></pre>

<p><em>最后,这就是我使用 <code>saveFirebase()</code> 函数</em></p> 插入 Firebase 的方式

<pre><code>saveFirebase() {
    return new Promise((resolve, reject) =&gt; {
      let storageRef = firebase.storage().ref();
      let metadata = {
      contentType: &#39;audio/mp3&#39;,
      };
      this.file.readAsDataURL(this.storageDirectory, this.fileName).then((file) =&gt; {
      let voiceRef = storageRef.child(`voices/${this.fileName}`).putString(file, firebase.storage.StringFormat.DATA_URL);
      voiceRef.on(firebase.storage.TaskEvent.STATE_CHANGED, (snapshot) =&gt; {
          console.log(&#34;uploading&#34;);
      }, (e) =&gt; {
          console.log(&#39;inside the error&#39;);
          reject(e);
          console.log(JSON.stringify(e, null, 2),&#39;this.error&#39;);
      }, () =&gt; {
          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: &#39;audio/wav&#39;,
};

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]
查看完整版本: ios - 使用 ionic 媒体插件录制语音笔记在 IOS 上不起作用