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

ios - 移动后iOS上的cordova-plugin-media-with-compression似乎无法播放音频文件


                                            <p><p>我在 Ionic 2 应用程序中使用 cordova-plugin-media-with-compression。</p>

<p>在 iOS 上,如果我传递 <code>startRecord()</code> 文件名并再次调用它而不更改 <code>this.media</code>,我可以录制和播放。</p>

<p>我似乎无法播放存储在文件系统中其他位置的音频文件 - 因为我必须将一个新的 src 传递给 <code>startRecord()</code> 而这是我认为我做错了的一点。 </p>

<pre><code>import { Component } from &#39;@angular/core&#39;;
import { ModalController, LoadingController, ToastController, Platform } from &#39;ionic-angular&#39;;
import { File, FileEntry, Entry, FileError, DirectoryEntry} from &#39;ionic-native&#39;;

declare var Media: any; // stops errors w/ cordova-plugin-media-with-compression types

@Component({
selector: &#39;page-add-doc&#39;,
templateUrl: &#39;add-doc.html&#39;
})
export class AddDocPage {
isRecording = false;
isRecorded = false;
audioUrl =&#39;&#39;;
localAudioUrl = &#39;&#39;;
media: any;
newFileName: string;
newFileNameM4A: string;
homerAudio = &#39;http://techslides.com/demos/samples/sample.m4a&#39;

constructor(private modalCtrl: ModalController,
            private loadingCtrl: LoadingController,
            private toastCtrl: ToastController,
            private platform: Platform,
            ) {
            platform.ready()
                .then(() =&gt; {
                  console.log(&#39;Platform Ready&#39;);
                });
            }

ionViewDidLoad() {
      this.newFileName = new Date().getTime().toString();
      this.newFileNameM4A = this.newFileName +&#39;.m4a&#39;;
}

onRecordAudio() {
    this.media = new Media(this.newFileNameM4A);
    this.media.startRecord();
    this.isRecording = true;
}
onStopRecordAudio() {
    this.media.stopRecord();
    this.media.release();
    this.isRecording = false;
    this.isRecorded = true;
    try {
      File.copyFile(File.tempDirectory, this.newFileNameM4A, File.dataDirectory, this.newFileNameM4A)
      .then(
            (data: Entry) =&gt; {
            this.audioUrl = data.nativeURL;
      });
    } catch (FileError) {
      console.log(FileError)
    };
}

onPlayback() {
    this.media = new Media(this.newFileNameM4A);
    this.media.play();
    this.media.release();
}
onPlaybackTempDirectory() {
    this.media = new Media(File.tempDirectory + this.newFileNameM4A);
    this.media.play();
    this.media.release();
}

onPlaybackDataDirectory() {
    this.media = new Media(File.dataDirectory + this.localAudioUrl);
    this.media.play();
    this.media.release();
}   

onHomerAudio() {
    this.media = new Media(this.homerAudio)
    this.media.play();
    this.media.release();
}
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>相信我可能已经用 <a href="https://issues.apache.org/jira/browse/CB-7007" rel="noreferrer noopener nofollow">https://issues.apache.org/jira/browse/CB-7007</a> 的答案解决了这个问题</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 移动后iOS上的cordova-plugin-media-with-compression似乎无法播放音频文件,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42918226/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42918226/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 移动后iOS上的cordova-plugin-media-with-compression似乎无法播放音频文件