简体   繁体   English

preloadjs +吼声无法播放

[英]preloadjs + howler audio doesn't play

I was trying to make my first website. 我试图建立我的第一个网站。 I'm working on local, so if you need an online website for debug it, I can upload it. 我正在本地工作,所以如果您需要在线网站进行调试,可以将其上传。 I want to create a preload section on the first visit where show a preload bar/% progress and load all elements before show the page. 我想在第一次访问时创建一个预加载部分,其中显示一个预加载栏/进度,并在显示页面之前加载所有元素。 I'm doing the audio part and the preloader of the website. 我正在做网站的音频部分和预加载器。 To accomplish this part, I'm using howler.js for audio management and preloadjs.js for the preloader part. 为了完成这一部分,我将howler.js用于音频管理,并将preloadjs.js用于preloader部分。 The problem that I can not solve is to start the mp3 at the complete function of the mp3 load. 我无法解决的问题是在mp3加载的完整功能下启动mp3。 Below is the code I used. 下面是我使用的代码。

This is the music part with howler. 这是咆哮的音乐部分。

var baseurl = document.location.origin;
var soundfolder = baseurl+'/alink/wp-content/themes/alink/media/sounds/';

//SOUNDS EFFECTS
var bgmusic = [soundfolder+'background.mp3', soundfolder+'background.ogg'];

//Music background
var musicbg = new Howl({
    src: [bgmusic[0], bgmusic[1]],
    loop: true,
    volume: 0.5,
    preload: false,
});

This is the preloader part with prealodjs. 这是prealodjs的预加载器部分。

//PRELOADER
var manifest;
var preload;
function setupManifest() {
    manifest = [{
        src:  baseurl+"/alink/wp-content/themes/alink/media/sounds/background.mp3",
        id: "mp3file"
    }
    ];
}
function startPreload() {
    preload = new createjs.LoadQueue(true);
    preload.on("fileload", handleFileLoad);
    preload.on("progress", handleFileProgress);
    preload.on("complete", loadComplete);
    preload.on("error", loadError);
    preload.loadManifest(manifest);
}
function handleFileLoad(event) {
    console.log("A file has loaded of type: " + event.item.type);
}

function loadError(evt) {
    console.log("Error!",evt.text);
}


function handleFileProgress(event) {
}

function loadComplete(event) {
    console.log("Finished Loading Assets");
    musicbg.play();
}
setupManifest();
startPreload();

Following some tutorials and js library guidelines, in the howler call I entered the "preload: false" parameter. 按照一些教程和js库指南,在咆哮调用中,我输入了“ preload:false”参数。

Without the preload part and without the "preload: false" parameter, the musical part works perfectly. 如果没有预加载部分,也没有“ preload:false”参数,则音乐部分会完美运行。 By inserting the parameter and the code part of the preloader, when the loadComplete() function is called, the music does not start. 通过插入预加载器的参数和代码部分,当调用loadComplete()函数时,音乐不会开始。 (in the console the function is called correctly. (在控制台中,该函数已正确调用。

I really can not figure out where the problem is, so I ask you what I'm doing wrong. 我真的不知道问题出在哪里,所以我问你我在做什么错。 I think there is a missing part where the file loaded from preloadjs functions is not related to howler call and it can't find the mp3 file in the cache. 我认为有一个缺少的部分,其中从preloadjs函数加载的文件与咆哮调用无关,并且在缓存中找不到mp3文件。

Thank you very much for your help. 非常感谢您的帮助。

if you load audio using PreloadJS without SoundJS "installed" to assist with preloading, the audio can only be loaded as HTMLAudio, which is pretty limited. 如果您使用PreloadJS加载音频而没有“安装” SoundJS来帮助进行预加载,则只能将音频作为HTMLAudio加载,这非常有限。 I am fairly certain Howler uses the WebAudio context, so it would just re-load the audio when it needs it. 我可以肯定,Howler使用WebAudio上下文,因此它将在需要时重新加载音频。

PreloadJS is really only designed to load WebAudio sounds to be played with SoundJS . PreloadJS实际上仅设计为加载要用SoundJS播放的WebAudio声音。 In fact, it actually uses a lot of shared code to download and prepare audio for playback. 实际上,它实际上使用了大量共享代码来下载和准备音频以供回放。 This is not necessarily by design (to prevent usage of other libraries), but WebAudio uses a shared audio context when preloading audio buffers, and PreloadJS knows how to share that context with SoundJS. 这不一定是设计使然(防止使用其他库),但是WebAudio在预加载音频缓冲区时使用共享的音频上下文,并且PreloadJS知道如何与SoundJS共享该上下文。 Howler is probably similar, where it preloads using an audio context it knows how to work with. 咆哮可能与之类似,它使用它知道如何工作的音频上下文进行预加载。

As a maintainer of the CreateJS libraries I am certainly biased, but if you want to preload audio with PreloadJS, then SoundJS is a better option than Howler. 作为CreateJS库的维护者,我当然有偏见,但是如果您想使用PreloadJS预加载音频,那么SoundJS是比Howler更好的选择。

var queue = new createjs.LoadQueue();
queue.installPlugin(createjs.Sound); // Tell PreloadJS to use SoundJS to load audio
// ... other queue stuff

// After loading is done:
createjs.Sound.play("soundId");

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM