google-chrome-extension - 我无法在Chrome扩展程序上播放youtube chromeless播放器

标签 google-chrome-extension youtube youtube-api youtube-javascript-api

我是Chrome扩展程序的新手。我正在制作可播放youtube chromeless播放器的chrome扩展程序。

它适用于chrome浏览器。但是,它不适用于chrome扩展程序。

我测试了本地.swf文件。那是在chrome扩展上工作的。

我认为,chrome扩展程序不能调用onYouTubePlayerReady()

所以我在window.onYouTubePlayerReady()之后叫swfobject.embedSWF()。但是,它不能在ytplayer.loadVideoById("xa8TBfPw3u0", 0);上显示错误消息。

错误消息是Uncaught TypeError: Object #<HTMLObjectElement> has no method 'loadVideoById'
manifest.json有问题吗?还是在YouTube API中?我不知道为什么无法在chrome扩展程序上工作。

popup.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>YouTube Play</title>
    </head>
    <body>
    <table width="1000" height="390">
        <tr>
            <td>
            <div id="videoDiv">
                Loading...
            </div></td>
            <td valign="top">
            <div id="videoInfo">
                <p>
                    Player state: <span id="playerState">--</span>
                </p>
                <p>
                    Current Time: <span id="videoCurrentTime">--:--</span> | Duration: <span id="videoDuration">--:--</span>
                </p>
                <p>
                    Bytes Total: <span id="bytesTotal">--</span> | Start Bytes: <span id="startBytes">--</span> | Bytes Loaded: <span id="bytesLoaded">--</span>
                </p>
                <p>
                    Controls: <input type="button" id="play" value="Play" />
                    <input type="button" id="pause" value="Pause" />
                    <input type="button" id="mute" value="Mute" />
                    <input type="button" id="unmute" value="Unmute" />
                </p>
                <p>
                    <input id="volumeSetting" type="text" size="3" />
                    &nbsp;<input type="button" id="setVolume" value="Set Volume" /> | Volume: <span id="volume">--</span>
                </p>
            </div></td>
        </tr>
    </table>

    <script type="text/javascript" src="jsapi.js"></script>
    <script type="text/javascript" src="my_script.js"></script>
    <script type="text/javascript" src="swfobject.js"></script>
    </body>
</html>

manifest.json
{
    "manifest_version": 2,
    "name": "YouTube Player",
    "description": "This extension is YouTube Player",
    "version": "1.0",
    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },
    "permissions": ["tabs", "http://*/*", "https://*/*", "background"],
    "content_scripts": [
        {
            "matches": ["http://www.youtube.com/*"],
            "js": ["my_script.js", "swfobject.js", "jsapi.js"]
        }
    ]
}

my_script.js
/*
* Chromeless player has no controls.
*/

// Update a particular HTML element with a new value
function updateHTML(elmId, value) {
    document.getElementById(elmId).innerHTML = value;
}

// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
    alert("An error occured of type:" + errorCode);
}

// This function is called when the player changes state
function onPlayerStateChange(newState) {
    updateHTML("playerState", newState);
}

// Display information about the current state of the player
function updatePlayerInfo() {
    // Also check that at least one function exists since when IE unloads the
    // page, it will destroy the SWF before clearing the interval.
    if (ytplayer && ytplayer.getDuration) {
        updateHTML("videoDuration", ytplayer.getDuration());
        updateHTML("videoCurrentTime", ytplayer.getCurrentTime());
        updateHTML("bytesTotal", ytplayer.getVideoBytesTotal());
        updateHTML("startBytes", ytplayer.getVideoStartBytes());
        updateHTML("bytesLoaded", ytplayer.getVideoBytesLoaded());
        updateHTML("volume", ytplayer.getVolume());
    }
}

// Allow the user to set the volume from 0-100
function setVideoVolume() {
    var volume = parseInt(document.getElementById("volumeSetting").value);
    if (isNaN(volume) || volume < 0 || volume > 100) {
        alert("Please enter a valid volume between 0 and 100.");
    } else if (ytplayer) {
        ytplayer.setVolume(volume);
    }
}

function playVideo() {
    if (ytplayer) {
        ytplayer.playVideo();
    }
}

function pauseVideo() {
    if (ytplayer) {
        ytplayer.pauseVideo();
    }
}

function muteVideo() {
    if (ytplayer) {
        ytplayer.mute();
    }
}

function unMuteVideo() {
    if (ytplayer) {
        ytplayer.unMute();
    }
}

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("ytPlayer");
    // This causes the updatePlayerInfo function to be called every 250ms to
    // get fresh data from the player
    setInterval(updatePlayerInfo, 250);
    updatePlayerInfo();
    ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
    ytplayer.addEventListener("onError", "onPlayerError");
    //Load an initial video into the player
    ytplayer.loadVideoById("xa8TBfPw3u0", 0);
}

// The "main method" of this sample. Called when someone clicks "Run".
function loadPlayer() {
    // Lets Flash from another domain call JavaScript
    var params = {
        allowScriptAccess : "always"
    };
    // The element id of the Flash embed
    var atts = {
        id : "ytPlayer"
    };
    // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
    swfobject.embedSWF("http://www.youtube.com/apiplayer?" + "&enablejsapi=1&playerapiid=ytplayer", "videoDiv", "640", "390", "8", null, null, params, atts);

    //window.onYouTubePlayerReady();

    document.getElementById("play").onclick = playVideo;
    document.getElementById("pause").onclick = pauseVideo;
    document.getElementById("mute").onclick = muteVideo;
    document.getElementById("unmute").onclick = unMuteVideo;
    document.getElementById("setVolume").onclick = setVideoVolume;
}

function _run() {
    loadPlayer();
}

google.setOnLoadCallback(_run);

请帮我。

最佳答案

最近,在使用Chrome扩展程序时遇到了相同的问题。测试时,根本不会触发 call 。直到我阅读以下内容:

To test any of these calls, you must have your file running on a webserver, as the Flash player restricts calls between local files and the internet.



From YouTube's Player API Documentation

关于google-chrome-extension - 我无法在Chrome扩展程序上播放youtube chromeless播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19456064/

相关文章:

c# - YouTube V3 API - Google.Apis.Requests.RequestErrorBackend 错误 [503]

c# - 未找到来自 google api 的 UserCredential 类

google-chrome-extension - Chrome 扩展 : How to get key events

javascript - chrome.tabs.sendMessage 不会将 tabId 传递给发件人

YouTube 直播 RTMP 网址

youtube - Youtube Player API-选择了“仅播放浏览器”标签

youtube - 如何在使用video.js播放时获取youtube元数据7

Android-Youtube Player Api - 切换视频?

Javascript/jQuery 浏览器检测

javascript - 探测扩展时出现 GET net::ERR_FAILED 错误