Skip to content

Commit

Permalink
Merge pull request #250 from THEOplayer/feature/toggle_wake_lock
Browse files Browse the repository at this point in the history
Feature/toggle wake lock
  • Loading branch information
tvanlaerhoven authored Jan 30, 2024
2 parents a6d64fb + 2536473 commit d7bcbf8
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.1.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Added the ability to toggle `keepScreenOn` on Android. By default, screen timeout is disabled while the player is visible.

## [3.4.2] - 23-12-22

### Fixed
Expand Down
6 changes: 0 additions & 6 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
-->
<uses-permission android:name="android.permission.INTERNET" />

<!--
Allows using PowerManager WakeLocks to keep processor from sleeping or screen from dimming.
Protection level: normal
-->
<uses-permission android:name="android.permission.WAKE_LOCK" />

<!--
Allows a regular application to use Service.startForeground.
Protection level: normal
Expand Down
7 changes: 7 additions & 0 deletions android/src/main/java/com/theoplayer/player/PlayerModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ class PlayerModule(context: ReactApplicationContext) : ReactContextBaseJavaModul
}
}

@ReactMethod
fun setKeepScreenOn(tag: Int, value: Boolean) {
viewResolver.resolveViewByTag(tag) { view: ReactTHEOplayerView? ->
view?.playerContext?.playerView?.keepScreenOn = value
}
}

@ReactMethod
fun setTextTrackStyle(tag: Int, style: ReadableMap?) {
viewResolver.resolveViewByTag(tag) { view: ReactTHEOplayerView? ->
Expand Down
9 changes: 9 additions & 0 deletions src/api/player/THEOplayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ export interface THEOplayer extends EventDispatcher<PlayerEventMap> {
*/
aspectRatio: AspectRatio;

/**
* Toggle the wake-lock on the player view. The screen will time out if disabled.
*
* @defaultValue `true`
* @remarks
* Only available on Android.
*/
keepScreenOn: boolean;

/**
* The active configuration for PiP.
*/
Expand Down
5 changes: 2 additions & 3 deletions src/internal/adapter/NativePlayerState.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { MediaTrack, PreloadType, PresentationMode, SourceDescription, TextTrack, TimeRange } from 'react-native-theoplayer';
import type { BackgroundAudioConfiguration } from '../../api/backgroundAudio/BackgroundAudioConfiguration';
import type { PiPConfiguration } from 'react-native-theoplayer';
import type { AspectRatio } from 'react-native-theoplayer';
import type { PiPConfiguration, AspectRatio, BackgroundAudioConfiguration } from 'react-native-theoplayer';

export interface NativePlayerState {
source: SourceDescription | undefined;
Expand All @@ -20,6 +18,7 @@ export interface NativePlayerState {
playbackRate: number;
preload: PreloadType;
aspectRatio: AspectRatio;
keepScreenOn: boolean;
audioTracks: MediaTrack[];
videoTracks: MediaTrack[];
textTracks: TextTrack[];
Expand Down
12 changes: 12 additions & 0 deletions src/internal/adapter/THEOplayerAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const defaultPlayerState: NativePlayerState = {
playbackRate: 1,
preload: 'none',
aspectRatio: AspectRatio.FIT,
keepScreenOn: true,
audioTracks: [],
videoTracks: [],
textTracks: [],
Expand Down Expand Up @@ -502,6 +503,17 @@ export class THEOplayerAdapter extends DefaultEventDispatcher<PlayerEventMap> im
NativeModules.PlayerModule.setAspectRatio(this._view.nativeHandle, ratio);
}

get keepScreenOn(): boolean {
return this._state.keepScreenOn;
}

set keepScreenOn(value: boolean) {
this._state.keepScreenOn = value;
if (Platform.OS === 'android') {
NativeModules.PlayerModule.setKeepScreenOn(this._view.nativeHandle, value);
}
}

pause(): void {
if (this.hasValidSource()) {
this._state.paused = true;
Expand Down
8 changes: 8 additions & 0 deletions src/internal/adapter/THEOplayerWebAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,14 @@ export class THEOplayerWebAdapter extends DefaultEventDispatcher<PlayerEventMap>
// unused
}

get keepScreenOn(): boolean {
return false;
}

set keepScreenOn(_screenOn: boolean) {
// unused
}

get duration(): number {
return this._player ? this._player.duration * 1e3 : NaN;
}
Expand Down

0 comments on commit d7bcbf8

Please sign in to comment.