Share via


如何在音訊/視訊播放期間保持顯示 (HTML)

[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]

了解如何使用 DisplayRequest 類別,在播放本機或串流視訊時保持顯示。 執行 Windows 執行階段應用程式的裝置通常會在使用者離開時讓顯示器變暗 (最後會將它關閉) 以延長電池壽命,但是視訊應用程式需要讓螢幕一直開著才能讓使用者觀看視訊。DisplayRequest 類別讓您告訴 Windows 保持開啟顯示器,讓使用者可以觀看視訊。

您必須知道的事

技術

先決條件

指示

步驟 1: 建立 DisplayRequest 物件

建立全域 DisplayRequest 變數。將變數初始化為 null。


// Create this variable at a global scope.
var dispRequest = null;

步驟 2: 在開始播放視訊之前啟用 DisplayRequest

呼叫 requestActive 通知 Windows 應用程式需要讓顯示器保持開啟。

下列範例會在開始播放視訊之前先啟用要求:

function startVideoPlayback() {
    
    if (dispRequest === null) {
        
        // Activate a display-required request. If successful, the screen is 
        // guaranteed not to turn off automatically due to user inactivity.
        dispRequest = new Windows.System.Display.DisplayRequest;
        dispRequest.requestActive();
        
        WinJS.log && WinJS.log("Display request activated", 
                               "sample", 
                               "status");
        
        
        // Insert your own code here to start the video.
        
    }
}

步驟 3: 視訊播放停止或暫停時停用 DisplayRequest

只要視訊播放停止、暫停或因為播放錯誤而中斷時,呼叫 requestRelease 以釋放顯示要求。當您的應用程式不再有任何啟用的顯示要求時,Windows 會在沒有使用裝置時將顯示器畫面變暗 (最後將它關閉) 以延長電池續航力。

下列範例會停用要求:

function stopVideoPlayback() {
    
    // Insert your own code here to stop the video.
    
    if (dispRequest != null) {
        
        // Deactivate the display request and set the var to null.
        dispRequest.requestRelease();
        dispRequest = null;
        
        WinJS.log && WinJS.log("Display request released", 
                               "sample",
                               "status");
        
    }
}

備註

注意  當您的應用程式沒有在螢幕上顯示的時候,Windows 會自動停用它的啟用顯示要求,並在應用程式回到前景時再重新啟用顯示要求。

 

您也可以使用這個 API,在指示 GPS 應用程式時讓螢幕保持開啟。在這個情況中,使用瀏覽事件取代視訊播放事件。

若要根據功能上下文查看類似程式碼,請下載顯示器電源狀態範例

相關主題

快速入門:在 Windows 市集應用程式中播放視訊