在Ionic 2 / Angular 2

时间:2017-04-16 16:25:37

标签: angularjs http typescript ionic-framework ionic2

我正在构建一个音板应用程序,它通过向服务器发出http请求并创建播放音频的文件来工作。我遇到的问题是,当单击按钮并播放音频时,音频开始大约需要6-7秒。

我知道它与用户的互联网速度和服务器有很大关系,但我想知道是否有什么,我可以添加到代码中以加快此过程

我正在使用firebase托管来发送音频。

soundboard.ts

import { Component } from '@angular/core';
import { Http } from '@angular/http';


@Component({
  templateUrl: 'soundboard.html'
})
export class SoundboardPage {

  base_url: string = "http://mywebsite.com/sounds";
  sounds: any = [];
  media: any = null;

  constructor(public http: Http) {
    this.http.get(this.base_url)
      .subscribe(
        data => {
          /* Create a webpage out of the data from the http.get request */
          let content: string = data.text();
          let doc: any = document.createElement("html");
          doc.innerHTML = content;

          /* Extract all "a" tags from it */
          let links: any = doc.getElementsByTagName("a");

          /* Loop through them, saving their title and sound file */
          for(let link of links) {
            let filename = link.getAttribute("href")
            if(filename.startsWith("/")) {
              filename = this.base_url + filename
            }
            else {
              filename = this.base_url + "/" + filename
            }
            this.sounds.push({              
              file: filename
            });
          }
        },
        err => console.error('There was an error: ' + err),
        () => console.log('Get request completed')
       );
  }


  /* Plays a sound, pausing other playing sounds if necessary */
  play(sound) {
    console.log(sound)
    if(this.media) {
      this.media.pause();
    }
    this.media = new Audio(sound.file);
    this.media.load();
    this.media.play();
  }
}

0 个答案:

没有答案