sing.engine.js 11.5 KB
+(function ($, window, document, undefined) {
"use strict";

    var pluginName = "engine";

    var i = 0;
    var jsIsReady = false;

    function Plugin (options) {
        jsIsReady = true;
        this.settings = $.extend(true, {}, Plugin.defaultOptions, options);
        this.icount = i++;
        this.init();
    };

    window.ssoundLog = function(msg) {
        //$("#result").append('<li>' + msg + '</li>');
        if (window.console && ssoundLog) console.log(msg);
    };

    window.jsIsReady = function() {
        return jsIsReady;
    }

    $.extend(Plugin, {
        swfs: {},

        defaultOptions: {
            swf: {
                swfVersionStr: "11.1.0",
                xiSwfUrlStr: "playerProductInstall",
                flashvars: {
                    delayInitTime: 1000,
                    flashLoadEventHandler: "flashLoadEventHandler",
                    jsLogCallback: "ssoundLog"
                },
                params: {
                    quality: "high",
                    bgcolor: "#ffffff",
                    allowscriptaccess: "*",
                    allowfullscreen: "true"
                },
                attributes: {
                    id: "SSoundSwf",
                    name: "SSoundSwf",
                    align: "middle"
                },
                width: "0px",
                height: "0px",
                domId: "SSoundWindow",
                swfCss: "display:none;text-align:left;",
                swfFile: "/engine/SSound.swf"
            },
            init: {
                userId: "anniywell",

                factroyTimeout: 50000,                                        
                coreTimeout: 50000,                                      
                retryTimeout: 50000,

                logLevel: "ALL",
                logTarget: 'JSTarget',
                backgroundColor: 0x777777,    

                urlListServiceUrl:['http://list.cloud.ssapi.cn:8080/list.json'],
                defaultAPIServiceUrlArray: ["ws://api.cloud.ssapi.cn:8080"],

                
                appKey: "99ce071b84438018",
                secretKey: "0bb41148d4fb526d05cfb9db694d4186" 
            },
            start: {
                playDing: false,
                recordLength:5000,
                serverParam: {   
                    coreType: "en.sent.score",           
                    //res: "core resource",
                    rank: 100,  
                    scoreType:100,
                    refText: "",
                    attachAudioUrl:1,                      // 返回音频下载地址
                    precision: 0.5,
                    userId: "anniywell",
                    robust: 0
                }
            },
            ssoundEvent: {
                micAllowed: function() {
                    ssoundLog('micAllowed');
                },
                micActivity: function() {
                    ssoundLog('micActivity');
                },
                micException: function() {
                    ssoundLog('micException');
                },
                timeoutException: function() {
                    ssoundLog('timeoutException');
                },
                recordException: function() {
                    ssoundLog('recordException');

                },
                netException: function() {
                    ssoundLog('netException');

                },
                requestException: function(t, resultObject) {
                    ssoundLog('result:' + JSON.stringify(resultObject));
                },
                ready: function() {
                    ssoundLog('ready');

                },

                swfReady: function() {

                },

                startRecord: function() {
                    ssoundLog('startRecord');

                },
                stopRecord: function() {
                    ssoundLog('stopRecord');

                },
                startReplay: function() {
                    ssoundLog('startReplay');

                },
                stopReplay: function() {
                    ssoundLog('stopReplay');

                },
                error: function(code) {
                    ssoundLog('error:', JSON.stringify(code));
                },

                micLevel: function(level) {
                    //ssoundLog('level:' +  level);
                },

                playPosition: function(channel) {
                    ssoundLog('channel:' + JSON.stringify(channel));
                },

                soundCompleteHandler: function() {
                    ssoundLog('sound: play end');
                },

                result: function(t, resultObject) {
                    this.result = resultObject;
                    ssoundLog('result:' + JSON.stringify(resultObject));
                }
            }

        }, 
        prototype: {
            init: function() {
                var o = this, s = this.settings;
                
                swfobject.embedSWF(
                        s.swf.swfFile,
                        s.swf.domId,
                        s.swf.width,
                        s.swf.height,
                        s.swf.swfVersionStr,
                        s.swf.xiSwfUrlStr,
                        s.swf.flashvars,
                        s.swf.params,
                        s.swf.attributes, function () {
                        }
                    );
                swfobject.createCSS("#" + s.swf.domId, s.swf.swfCss);
                this.swfObj = null;
                this.isPlay = 0;
                this.isLoadSwf = false;
                this.micAllowed = false;

                this.interval = null;
                o.connect();
            },

            newEngine: function() {
                var i = this.swfObj.init(this.settings.init);                
                //ssoundLog(i);
            },

            start: function(txt) {
                var o = this.settings.start;
                if (typeof txt == "string") {
                		o.serverParam.refText = txt;
                } else {
                		o = $.extend(o, txt)
                }
                
                var r = this.swfObj.startRecord(o);
                eval("r=" + r);
                if (r[0].statusCode != 50004) this.settings.ssoundEvent.error(r);
            },

            stop: function() {
                this.swfObj.stopRecord();
            },

            dispose: function() {
                this.swfObj.dispose();
            },

            startReplay: function(o) {
                o = $.extend({
                    tokenId: this.result && this.result.tokenId ? this.result.tokenId : '',
                    startPos: 0,
                    stopPos: 1000
                }, o);
                var r = this.swfObj.startReplay(o);
                eval("r=" + r);
                if (r[0].statusCode != 50004) this.settings.ssoundEvent.error(r);
            },

            getSoundAttr: function() {
                if (this.isLoadSwf) {
                    //v = {
                        //bytesTotal: _sound.bytesTotal,
                        //length: _sound.length,
                        //bytesLoaded: _sound.bytesLoaded
                    //};
                    return this.swfObj.getAttr('Sound');
                }

            },

            loadSound: function(file) {
                if (this.isLoadSwf) {
                    this.swfObj.loadSound(file);	
                }
            },

            playSound: function() {
                if (this.isLoadSwf && !this.isPlay) {
                    this.isPlay = 1;
                    this.swfObj.playSound();
                }
            },

            pauseSound: function() {
                if (this.isLoadSwf && this.isPlay) {
                    this.isPlay = 0;
                    this.swfObj.pauseSound();
                }
            },

            stopSound: function() {
                if (this.isPlay < 1) return ;
                this.isPlay = 0;
                //window.clearInterval(this.interval);
                this.swfObj.stopSound();
            },

            testMicrophone: function(success) {
                var g = this;
                var s = g.swfObj.state({});
                var state = (s.microphone.state == "microphone.muted");
                g.micAllowed = !state;

                if (state) {
                    //g.swfObj.showSetting();
                    var c = $('#' + g.settings.swf.attributes.name);
                    c.css("width", "215px");
                    c.css("height", "138px");
                    var ti = window.setInterval(function(){
                        if (g.micAllowed) {
                            window.clearInterval(ti);
                            c.css("width", "0px");
                            c.css("height", "0px");
                            if (success) success();
                        }
                    }, 300);
                    return ;
                }
                if (success) success();
            },

            connect: function() {
                var o = this;
                var s = this.settings; 
                var e = s.ssoundEvent;
                var ename = "ssoundEvent" + this.icount;
                var name = s.swf.attributes.id;

                window[ename] = function() {
                    var type = arguments[0];
                    switch (type) {
                        case 'micAllowed':
                            o.micAllowed = true;
                            break;
                    }
                    if (e[type]) {
                        e[type].apply(o, arguments);
                    }
                }

                var g;
                if(navigator.appName.indexOf("Microsoft") != -1) {
                    g = window;
                } else {
                    g = document;
                }
                

                window.flashLoadEventHandler = function(statusCode, returnStr) {
                    // alert(statusCode);
                    if (statusCode == 50000) {
                        var ti = window.setInterval(function(){
                            if (g[name] && g[name].getAttr) {
                                window.clearInterval(ti);
                                o.swfObj = g[name];
                                if (e.swfReady) e.swfReady();
                                o.swfObj.setHandler(ename);
                                o.isLoadSwf = true;
                                o.interval = window.setInterval(function(){
                                    if (o.swfObj) {
                                        if (o.swfObj.getmicActivityLevel && s.ssoundEvent.micLevel) 
                                            s.ssoundEvent.micLevel(o.swfObj.getmicActivityLevel());
                                        var ps = o.swfObj.getAttr('Sound.play');
                                        if (ps.position && s.ssoundEvent.playPosition) s.ssoundEvent.playPosition(ps);
                                    }
                                }, 100);

                            }
                        }, 300);

                    }
                }
            }
        }
    });
    window["sing"] = {};
    window["sing"][pluginName] = Plugin;

})(jQuery, window, document);