l***@21cn.com
l***@21cn.com
  • 发布:2020-05-17 22:27
  • 更新:2022-02-17 09:44
  • 阅读:1627

贡献native.js(NJS)一些代码,将持续更新

分类:HTML5+

===为了书写方便,同时为了性能,频繁使用importClass()导入类会损失更多性能,所以封装了导入类=====================
使用方法:
iosImport('Hello'); //默认导入windows全局变量
var hello = new Hello();
hello.say();

如果不想导入windows全局变量,则可以这样:
var Hello = iosImport('Hello', false); //多传入一个参数false
var hello = new Hello();
hello.say();

    var androidImported = {};  
    var iosImported = {};  
  
    window.androidImport = function(classname, global) {  
        if (false !== global) {  
            global = true;  
        }  
  
        if (!window.plus) {  
            return null;  
        }  
  
        if (!androidImported[classname]) {  
            androidImported[classname] = plus.android.importClass(classname);  
        }  
  
        if (global) {  
            var myClass = classname.split('.').pop();  
            if (!window[myClass]) {  
                window[myClass] = androidImported[classname];  
            }  
        }  
  
        return androidImported[classname];  
    }  
  
    window.iosImport = function(classname, global) {  
        if (false !== global) {  
            global = true;  
        }  
  
        if (!window.plus) {  
            return null;  
        }  
  
        if (!iosImported[classname]) {  
            iosImported[classname] = plus.ios.importClass(classname);  
        }  
  
        if (global) {  
            var myClass = classname.split('.').pop();  
            if (!window[myClass]) {  
                window[myClass] = iosImported[classname];  
            }  
        }  
  
        return iosImported[classname];  
    }

===播放自定义提示音(需离线打包,添加自己的声音资源)================================
搞这个是因为官方没提供播放系统声音的方法,而使用plus.ios.invoke又没效果,比如plus.ios.invoke(null, 'AudioServicesPlaySystemSound', 1103);压根不起作用,官方的plus.audio.createPlayer感觉又有点麻烦,也不知道能否播放自己添加的声音资源,还需要添加liblibMedia.a,libopencore-amrnb.a,libmp3lame.a这几个库文件,增加包尺寸。

使用方法:
iosPlaySystemSound('my.wav');

window.iosPlaySystemSound = function(filename) {  
        if (!window.plus) {  
            return false;  
        }  
  
        iosImport('NSBundle');  
        iosImport('AVAudioPlayer');  
  
        var bundle = NSBundle.mainBundle();  
        var soundUrl = bundle.URLForResourcewithExtension(filename, null);  
  
        var player = new AVAudioPlayer();  
        player.initWithContentsOfURLerror(soundUrl, null);  
        if (!player) {  
            console.log('failed to play: ' + filename);  
            return false;  
        }  
        player.setNumberOfLoops(0); //-1:无限循环  
        player.setVolume(1);  
        player.prepareToPlay();  
        player.play();  
    }

===短震,震动反馈==============================================

window.shortVibrate = function() {  
        if (!window.plus) {  
                return false;  
        }  
  
        if (plus.os.name == 'iOS') {  
            iosImport('UIImpactFeedbackGenerator');  
            var impact = new UIImpactFeedbackGenerator();  
            impact.prepare();  
            impact.init(1);  
            impact.impactOccurred();  
        } else {  
            window.plus.device.vibrate(10);  
        }  
    }
0 关注 分享

要回复文章请先登录注册

FullStack

FullStack

播放系统提示声(ios):[https://ext.dcloud.net.cn/plugin?id=7432](https://ext.dcloud.net.cn/plugin?id=7432)
2022-02-17 09:44
l***@21cn.com

l***@21cn.com (作者)

回复 1***@qq.com :
插件这玩意太麻烦了,懒得整。
2020-05-18 11:03
1***@qq.com

1***@qq.com

Ios不是有一个插件吗
2020-05-18 07:18