HTML5 音频:如何仅播放音频文件(音频精灵)的选定部分?

发布于 2024-11-05 12:31:54 字数 1905 浏览 1 评论 0原文

我正在开发一个 iOS 节拍器网络应用程序。由于移动 Safari 只能一次播放一个声音,我'我尝试创建一个“音频精灵” - 我可以使用单个音轨的不同片段来生成不同的声音。我有一个 1 秒的剪辑,上面有 2 个半秒的声音。

<audio id="sample" src="sounds.mp3"></audio>

<a href="javascript:play1();">Play1</a>
<a href="javascript:play2();">Play2</a>
<a href="javascript:pauseAudio();">Pause</a>

<script>
var audio = document.getElementById('click');

function play1(){
    audio.currentTime = 0;
    audio.play();

    // This is the problem area
    audio.addEventListener('timeupdate', function (){
        if (currentTime >= 0.5) {
            audio.pause();
        }
    }, false);
}   

function play2(){
    audio.currentTime = 0.5;
    audio.play();
}

function pause(){
    audio.pause();
}
</script>

请在 JSFiddle 上查看。

如您所见,我尝试使用“timeupdate”事件(jPlayer 示例)但无济于事。

我看过 Remy Sharp 关于音频精灵的帖子,但是 (A)我无法让它为我工作,并且(B)我宁愿远离库依赖。

有什么想法吗?


更新

我现在可以使用setInterval工作:

function play1(){
    audio.currentTime = 0;
    audio.play();
    int = setInterval(function() {
        if (audio.currentTime > 0.4) {
            audio.pause();
            clearInterval(int);
        }
    }, 10);
}    

function play2(){
    clearInterval(int);
    audio.currentTime = 0.5;
    audio.play();
}

顺序和设置/清除间隔存在一些问题,但出于我的目的 - 似乎工作。

JSFiddle

PS 如果有人对此有解决方案,则可以在游戏和界面音效中使用。

I'm working on an iOS metronome web app. Since mobile Safari can only play one sound at a time, I'm attempting to create an 'audio sprite' - where I can use different segments of a single audio track to generate different sounds. I have a 1 second clip with 2 half-second sounds on it.

<audio id="sample" src="sounds.mp3"></audio>

<a href="javascript:play1();">Play1</a>
<a href="javascript:play2();">Play2</a>
<a href="javascript:pauseAudio();">Pause</a>

<script>
var audio = document.getElementById('click');

function play1(){
    audio.currentTime = 0;
    audio.play();

    // This is the problem area
    audio.addEventListener('timeupdate', function (){
        if (currentTime >= 0.5) {
            audio.pause();
        }
    }, false);
}   

function play2(){
    audio.currentTime = 0.5;
    audio.play();
}

function pause(){
    audio.pause();
}
</script>

See it on JSFiddle.

As you can see, I've attempted to use the 'timeupdate' event (jPlayer example) with no avail.

I've seen Remy Sharp's post on audio sprites, but (A) I wasn't able to get it to work for me, and (B) I would prefer to stay away from a library dependency.

Any ideas?


Update

I now have it working using setInterval:

function play1(){
    audio.currentTime = 0;
    audio.play();
    int = setInterval(function() {
        if (audio.currentTime > 0.4) {
            audio.pause();
            clearInterval(int);
        }
    }, 10);
}    

function play2(){
    clearInterval(int);
    audio.currentTime = 0.5;
    audio.play();
}

There are some issues with sequence and setting/clearing intervals, but for my purpose - it seems to work.

JSFiddle

P.S. If anyone has a fix for this, this could be used in Games and Interface Sound FX.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捎一片雪花 2024-11-12 12:31:54

我认为这里有几个问题。

首先,每次用户单击 Play 1 时,您都会添加一个事件侦听器。

另外我认为

if (currentTime >= 0.5) { ...

应该是

if (audio.currentTime >= 0.5) { ...

这也有效:

<audio id="sample" src="http://dl.dropbox.com/u/222645/click1sec.mp3" controls preload></audio>

<a href="javascript:playSegment(0.0, 0.5);">Play1</a>
<a href="javascript:playSegment(0.5);">Play2</a>

<script>
var audio = document.getElementById('sample');
var segmentEnd;

audio.addEventListener('timeupdate', function (){
    if (segmentEnd && audio.currentTime >= segmentEnd) {
        audio.pause();
    }   
    console.log(audio.currentTime);
}, false);

function playSegment(startTime, endTime){
    segmentEnd = endTime;
    audio.currentTime = startTime;
    audio.play();
}
</script>

I think there are a couple of problems here.

Firstly, you're adding an event listener every time the user clicks Play 1.

Also I think

if (currentTime >= 0.5) { ...

should be

if (audio.currentTime >= 0.5) { ...

This also works:

<audio id="sample" src="http://dl.dropbox.com/u/222645/click1sec.mp3" controls preload></audio>

<a href="javascript:playSegment(0.0, 0.5);">Play1</a>
<a href="javascript:playSegment(0.5);">Play2</a>

<script>
var audio = document.getElementById('sample');
var segmentEnd;

audio.addEventListener('timeupdate', function (){
    if (segmentEnd && audio.currentTime >= segmentEnd) {
        audio.pause();
    }   
    console.log(audio.currentTime);
}, false);

function playSegment(startTime, endTime){
    segmentEnd = endTime;
    audio.currentTime = startTime;
    audio.play();
}
</script>
甜`诱少女 2024-11-12 12:31:54

我没有找到一个干净的函数来播放音频对象的片段,所以这就是我想出的。它还将解决用户在短时间内单击多个触发器时会发生的以下错误。

“未捕获(承诺中)DOMException:play() 请求被暂停() 调用中断”

const audioObj_1 = new Audio('/some_audio_file.m4a');
playSegment(audioObj_1 , 1.11, 2.45); // this will play from 1.11 sec to 2.45 sec

function playSegment(audioObj, start, stop){
    let audioObjNew = audioObj.cloneNode(true); //this is to prevent "play() request was interrupted" error. 
    audioObjNew.currentTime = start;
    audioObjNew.play();
    audioObjNew.int = setInterval(function() {
        if (audioObjNew.currentTime > stop) {
            audioObjNew.pause();
            clearInterval(audioObjNew.int);
        }
    }, 10);
} 

I didn't find a clean function to play segment of an audio object so this is what I come up with. It will also solves the the following error which will occur if the user click multiple triggers within short amount of time.

"Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause()"

const audioObj_1 = new Audio('/some_audio_file.m4a');
playSegment(audioObj_1 , 1.11, 2.45); // this will play from 1.11 sec to 2.45 sec

function playSegment(audioObj, start, stop){
    let audioObjNew = audioObj.cloneNode(true); //this is to prevent "play() request was interrupted" error. 
    audioObjNew.currentTime = start;
    audioObjNew.play();
    audioObjNew.int = setInterval(function() {
        if (audioObjNew.currentTime > stop) {
            audioObjNew.pause();
            clearInterval(audioObjNew.int);
        }
    }, 10);
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文