Skip to content

Commit

Permalink
optimize wake lock & wifi lock
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfeng committed May 19, 2022
1 parent 8d6b3b7 commit 4a6fdba
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package snow.player.exo.api16;

import static com.google.android.exoplayer2.C.WAKE_MODE_NETWORK;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
Expand Down Expand Up @@ -194,6 +196,7 @@ private int toErrorCode(PlaybackException error) {

private void initExoPlayer(Context context) {
mExoPlayer = new ExoPlayer.Builder(context)
.setWakeMode(WAKE_MODE_NETWORK)
.setLooper(Looper.getMainLooper())
.build();

Expand Down
3 changes: 3 additions & 0 deletions exo/src/main/java/snow/player/exo/ExoMusicPlayer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package snow.player.exo;

import static com.google.android.exoplayer2.C.WAKE_MODE_NETWORK;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
Expand Down Expand Up @@ -205,6 +207,7 @@ private int toErrorCode(PlaybackException error) {

private void initExoPlayer(Context context, @Nullable MediaSource.Factory mediaSourceFactory) {
ExoPlayer.Builder builder = new ExoPlayer.Builder(context)
.setWakeMode(WAKE_MODE_NETWORK)
.setLooper(Looper.getMainLooper());

if (mediaSourceFactory != null) {
Expand Down
1 change: 1 addition & 0 deletions player/src/main/java/snow/player/AbstractPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,7 @@ private void notifyPrepared(int audioSessionId, int duration) {

private void notifyPlaying(boolean stalled, int progress, long updateTime) {
mPlayerStateHelper.onPlay(stalled, progress, updateTime);
requireWakeLock();

if (!stalled) {
mMediaSession.setPlaybackState(buildPlaybackState(PlaybackStateCompat.STATE_PLAYING));
Expand Down
3 changes: 3 additions & 0 deletions player/src/main/java/snow/player/audio/MediaMusicPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.media.PlaybackParams;
import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.util.Log;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -71,6 +72,8 @@ public MediaMusicPlayer(@NonNull Context context, @NonNull Uri uri, @Nullable Ma
mMediaPlayer = new MediaPlayer();
mInvalid = false;

mMediaPlayer.setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK);

mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Expand Down

0 comments on commit 4a6fdba

Please sign in to comment.