导入模块中的mp3文件时出现问题。

huangapple go评论91阅读模式
英文:

Problem with importing mp3 files in a module

问题

model.js 文件中似乎存在 HTML 转义字符(如 "),这可能导致加载音频文件时返回空对象。请确保文件路径没有额外的 HTML 转义字符。以下是修正后的 model.js 文件的示例:

import favicon from "../img/favicon.png";
import css3 from "../img/css3.png";
import html5 from "../img/html5.png";
import javascript from "../img/javascript.png";
import github from "../img/github.png";
import blobBtn from "../audio/blobBtn.mp3";
import closeBtn from "../audio/closeBtn.mp3";
import correctAnswer from "../audio/correctAnswer.mp3";
import correctUsername from "../audio/correctUsername.mp3";
import deleteRecord from "../audio/deleteRecord.mp3";
import emptyUsername from "../audio/emptyUsername.mp3";
import gameOver from "../audio/gameOver.mp3";
import navBtns from "../audio/navBtns.mp3";
import preferencesBtns from "../audio/preferencesBtns.mp3";
import restart from "../audio/restart.mp3";
import startQuiz from "../audio/startQuiz.mp3";
import wrongAnswer from "../audio/wrongAnswer.mp3";

export const files = {
  audioFiles: [
    blobBtn,
    closeBtn,
    correctAnswer,
    correctUsername,
    deleteRecord,
    emptyUsername,
    gameOver,
    navBtns,
    preferencesBtns,
    restart,
    startQuiz,
    wrongAnswer,
  ],
  imageFiles: [favicon, css3, html5, javascript, github],
  loadedFiles: 0,
  totalFiles: 0,
};

这样应该能够正常加载音频文件而不返回空对象。

英文:

I'm trying to implement a loading page in my JavaScript app that loads all the files before launching the app (twelve mp3 files and five png files) , but the way I'm doing it returns empty objects with mp3 files. I'm using parcel as bundler.

While I was loading the mp3 files through the index.html and the query selector, everything was fine. Now I'm trying to load them in the model.js file. With images everything works fine, with mp3 doesn't.

What I'm doing wrong?

model.js:

import favicon from  "../img/favicon.png"
import css3 from  "../img/css3.png"
import html5 from  "../img/html5.png"
import javascript from  "../img/javascript.png"
import github from  "../img/github.png"
import blobBtn from  "../audio/blobBtn.mp3"
import closeBtn from  "../audio/closeBtn.mp3"
import correctAnswer from  "../audio/correctAnswer.mp3"
import correctUsername from  "../audio/correctUsername.mp3"
import deleteRecord from  "../audio/deleteRecord.mp3"
import emptyUsername from  "../audio/emptyUsername.mp3"
import gameOver from  "../audio/gameOver.mp3"
import navBtns from  "../audio/navBtns.mp3"
import preferencesBtns from  "../audio/preferencesBtns.mp3"
import restart from  "../audio/restart.mp3"
import startQuiz from  "../audio/startQuiz.mp3"
import wrongAnswer from  "../audio/wrongAnswer.mp3"


export const files ={
    audioFiles: [
      blobBtn,
      closeBtn,
      correctAnswer,
      correctUsername,
      deleteRecord,
      emptyUsername,
      gameOver,
      navBtns,
      preferencesBtns,
      restart,
      startQuiz,
      wrongAnswer,
      ],
      imageFiles: [
        favicon,
        css3,
        html5,
        javascript,
        github
      ],
    loadedFiles: 0,
    totalFiles: 0,
}

controller.js:

const loadFiles = async function() {
    try {
        await Promise.all([...model.files.audioFiles, ...model.files.imageFiles].map(loadFile));
        console.log('All files are loaded'); //Doesn't start cause audioFiles is an array of empty objects
        filesLoaded();
    } catch (error) {
        console.error("There was an error loading the files:", error);
    }
}
  
const loadFile = async function(file) {
    return new Promise((resolve) => {
        console.log(file); // Ok with images, not with mp3 (12 empty objects)
        const fileType = file.includes(".mp3") ? "audio" : "image";
        const element = fileType === "audio" ? new Audio() : new Image();
        element.onload = element.oncanplaythrough = resolve;
        element.src = file;
    });
};
  
  
const filesLoaded = function() {
    loadingView.hideWindow();
    welcomeView.showWindow();
    welcomeView.startHandlers(controlUsername, controlFromWelcomeToPreferences);
}
const init =  function() {
    model.totalFiles = model.files.audioFiles.length + model.files.imageFiles.length;
    loadingView.showWindow();
    loadingView.renderData()
    loadFiles()
    /* loadingView.simulateLoading() */
}()

答案1

得分: 1

我继续运行测试,清除包缓存,并“最终”生成了导致解决方案的错误:“未找到...的转换器”错误

在搜索错误时,我最终来到了这里:Github - Parcel issues

所以,错误是文件路径字符串中缺少了**“url:”粒子**。

英文:

I continued to run tests, clearing the parcel cache, and "finally" generated the error that led me to the solution: "No transformers found for ..." error.

While searching for the error, I ended up here: Github - Parcel issues

So, the error was that the "url:" particle was missing in the file path string.

huangapple
  • 本文由 发表于 2023年5月30日 03:16:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定