简体   繁体   English

使用JavaScript播放/暂停MP3问题

[英]playing/pausing MP3 with JavaScript Issue

I have a problem with pausing a currently playing mp3. 我暂停当前正在播放的mp3时遇到问题。

For an example, if I run my function like this: playSong(drake); 例如,如果我这样运行我的函数: playSong(drake); it starts running the code out of the first part of my if statement and it's playing the MP3 out of the "drake" object and sets songValue=2 The problem is that it doesn't pause the song if I run it the second time but my console.log gets displayed in the console, so it definitely runs the second part of my if statement when I click it the second time but it doesn't pause the song for some reasons. 它从if语句的第一部分开始运行代码,并从“ drake”对象中播放MP3并设置songValue = 2问题是,如果我第二次运行它,它不会暂停歌曲,但是我的console.log会显示在控制台中,因此当我第二次单击它时,它肯定会运行if语句的第二部分,但由于某些原因它不会暂停歌曲。

//object with mp3 audio
var drake = {
    value: 3,
    name: 'Energy',
    artist: 'Drake',
    audio: 'energy.mp3', //
    img: '<img style="width: 50%; margin-right: 25%; margin-left: 25%; margin-top: 10%;" src="http://www.getrichrapping.com/wp-content/uploads/2015/08/Drake-Energy.jpg">'
};


songValue = 1;

// plays and SHOULD pause a Song
function playSong(object) {
    var Song = new Audio(object.audio);

    //plays the song 
    if (songValue == 1) {

        Song.play();
        songValue = 2;

        // SHOULD pause the song when the functions is runned the second time
    } else if (songValue == 2) {
        console.log("Is it working ?"); // String to test if the "else if" gets runned
        Song.pause();
        songValue = 1;

    }
};

move the var Song = new Audio(object.audio) outside playSong. 将var Song = new Audio(object.audio)移到playSong之外。

You essentially make a new audio each time you call the function! 每次调用该函数时,您基本上都会制作一个新的音频!

of course you would need to make changes to reference the object.audio since object is not a global. 当然,由于对象不是全局对象,因此您需要进行更改以引用object.audio。

A better way is to do like this: 更好的方法是这样做:

 //object with mp3 audio var drake = { value: 3, name: 'Energy', artist: 'Drake', audio: 'https://www.w3schools.com/html/horse.mp3', // img: '<img style="width: 50%; margin-right: 25%; margin-left: 25%; margin-top: 10%;" src="http://www.getrichrapping.com/wp-content/uploads/2015/08/Drake-Energy.jpg">' }; songValue = 1; // plays and SHOULD pause a Song function playSong(object) { //plays the song if (songValue == 1) { var player = document.getElementById('song'); var sourceMp3 = document.getElementById('sourceMp3'); sourceMp3.src = object.audio; player.load(); //just start buffering (preload) player.play(); //start playing songValue = 2; // SHOULD pause the song when the functions is runned the second time } else if (songValue == 2) { console.log("Is it working ?"); // String to test if the "else if" gets runned var player = document.getElementById('song'); if (!player.paused) player.pause(); songValue = 1; } }; 
 <audio id="song" controls="controls"> <source id="sourceMp3" src="" type="audio/mp3" /> Your browser does not support the audio element. </audio> <p> <button onclick='playSong(drake)'> Play Song </button> </p> 

Referenced fiddle: http://jsfiddle.net/jm6ky/2/ 参考小提琴: http : //jsfiddle.net/jm6ky/2/

Updated fiddle: http://jsfiddle.net/c6jgjewg/2/ 更新的小提琴: http : //jsfiddle.net/c6jgjewg/2/

You should store you Audio object in your base object like this 您应该像这样将Audio对象存储在基础对象中

if (object.song == undefined){
    object.song = new Audio(object.audio);
}

And use object.song instead of the Song variable 并使用object.song代替Song变量

You can user the object.song.paused(boolean) to know if the song is paused or not and launch the song again. 您可以使用object.song.paused(boolean)来了解歌曲是否已暂停,然后再次启动歌曲。

You should have 2 separate functions. 您应该具有2个独立的功能。 One for creating the audio element, and one for playing/pausing a given audio element. 一种用于创建音频元素,另一种用于播放/暂停给定的音频元素。

One of your problems is that every time you're calling playSong() , you're creating a whole new audio element. 您的问题之一是,每次调用playSong() ,您都在创建一个全新的音频元素。

Solution

var drake = {
    value: 3,
    name: 'Energy',
    artist: 'Drake',
    audio: 'energy.mp3', //
    img: '<img style="width: 50%; margin-right: 25%; margin-left: 25%; margin-top: 10%;" src="http://www.getrichrapping.com/wp-content/uploads/2015/08/Drake-Energy.jpg">'
};

var audioElement = playSong(drake); // load song and play it

togglePlayPause(audioElement); // pause song

function playSong(object) {
    var Song = new Audio(object.audio);
    Song.play();

    return Song;
}

function togglePlayPause(audioElement) {
    if (audioElement.paused) {
        audioElement.play();
    } else {
        audioElement.pause();
    }
}

You first create the audio element and store it in a variable, using the playSong() function. 首先,使用playSong()函数创建音频元素并将其存储在变量中。 Then you pass the audio element in to togglePlayPause() to toggle its playing status. 然后,将音频元素传递到togglePlayPause()以切换其播放状态。

In the example, I'm playing the song and then immediately calling togglePlayPause() to pause it again. 在示例中,我正在播放歌曲,然后立即调用togglePlayPause()再次将其暂停。

Also, you don't need to keep the playing status in a separate variable. 另外,您无需将播放状态保持在单独的变量中。 I've just used the .paused property on the audio element, which returns true if the player has been paused. 我刚刚在音频元素上使用了.paused属性,如果播放器已暂停,该属性将返回true。 If you really needed a separate variable, you should store it within the drake object as a new property .isPlaying . 如果确实需要单独的变量,则应将其作为新属性.isPlaying存储在drake对象中。

https://jsfiddle.net/5k38da10/ https://jsfiddle.net/5k38da10/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM