Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,21 @@ abstract class BaseAudioPlayer internal constructor(
exoPlayer.pauseAtEndOfMediaItems = pause
}

/**
* Abandons audio focus so other apps can restore their volume.
* Uses ExoPlayer's setAudioAttributes toggle when handleAudioFocus is true,
* otherwise uses the custom FocusManager.
*/
fun abandonAudioFocus() {
if (options.handleAudioFocus) {
val attrs = exoPlayer.audioAttributes
exoPlayer.setAudioAttributes(attrs, false)
exoPlayer.setAudioAttributes(attrs, true)
} else {
focusManager.abandonAudioFocusIfHeld()
}
}

/**
* Stops and destroys the player. Only call this when you are finished using the player, otherwise use [pause].
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,12 @@ class MusicModule(reactContext: ReactApplicationContext) : NativeTrackPlayerSpec
callback.resolve(null)
}

override fun abandonAudioFocus(callback: Promise) = launchInScope {
if (verifyServiceBoundOrReject(callback)) return@launchInScope
musicService.abandonAudioFocus()
callback.resolve(null)
}

override fun validateOnStartCommandIntent(callback: Promise) = launchInScope {
if (verifyServiceBoundOrReject(callback)) return@launchInScope
callback.resolve(musicService.onStartCommandIntentValid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ class MusicService : HeadlessJsMediaService() {
sWakeLock?.release()
}

@MainThread
fun abandonAudioFocus() {
player.abandonAudioFocus()
}

fun getBitmapLoader(): BitmapLoader {
return mediaSession.bitmapLoader
}
Expand Down
3 changes: 3 additions & 0 deletions ios/TrackPlayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ - (void)validateOnStartCommandIntent:(nonnull RCTPromiseResolveBlock)resolve rej
- (void)abandonWakeLock:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
resolve(nil);
}
- (void)abandonAudioFocus:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
resolve(nil);
}
- (void)acquireWakeLock:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
resolve(nil);
}
Expand Down
1 change: 1 addition & 0 deletions src/NativeTrackPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export interface Spec extends TurboModule {
// android methods
acquireWakeLock(): Promise<void>;
abandonWakeLock(): Promise<void>;
abandonAudioFocus(): Promise<void>;
validateOnStartCommandIntent(): Promise<boolean>;
}

Expand Down
8 changes: 8 additions & 0 deletions src/trackPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,14 @@ export async function abandonWakeLock() {
TrackPlayer.abandonWakeLock();
}

/**
* Abandons audio focus so other apps can restore their volume (android only.)
*/
export async function abandonAudioFocus(): Promise<void> {
if (!isAndroid) return;
return TrackPlayer.abandonAudioFocus();
}

/**
* get onStartCommandIntent is null or not (Android only.). this is used to identify
* if musicservice is restarted or not.
Expand Down
Loading