Freigeben über


How to keep the display on during audio/video playback (XAML)

[ This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation ]

Learn how to keep the display on while playing back local or streaming video by using the DisplayRequest class. Normally, a device running a Windows Runtime app will dim the display (and eventually turn it off) to save battery life when the user is away, but video apps need to keep the screen on so the user can see the video. The DisplayRequest class lets you tell Windows to keep the display turned on so the user can see the video.

To see this feature in action as part of a complete media-playback sample, see Media playback, start to finish.

What you need to know

Technologies

Prerequisites

Instructions

Step 1: Create a DisplayRequest object

Create a global DisplayRequest variable. Initialize it to null.

// Create this variable at a global scope. Set it to null.
private DisplayRequest dispRequest = null;
' Create this variable at a global scope. Set it to nothing.
Private dispRequest As DisplayRequest
dispRequest = Nothing

Step 2: Activate the DisplayRequest just before starting video playback

Call requestActive to notify Windows that the app requires the display to remain on.

The following example activates the request before starting video playback:

public void 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 DisplayRequest();
        dispRequest.RequestActive();
        
        rootPage.NotifyUser("Display request activated",
                        NotifyType.StatusMessage);
        
        
        // Insert your own code here to start the video.
        
    }
}
Public Sub StartVideoPlayback()
    If dispRequest Is Nothing Then
        
        ' Activate a display-required request. If successful, the screen is 
        ' guaranteed not to turn off automatically due to user inactivity.
        dispRequest = New DisplayRequest()
        dispRequest.RequestActive()
        
        rootPage.NotifyUser("Display request activated", 
                            NotifyType.StatusMessage)
        
        
        ' Insert your own code here to start the video.
        
    End If
End Sub

Step 3: Deactivate the DisplayRequest when video playback stops or is paused

Call requestRelease to release the display request whenever video playback is stopped, paused, or interrupted by a playback error. When your app no longer has any active display requests, Windows will save battery life by dimming the display (and eventually turn it off) when the device is not being used.

The following example deactivates the request:

public void 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;
        
        rootPage.NotifyUser("Display request released",
                            NotifyType.StatusMessage);
        
    }
}
Public Sub StopVideoPlayback()
    
    ' Insert your own code here to stop the video.
    
    If dispRequest IsNot Nothing Then
        
        ' Deactivate the display request and set the var to nothing.
        dispRequest.RequestRelease()
        dispRequest = Nothing
        
        rootPage.NotifyUser("Display request released", 
                            NotifyType.StatusMessage)
        
    End If
End Sub

Remarks

Note  Windows automatically deactivates your app's active display requests when it is moved off screen, and re-activates them when your app comes back to the foreground.

 

You can also use this API to keep the screen on while giving directions in a GPS app. In this case, substitute navigation events for video playback events.

To see similar code in the context of functions, download the display power state sample.

Quickstart: video and audio

Display power state sample

Media playback, start to finish