我正在编写一个非常简单的教程,介绍如何使用Tabrisjs播放音频文件,这个简单的代码可以在Playground中测试。它可以工作,但只播放音频文件一次,我必须重新加载才能重新播放。如何让音频不止一次播放?
// simple example to play audio file
const {Button, ui} = require('tabris');
var my_media = new Media('http://static1.grsites.com/archive/sounds/cartoon/cartoon001.mp3');
let btnPlay = new Button({
centerX: 0, centerY: 0,
text: 'Play sound',
background: 'blue',
textColor: 'white',
font: '24px'
}).on("select", function() {
my_media.play();
}).appendTo(ui.contentView);
参考: - 我试图简化tabrisjs.com提供的示例
这将正确播放音频文件:
MediaPage.js
https://github.com/eclipsesource/tabris-js/tree/master/examples/cordova
答案 0 :(得分:1)
这似乎可以做我想要的一切 - 包括打断正在播放的声音。您可以在tabrisjs.com操场上进行测试。
// simple example to play audio file
// ref: https://github.com/apache/cordova-plugin-media#readme
const {Button, ui} = require('tabris');
const SOUND_1 = 'http://static1.grsites.com/archive/sounds/cartoon/cartoon001.mp3';
const SOUND_2 = 'http://static1.grsites.com/archive/sounds/cartoon/cartoon002.mp3';
const SOUND_3 = 'http://static1.grsites.com/archive/sounds/cartoon/cartoon003.mp3';
let btnPlay1 = new Button({
centerX: 0,
top: 100,
text: 'Play sound 1',
background: 'blue',
textColor: 'white',
font: '24px'
}).on('select', function() {
playAudio(SOUND_1)
}).appendTo(ui.contentView);
let btnPlay2 = new Button({
centerX: 0,
top: 'prev() 10',
text: 'Play sound 2',
background: 'red',
textColor: 'white',
font: '24px'
}).on('select', function() {
playAudio(SOUND_2)
}).appendTo(ui.contentView);
let btnPlay3 = new Button({
centerX: 0,
top: 'prev() 10',
text: 'Play sound 3',
background: 'green',
textColor: 'white',
font: '24px'
}).on('select', function() {
playAudio(SOUND_3)
}).appendTo(ui.contentView);
function playAudio(url) {
// Play the audio file at url
var my_media = new Media(url,
// success callback
function() {
console.log('playAudio(): ' + url + ' Audio Success');
},
// error callback
function(err) {
console.log('playAudio(): ' + url + 'Audio Error: ' + err);
}
);
my_media.play();
}
答案 1 :(得分:0)
我尝试了你的代码, 不止一次播放,但是文件不会播放第二次,直到第一次播放结束。如果您希望它自动中断,即每次点击按钮时从头开始播放,您需要修改select
处理程序:
my_media.stop();
my_media.play();
cordova-plugin-media docs详细介绍了Media
类中可用的所有方法。