Skip to content

Commit

Permalink
update sample codes
Browse files Browse the repository at this point in the history
  • Loading branch information
KunMinX committed Aug 12, 2022
1 parent 356d3eb commit 6e2d1e9
Show file tree
Hide file tree
Showing 19 changed files with 154 additions and 220 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Jetpack-MusicPlayer 目标是:**一行代码即可接入 音乐播放控制组
1.在 build.gradle 中添加依赖。

```groovy
implementation 'com.kunminx.player:player:3.7.0'
implementation 'com.kunminx.player:player:4.0.0'
```

提示:鉴于 Jcenter 关闭,我们已将仓库迁移至 Maven Central,请自行在根目录 build.gradle 添加 `mavenCentral()`
Expand Down
2 changes: 1 addition & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ If you are thinking about [**how to choose the right architecture for your proje
1. Add a dependency on the library in build.gradle.

```groovy
implementation 'com.kunminx.player:player:3.7.0'
implementation 'com.kunminx.player:player:4.0.0'
```

Tip: 鉴于 Jcenter 的关闭,我们已将仓库迁移至 Maven Central,请自行在根目录 build.gradle 添加 `mavenCentral()`
Expand Down
28 changes: 6 additions & 22 deletions app/src/main/java/com/kunminx/puremusic/player/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,17 @@
import android.content.Context;
import android.content.Intent;

import androidx.lifecycle.LiveData;

import com.danikula.videocache.HttpProxyCacheServer;
import com.kunminx.player.PlayerController;
import com.kunminx.player.PlayingInfoManager;
import com.kunminx.player.bean.dto.ChangeMusic;
import com.kunminx.player.bean.dto.PlayingMusic;
import com.kunminx.player.contract.ICacheProxy;
import com.kunminx.player.contract.IPlayController;
import com.kunminx.player.contract.IServiceNotifier;
import com.kunminx.player.domain.PlayerInfoDispatcher;
import com.kunminx.puremusic.data.bean.TestAlbum;
import com.kunminx.puremusic.player.helper.PlayerFileNameGenerator;
import com.kunminx.puremusic.player.notification.PlayerService;

import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -163,6 +159,11 @@ public String getTrackTime(int progress) {
return mController.getTrackTime(progress);
}

@Override
public PlayerInfoDispatcher getDispatcher() {
return mController.getDispatcher();
}

@Override
public TestAlbum getAlbum() {
return mController.getAlbum();
Expand All @@ -183,23 +184,6 @@ public int getAlbumIndex() {
return mController.getAlbumIndex();
}

public LiveData<ChangeMusic<TestAlbum, TestAlbum.TestMusic, TestAlbum.TestArtist>> getChangeMusicResult() {
return mController.getChangeMusicResult();
}

public LiveData<PlayingMusic<TestAlbum, TestAlbum.TestMusic, TestAlbum.TestArtist>> getPlayingMusicResult() {
return mController.getPlayingMusicResult();
}

public LiveData<Boolean> getPauseResult() {
return mController.getPauseResult();
}

@Override
public LiveData<Enum<PlayingInfoManager.RepeatMode>> getPlayModeResult() {
return mController.getPlayModeResult();
}

@Override
public Enum<PlayingInfoManager.RepeatMode> getRepeatMode() {
return mController.getRepeatMode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.kunminx.architecture.ui.page.BaseFragment;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.State;
import com.kunminx.player.domain.PlayerEvent;
import com.kunminx.puremusic.BR;
import com.kunminx.puremusic.R;
import com.kunminx.puremusic.data.bean.TestAlbum;
Expand Down Expand Up @@ -73,8 +74,10 @@ protected DataBindingConfig getDataBindingConfig() {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

PlayerManager.getInstance().getChangeMusicResult().observe(getViewLifecycleOwner(), changeMusic -> {
mAdapter.notifyDataSetChanged();
PlayerManager.getInstance().getDispatcher().output(this, playerEvent -> {
if (playerEvent.eventId == PlayerEvent.EVENT_CHANGE_MUSIC) {
mAdapter.notifyDataSetChanged();
}
});

mMusicRequester.getFreeMusicsResult().observe(getViewLifecycleOwner(), dataResult -> {
Expand Down
62 changes: 26 additions & 36 deletions app/src/main/java/com/kunminx/puremusic/ui/page/PlayerFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.kunminx.architecture.utils.ToastUtils;
import com.kunminx.architecture.utils.Utils;
import com.kunminx.player.PlayingInfoManager;
import com.kunminx.player.domain.PlayerEvent;
import com.kunminx.puremusic.BR;
import com.kunminx.puremusic.R;
import com.kunminx.puremusic.databinding.FragmentPlayerBinding;
Expand Down Expand Up @@ -88,42 +89,31 @@ public void onPanelStateChanged(
}
});

PlayerManager.getInstance().getChangeMusicResult().observe(getViewLifecycleOwner(), changeMusic -> {
mStates.title.set(changeMusic.getTitle());
mStates.artist.set(changeMusic.getSummary());
mStates.coverImg.set(changeMusic.getImg());

if (mListener != null) {
view.post(mListener::calculateTitleAndArtist);
}
});

PlayerManager.getInstance().getPlayingMusicResult().observe(getViewLifecycleOwner(), playingMusic -> {
mStates.maxSeekDuration.set(playingMusic.getDuration());
mStates.currentSeekPosition.set(playingMusic.getPlayerPosition());
});

PlayerManager.getInstance().getPauseResult().observe(getViewLifecycleOwner(), aBoolean -> {
mStates.isPlaying.set(!aBoolean);
});

PlayerManager.getInstance().getPlayModeResult().observe(getViewLifecycleOwner(), anEnum -> {
int tip;
if (anEnum == PlayingInfoManager.RepeatMode.LIST_CYCLE) {
mStates.playModeIcon.set(MaterialDrawableBuilder.IconValue.REPEAT);
tip = R.string.play_repeat;
} else if (anEnum == PlayingInfoManager.RepeatMode.SINGLE_CYCLE) {
mStates.playModeIcon.set(MaterialDrawableBuilder.IconValue.REPEAT_ONCE);
tip = R.string.play_repeat_once;
} else {
mStates.playModeIcon.set(MaterialDrawableBuilder.IconValue.SHUFFLE);
tip = R.string.play_shuffle;
}
if (view.getParent().getParent() instanceof SlidingUpPanelLayout) {
SlidingUpPanelLayout sliding = (SlidingUpPanelLayout) view.getParent().getParent();
if (sliding.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED) {
ToastUtils.showShortToast(getApplicationContext(), getString(tip));
}
PlayerManager.getInstance().getDispatcher().output(this, playerEvent -> {
switch (playerEvent.eventId) {
case PlayerEvent.EVENT_CHANGE_MUSIC:
mStates.title.set(playerEvent.param.changeMusic.getTitle());
mStates.artist.set(playerEvent.param.changeMusic.getSummary());
mStates.coverImg.set(playerEvent.param.changeMusic.getImg());
if (mListener != null) view.post(mListener::calculateTitleAndArtist);
break;
case PlayerEvent.EVENT_PROGRESS:
mStates.maxSeekDuration.set(playerEvent.param.playingMusic.getDuration());
mStates.currentSeekPosition.set(playerEvent.param.playingMusic.getPlayerPosition());
break;
case PlayerEvent.EVENT_PLAY_STATUS:
mStates.isPlaying.set(!playerEvent.param.toPause);
break;
case PlayerEvent.EVENT_REPEAT_MODE:
Enum<PlayingInfoManager.RepeatMode> anEnum = playerEvent.param.repeatMode;
if (anEnum == PlayingInfoManager.RepeatMode.LIST_CYCLE) {
mStates.playModeIcon.set(MaterialDrawableBuilder.IconValue.REPEAT);
} else if (anEnum == PlayingInfoManager.RepeatMode.SINGLE_CYCLE) {
mStates.playModeIcon.set(MaterialDrawableBuilder.IconValue.REPEAT_ONCE);
} else {
mStates.playModeIcon.set(MaterialDrawableBuilder.IconValue.SHUFFLE);
}
break;
}
});

Expand Down
8 changes: 5 additions & 3 deletions architecture/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ dependencies {
androidTestImplementation "androidx.test.ext:junit:1.1.3"
androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"

api "androidx.appcompat:appcompat:1.4.2"
api "androidx.navigation:navigation-runtime:2.5.0"

api "androidx.appcompat:appcompat:1.5.0"
api "androidx.navigation:navigation-runtime:2.5.1"
api('com.github.KunMinX:MVI-Dispatcher:6.1.0-beta') {
exclude group: 'com.kunminx.arch', module: 'unpeek-livedata'
}
api 'com.github.KunMinX:UnPeek-LiveData:v7.6.0'
api 'com.github.KunMinX:Smooth-Navigation:v4.0.0'
api 'com.github.KunMinX.Strict-DataBinding:strict_databinding:5.3.0'
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ apply plugin: 'io.codearte.nexus-staging'

buildscript {
ext {
appTargetSdk = 31
appTargetSdk = 32
appMinSdk = 23
appVersionCode = 30700
appVersionName = "3.7.0"
appVersionCode = 40000
appVersionName = "4.0.0"
lifecycleVersion = "2.4.1"
navigationVersion = "2.4.2"
}
Expand Down
2 changes: 1 addition & 1 deletion player/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation "androidx.appcompat:appcompat:1.5.0"
api('com.github.KunMinX:MVI-Dispatcher:6.1.0-beta') {
exclude group: 'com.kunminx.arch', module: 'unpeek-livedata'
}
implementation 'com.google.android.exoplayer:exoplayer:2.18.1'
implementation "androidx.lifecycle:lifecycle-livedata:2.4.1"
}
32 changes: 6 additions & 26 deletions player/src/main/java/com/kunminx/player/DefaultPlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@
import android.annotation.SuppressLint;
import android.content.Context;

import androidx.lifecycle.LiveData;

import com.kunminx.player.bean.DefaultAlbum;
import com.kunminx.player.bean.dto.ChangeMusic;
import com.kunminx.player.bean.dto.PlayingMusic;
import com.kunminx.player.contract.ICacheProxy;
import com.kunminx.player.contract.IPlayController;
import com.kunminx.player.contract.IServiceNotifier;
import com.kunminx.player.domain.PlayerInfoDispatcher;

import java.util.List;

Expand Down Expand Up @@ -138,6 +135,11 @@ public String getTrackTime(int progress) {
return mController.getTrackTime(progress);
}

@Override
public PlayerInfoDispatcher getDispatcher() {
return mController.getDispatcher();
}

@Override
public DefaultAlbum getAlbum() {
return mController.getAlbum();
Expand All @@ -158,28 +160,6 @@ public int getAlbumIndex() {
return mController.getAlbumIndex();
}

public LiveData<ChangeMusic<DefaultAlbum,
DefaultAlbum.DefaultMusic,
DefaultAlbum.DefaultArtist>> getChangeMusicResult() {
return mController.getChangeMusicResult();
}

public LiveData<PlayingMusic<DefaultAlbum,
DefaultAlbum.DefaultMusic,
DefaultAlbum.DefaultArtist>> getPlayingMusicResult() {
return mController.getPlayingMusicResult();
}

public LiveData<Boolean> getPauseResult() {
return mController.getPauseResult();
}


@Override
public LiveData<Enum<PlayingInfoManager.RepeatMode>> getPlayModeResult() {
return mController.getPlayModeResult();
}

@Override
public Enum<PlayingInfoManager.RepeatMode> getRepeatMode() {
return mController.getRepeatMode();
Expand Down
Loading

0 comments on commit 6e2d1e9

Please sign in to comment.