5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【JavaScript】動画倍速機能の実装

Last updated at Posted at 2019-05-21

Wordpressのプラグインで、動画倍速ってないんですね。
これを元に開発しようかな。

動画(音声)を倍速にする機能(ボタンクリック)

index.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="main.js"></script>
</head>
<body>
    <output id="res">
        <video src='/Users/matsumurashun/Downloads/pu-tyan.mp4' width='500' height='300' controls loop></video>
        <form>
            <input type="button" value="1倍速">
            <input type="button" value="2倍速">
            <input type="button" value="3倍速">
        </form>
    </output>    
</body>
</html>
main.js
window.onload = () => {
    const btn = document.getElementsByTagName("input");
    const video = document.getElementsByTagName("video")[0];

    btn[0].addEventListener('click', () => {
        video.playbackRate = 1.0;
        video.defaultPlaybackRate = 1.0;
    }, false);

    btn[1].addEventListener('click', () => {
        video.playbackRate = 2.0;
        video.defaultPlaybackRate = 2.0;
    }, false);

    btn[2].addEventListener('click', () => {
        video.playbackRate = 3.0;
        video.defaultPlaybackRate = 3.0;
    }, false);
}

動画(音声)を倍速にする機能(プルダウン選択)

index.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="main.js"></script>
</head>
<body>
    <output id="res">
        <video src='/Users/matsumurashun/Downloads/pu-tyan.mp4' width='500' height='300' controls loop></video>
        <form>
            <select>
                <option value="1">1倍速</option>
                <option value="2">2倍速</option>
                <option value="3">3倍速</option>
            </select>
        </form>
    </output>    
</body>
</html>
main.js
window.onload = () => {
    const selector = document.getElementsByTagName("select")[0];
    selector.addEventListener('change', (it) => {
        video.playbackRate = setRate(it.target.value);
    });
    const setRate = (val) => {
        return Number(val);
    }
}

playbackRate:再生速度の倍率
Number:Numberオブジェクトで、引数に入れれば文字列を数値に変換してくれる

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?