Skip to content

Commit

Permalink
fix: Adjust Android focus and content type settings
Browse files Browse the repository at this point in the history
  • Loading branch information
wsamoht committed Dec 3, 2024
1 parent c958657 commit 7cf66e2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import android.os.Looper;
import android.util.Log;

import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.session.MediaController;
import androidx.media3.session.SessionCommand;
import androidx.media3.session.SessionResult;
Expand Down Expand Up @@ -102,11 +101,7 @@ public void initialize(PluginCall call) {

if (audioSource.useForNotification) {
audioSource.setPlayer(audioMediaController);

audioMediaController.setMediaItem(audioSource.buildMediaItem());
audioMediaController.setRepeatMode(audioSource.loopAudio ? ExoPlayer.REPEAT_MODE_ONE : ExoPlayer.REPEAT_MODE_OFF);
audioMediaController.setPlayWhenReady(false);
audioMediaController.addListener(new PlayerEventListener(this, audioSource));
audioSource.setPlayerAttributes();

audioMediaController.prepare();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import androidx.media3.common.MediaItem;
import androidx.media3.common.MediaMetadata;
import androidx.media3.common.AudioAttributes;
import androidx.media3.session.MediaController;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
Expand Down Expand Up @@ -66,23 +65,27 @@ public void initialize(Context context) {

setIsStopped();

player =
new ExoPlayer.Builder(context)
.setAudioAttributes(
new AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
.setContentType(C.AUDIO_CONTENT_TYPE_SPEECH)
.build(),
false
)
.setWakeMode(C.WAKE_MODE_NETWORK)
.build();
player = new ExoPlayer.Builder(context)
.setWakeMode(C.WAKE_MODE_NETWORK)
.build();
setPlayerAttributes();

player.prepare();
}

public void setPlayerAttributes() {
player.setAudioAttributes(
new AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
.setContentType(useForNotification ? C.AUDIO_CONTENT_TYPE_SPEECH : C.AUDIO_CONTENT_TYPE_MUSIC)
.build(),
useForNotification
);

player.setMediaItem(buildMediaItem());
player.setRepeatMode(loopAudio ? ExoPlayer.REPEAT_MODE_ONE : ExoPlayer.REPEAT_MODE_OFF);
player.setPlayWhenReady(false);
player.addListener(new PlayerEventListener(pluginOwner, this));

player.prepare();
}

public void changeAudioSource(String newSource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

import static androidx.media3.common.Player.*;

import android.util.Log;

import com.getcapacitor.JSObject;
import com.getcapacitor.PluginCall;

import androidx.media3.common.Player;

public class PlayerEventListener implements Listener {

private static final String TAG = "PlayerEventListener";
Expand Down
4 changes: 2 additions & 2 deletions example/src/js/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function initialize(): Promise<void> {
isBackgroundMusic: false,
loop: false,
showSeekForward: true,
showSeekBackward: true
showSeekBackward: true,
}).catch(ex => setError(ex));

await AudioPlayer.create({
Expand All @@ -36,7 +36,7 @@ async function initialize(): Promise<void> {
friendlyTitle: '',
useForNotification: false,
isBackgroundMusic: true,
loop: true
loop: true,
}).catch(ex => setError(ex));

await AudioPlayer.onAudioReady({ audioId: audioId }, async () => {
Expand Down

0 comments on commit 7cf66e2

Please sign in to comment.