cordova的Media插件中的Media.release() [英] Media.release() in cordova's Media plugin

查看:379
本文介绍了cordova的Media插件中的Media.release()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Media.release()如何工作。看看文档,它觉得你必须这样使用它:

How does Media.release() work. Looking at the docs it feels that you have to use it like this:

MediaService.loadMedia('sounds/connection-error.wav').then(function(media){
    media.play();
    media.release();
});

但是我google了足够知道这是错误的。我们必须在Android上显式释放核心实例。

But I googled enough to know that is wrong. We have to explicitly release the core instances on Android.

但是如何实现呢?如果我有8个视图在我的应用程序,如果我在每个视图上播放一个声音文件是否被视为8个核心实例被使用?我可以回去说视图号1,并再次播放与该视图相关联的声音?

But how to do that? If I have 8 views in my app and if I play a sound file on each of those views does that count as 8 core instances being used? And can I go back to say view number 1 and again play the sound associated with that view? If so, would that count as a 9th instances ?

直接调用 media.release()就像上面一样

推荐答案

使用Cordova Media插件播放声音的最常见方法如下:

Most common way to play sounds using Cordova Media plugin is following:

function playAudio(src) {
    // HTML5 Audio
    if (typeof Audio != "undefined") { 
        new Audio(src).play() ;

    // Phonegap media
    } else if (typeof device != "undefined") {

        // Android needs the search path explicitly specified
        if (device.platform == 'Android') {
            src = '/android_asset/www/' + src;
        }

        var mediaRes = new Media(src,
            function onSuccess() {
               // release the media resource once finished playing
               mediaRes.release();
            },
            function onError(e){
                console.log("error playing sound: " + JSON.stringify(e));
            });
         mediaRes.play();
   } else {
       console.log("no sound API to play: " + src);
   }
}

这篇关于cordova的Media插件中的Media.release()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆