Javascript:在浏览器中播放

the*_*ror 5 javascript airplay

有没有办法在浏览器中使用 JavaScript 将图像、视频和音频发送到 AirPlay 服务器?

Lau*_*rin 3

在 JavaScript 中不可能实现这一点。但是,您也许可以使用NPAPI插件从浏览器运行它(非常痛苦)。

如果您可以运行本地服务器,有几个 node.js 模块可以使这变得更加容易。以下示例将流式传输发布到附近 AirPlay 设备的任何音频文件。

您可以使用以下方法进行测试:

curl -X POST --data-binary @sample.mp3 http://localhost:8080/audio
Run Code Online (Sandbox Code Playgroud)

它假设 FFmpeg 位于 /usr/local/bin/ffmpeg,并且 AirPlay 设备在 localhost:5000 上可用(您可以尝试使用 Airfoil 扬声器)。

var airtunes = require('airtunes'),
    express = require('express'),
    app = express(),
    device = airtunes.add('localhost'),
    spawn = require('child_process').spawn;

app.post('/audio', function(req, res) {
  // use ffmpeg to reencode data on the fly
  var ffmpeg = spawn('/usr/local/bin/ffmpeg', [
    '-i', 'pipe:0',       // Read from stdin
    '-f', 's16le',        // PCM 16bits, little-endian
    '-ar', '44100',       // Sampling rate
    '-ac', 2,             // Stereo
    'pipe:1'              // Output to stdout
  ]);

  // pipe data to AirTunes
  ffmpeg.stdout.pipe(airtunes, { end: false });

  // detect if ffmpeg was not spawned correctly
  ffmpeg.stderr.setEncoding('utf8');
  ffmpeg.stderr.on('data', function(data) {
    if(/^execvp\(\)/.test(data)) {
      console.log('failed to start ' + argv.ffmpeg);
      process.exit(1);
    }
  });

  req.pipe(ffmpeg.stdin);

  req.on('end', function() {
    res.end();
 });
});

device.on('status', function(status) {
  console.log('status: ' + status);
});

console.log('listening on port 8080');
app.listen(8080);
Run Code Online (Sandbox Code Playgroud)