音乐流媒体服务

时间:2017-07-13 05:38:23

标签: javascript php node.js server stream

我有一个用React写的客户端。此外,在我的Google云端硬盘上,我收到了一些音乐。我想制作一个音乐流媒体服务,并通过我的Google云端硬盘在线直播我的音乐。我是服务器端编程的菜鸟。你建议我怎么读或写这样的收音机?

1 个答案:

答案 0 :(得分:0)

您可以使用<audio>元素播放音频媒体。循环媒体回放的一种方法是创建包含对象内媒体资源的路径的数组。

canplay元素调用<audio>的{​​{1}}事件中.play()。在ended事件返回已解决的Promise

将数组传递给使用已解析的.reduce()初始化的Promise。播放完所有媒体曲目后,使用.then()再次呼叫功能。

您还可以使用Boolean标志来停止对同一功能的重复调度,该功能循环播放所请求媒体资源的媒体。

&#13;
&#13;
const mediaPlaylist = [{
  "track": "https://doc-0c-48-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/dhtqfqf4pt2b3kmukb3m1bcqjcsgcu8o/1499925600000/15486589845087228196/*/0B30WhR3Lbl-cQ3NFRzBuVk5KN28",
  "title": "Hypnotize U"
}, {
  "track": "https://doc-08-48-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/1fk2t6ot905rc6fs7ifil1sag5vr5h22/1499925600000/15486589845087228196/*/0B30WhR3Lbl-cR2JEQ3VvT0dxYzQ?e=download",
  "title": "Rock Star"
}, {
  "track": "https://doc-0c-48-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/rmop35nhocd25kroorov3p336f15i9m5/1499925600000/15486589845087228196/*/0B30WhR3Lbl-cU2lsdjVpMTRUcGM?e=download",
  "title": "She Want To Move"
}];

const audio = document.querySelector("audio");

const nowPlaying = audio.nextElementSibling;

const mediaTracks = ((promise, {
    track,
    title
  }) =>
  promise.then(() => new Promise(resolve => {
    audio.src = track;
    audio.addEventListener("canplay", event => {
      audio.play();
      nowPlaying.textContent = title;
    }, {
      once: true
    });
    audio.addEventListener("ended", event => {
      nowPlaying.textContent = "";
      resolve();
    }, {
      once: true
    });
  }))
);

let stopMedia = false;

const mediaLoop = (playlist = Array()) =>
  !stopMedia 
  ? playlist.reduce(mediaTracks, Promise.resolve()) 
  : Promise.resolve("media loop stopped");

const playMedia = () => 
mediaLoop(mediaPlaylist).then(playMedia);

playMedia()
.then(message => console.log(message))
.catch(err => {console.log(err); throw err});
&#13;
<audio controls></audio>Now playing: <label></label>
&#13;
&#13;
&#13;