首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WIkitude为每个图像目标添加声音

WIkitude为每个图像目标添加声音
EN

Stack Overflow用户
提问于 2014-05-10 16:36:30
回答 1查看 1K关注 0票数 0

我想问一下,是否可以为每个图像目标添加不同的声音。我有4个图像目标,我想播放不同的.mp3文件,当他们每一个被识别。我能这样做吗?如果你能提供一些示例代码,那就太好了。谢谢!

编辑:当调用AR.HtmlDrawableAR.HtmlDrawable方法时,我尝试播放声音(除了有4个图像目标外,我目前正在查看ImageRecognition HtmlDrawable示例),但我无法让它对每个目标播放不同的声音。另外,你能告诉我定义声音对象的最佳地点吗?目前,我有一个函数createSound,它位于我的World var中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-15 15:49:24

查看Wikitude示例ImageRecognition/MultipleTarget。下载SDK时,您可以在示例中找到它。通过添加以下代码扩展示例:

代码语言:javascript
复制
sound1: null,
sound2: null,

loadAudio: function() {
    this.sound1 = new AR.Sound("assets/sound1.mp3", {
        onLoaded: function() {
            // if the sound finished loading
        },
        onError: function() {
            // alert the user that the sound file could not be loaded
        },
    });

    this.sound2 = new AR.Sound("assets/sound2.mp3", {
        onLoaded: function() {
            // if the sound finished loading
        },
        onError: function() {
            // alert the user that the sound file could not be loaded
        },
    });
},

然后在init函数中调用loadAudio。然后实现了onEnterFieldOfVision函数的Trackable2DObject

代码语言:javascript
复制
onEnterFieldOfVision: function() {
    if (World.sound2 !== null) {
        World.sound1.play();
    }
}

在这里,完整的源代码:

代码语言:javascript
复制
var World = {
loaded: false,

init: function initFn() {
    /* Disable all sensors in "IR-only" Worlds to save performance. If the property is set to true, any geo-related components (such as GeoObjects and ActionRanges) are active. If the property is set to false, any geo-related components will not be visible on the screen, and triggers will not fire.*/
    AR.context.services.sensors = false;
    this.loadAudio();
    this.createOverlays();
},

sound1: null,
sound2: null,

loadAudio: function() {
    this.sound1 = new AR.Sound("assets/sound1.mp3", {
        onLoaded: function() {
            // if the sound finished loading
        },
        onError: function() {
            // alert the user that the sound file could not be loaded
        },
    });

    this.sound2 = new AR.Sound("assets/sound2.mp3", {
        onLoaded: function() {
            // if the sound finished loading
        },
        onError: function() {
            // alert the user that the sound file could not be loaded
        },
    });
},

createOverlays: function createOverlaysFn() {
    // Initialize Tracker
    this.tracker = new AR.Tracker("assets/magazine.wtc", {
        onLoaded: this.worldLoaded
    });

    // Create overlay for page one
    var imgOne = new AR.ImageResource("assets/imageOne.png");
    var overlayOne = new AR.ImageDrawable(imgOne, 1, {
        offsetX: -0.15,
        offsetY: 0
    });
    var pageOne = new AR.Trackable2DObject(this.tracker, "pageOne", {
        drawables: {
            cam: overlayOne
        },
        onEnterFieldOfVision: function() {
            if (World.sound2 !== null) {
                World.sound1.play();
            }
        }
    });

    // Create overlay for page two
    var imgTwo = new AR.ImageResource("assets/imageTwo.png");
    var overlayTwo = new AR.ImageDrawable(imgTwo, 0.5, {
        offsetX: 0.12,
        offsetY: -0.01
    });
    var pageTwo = new AR.Trackable2DObject(this.tracker, "pageTwo", {
        drawables: {
            cam: overlayTwo
        },
        onEnterFieldOfVision: function() {
            if (World.sound2 !== null) {
                World.sound2.play();
            }
        }
    });
},

worldLoaded: function worldLoadedFn() {
    var cssDivLeft = " style='display: table-cell;vertical-align: middle; text-align: right; width: 50%; padding-right: 15px;'";
    var cssDivRight1 = " style='display: table-cell;vertical-align: middle; text-align: left; padding-right: 15px; width: 38px'";
    var cssDivRight2 = " style='display: table-cell;vertical-align: middle; text-align: left; padding-right: 15px;'";
    document.getElementById('loadingMessage').innerHTML =
        "<div" + cssDivLeft + ">Scan Target &#35;1 (surfer) or &#35;2 (biker):</div>" +
        "<div" + cssDivRight1 + "><img src='assets/surfer.png'></img></div>" +
        "<div" + cssDivRight2 + "><img src='assets/bike.png'></img></div>";
}
};

World.init();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23583662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档