0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

YouTubeタイマーでモーニングコールとキッチンタイマー

0
Posted at

前回のYouTubeタイマーにプリセット機能拡張してタイマーをつけました。

前回アップしたモーニングコールのプリセットを改造して起動時刻を自動計算してセットするキッチンタイマー機能をつけました。

前回のYoutubeタイマーモーニングコールはこちら
YouTubeタイマーでモーニングコールサービス

モーニングコールの公開サイトはこちら
モーニングコールサービスサイト

残念ながらiPhoneでは動きませんでした。
Androidでは動くのですが、ブラウザが起動してスリープしていない状態になっていないと動きません。
なのでPC用と思ってください。

ソース

index.html
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>YouTube タイマー (リスト選択版)</title>
    <style>
        body { font-family: 'Helvetica Neue', Arial, sans-serif; display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 100vh; background-color: #f8f9fa; margin: 0; color: #333; }
        .container { background: white; padding: 2rem; border-radius: 12px; box-shadow: 0 10px 25px rgba(0,0,0,0.1); text-align: center; width: 90%; max-width: 500px; }
        h2 { margin-top: 0; color: #ff0000; }
        .form-group { margin-bottom: 15px; text-align: left; }
        label { display: block; font-size: 0.9rem; margin-bottom: 5px; color: #666; }
        select, input, button { width: 100%; padding: 12px; margin-bottom: 10px; border: 1px solid #ddd; border-radius: 6px; box-sizing: border-box; font-size: 1rem; }
        button { background-color: #ff0000; color: white; border: none; font-weight: bold; cursor: pointer; transition: background 0.3s; }
        button:hover { background-color: #cc0000; }
        #player { margin-top: 20px; border-radius: 8px; overflow: hidden; }
        .status { font-weight: bold; padding: 10px; border-radius: 4px; background: #fff3f3; color: #d32f2f; }
    </style>
</head>
<body>

<div class="container">
    <h2>Youtubeモーニングコール<br>キッチンタイマー</h2>
    
    <div class="form-group">
        <label>プリセットから選ぶ</label>
        <select id="videoSelect" onchange="updateVideoId()">
            <option value="">-- 選択してください --</option>
        </select>
    </div>

    <div class="form-group">
        <label>動画ID(手入力もOK)</label>
        <input type="text" id="videoId" placeholder="例:dQw4w9WgXcQ">
    </div>

    <div class="form-group">
        <label>再生予約時刻</label>
        <input type="time" id="targetTime" step="1">
    </div>
    
    <button onclick="setTimer()">タイマーをセット</button>
    <p id="message">時刻を設定してセットしてください</p>

    <div id="player"></div>
</div>

<script>
    // --- JSONデータ: ここを書き換えるだけでリストを増やせます ---
    const videoListData = [
        { "name": "通常モーニングコール", "id": "JEgteg1Bwzo", "delaySeconds":0 },
        { "name": "怖い姉バージョン", "id": "VdjDTWSAKqo", "delaySeconds":0 },
        { "name": "優しい姉バージョン", "id": "8t5YVycvQuw", "delaySeconds":0 },
        { "name": "姉を起こすバージョン", "id": "_7ni-mlJ2yk", "delaySeconds":0 },
        { "name": "兄を起こすバージョン", "id": "SK6hG494TwY", "delaySeconds":0 },
        { "name": "一緒におでかけ", "id": "2FBXcFpC41Q", "delaySeconds":0 },
        { "name": "普通にお見送り", "id": "039XT5PUGPA", "delaySeconds":0 },
        { "name": "お見送り遅れ気味", "id": "JEVFTcow2NE", "delaySeconds":0 },
        { "name": "5分経過版", "id": "uTIzTMFR-fs", "delaySeconds":300 },
        { "name": "3分経過版", "id": "RfDwJfZT1x8", "delaySeconds":180 }
    ];

    let player;
    let timerId = null;

    window.onload = () => {
        const select = document.getElementById('videoSelect');
        videoListData.forEach(item => {
            const option = document.createElement('option');
            option.value = item.id;
            option.textContent = item.name;
            select.appendChild(option);
        });
    };

    // リスト選択時に秒数から「未来の時刻」を計算してセット
    function updateVideoId() {
        const select = document.getElementById('videoSelect');
        const selectedId = select.value;
        const selectedData = videoListData.find(item => item.id === selectedId);

        if (selectedData) {
            document.getElementById('videoId').value = selectedData.id;
            
            if (selectedData.delaySeconds > 0) {
                // 現在時刻に秒数を加算
                const now = new Date();
                now.setSeconds(now.getSeconds() + selectedData.delaySeconds);

                // HH:mm:ss 形式に変換
                const targetHms = 
                    String(now.getHours()).padStart(2, '0') + ":" + 
                    String(now.getMinutes()).padStart(2, '0') + ":" + 
                    String(now.getSeconds()).padStart(2, '0');

                document.getElementById('targetTime').value = targetHms;
                setTimer(); // 自動セット実行
            } else {
                // 0 の場合は手動入力
                document.getElementById('targetTime').value = "";
                document.getElementById('message').innerText = "時刻を手動で入力してください";
                document.getElementById('message').className = "";
            }
        }
    }

    // --- 既存のタイマーロジック ---
    function setTimer() {
        const vId = document.getElementById('videoId').value;
        const targetTime = document.getElementById('targetTime').value;

        if (!vId || !targetTime) {
            alert("動画IDと時刻の両方を設定してください");
            return;
        }

        if (timerId) clearInterval(timerId);
        document.getElementById('message').className = "status";
        document.getElementById('message').innerText = `予約完了: ${targetTime} に再生します`;

        timerId = setInterval(() => {
            const now = new Date();
            const currentTime = 
                String(now.getHours()).padStart(2, '0') + ":" + 
                String(now.getMinutes()).padStart(2, '0') + ":" + 
                String(now.getSeconds()).padStart(2, '0');

            if (currentTime === targetTime) {
                clearInterval(timerId);
                playVideo(vId);
            }
        }, 1000);
    }

    // (playVideo関数などは変更なし)
    function playVideo(vId) {
        document.getElementById('message').innerText = "再生中!";
        if (player) player.destroy();
        player = new YT.Player('player', {
            height: '280', width: '100%', videoId: vId,
            playerVars: { 'autoplay': 1, 'mute': 0, 'playsinline': 1 },
            events: { 'onReady': (e) => e.target.playVideo() }
        });
    }
</script>

</body>
</html>

商用利用したい方へ

このソースを商用利用サイトを作りたい方は、m_kawase@embed-ai.comまでご相談くださいませ。
動画IDを自動取得したり、データベース化することで個人カスタマイズ可能なものを作れます。

個人利用の方はコメント欄にサイトURLを教えてください!

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?