Skip to content

Commit 7e8148b

Browse files
author
zhuruqiao
committed
add loop
1 parent 41fff32 commit 7e8148b

File tree

8 files changed

+136
-38
lines changed

8 files changed

+136
-38
lines changed

.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

+2-8
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,21 @@ android {
3636
}
3737

3838
dependencies {
39-
compile fileTree(dir: 'libs', include: ['*.jar'])
39+
compile fileTree(include: ['*.jar'], dir: 'libs')
4040
testCompile 'junit:junit:4.12'
4141
compile 'com.android.support:appcompat-v7:23.1.0'
4242
compile 'com.android.support:design:23.3.0'
43-
4443
//网络请求
4544
compile 'com.squareup.retrofit2:retrofit:2.0.2'
4645
compile 'com.squareup.retrofit2:converter-jackson:2.0.2'
4746
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
4847
compile 'io.reactivex:rxandroid:1.0.1'
49-
5048
//日志
5149
compile 'com.orhanobut:logger:1.15'
52-
5350
//图片
5451
compile 'com.github.bumptech.glide:glide:3.6.1'
55-
5652
compile 'org.greenrobot:eventbus:3.0.0'
57-
5853
//点击扩散
5954
compile 'com.kifile:MaterialView:1.0'
60-
61-
55+
compile project(':commonutils')
6256
}

app/src/main/java/com/maowubian/baseproject/component/home/events/SideBarEvent.java

+31-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.content.Intent;
5+
import android.databinding.BindingAdapter;
56
import android.databinding.DataBindingUtil;
67
import android.support.design.widget.NavigationView;
78
import android.view.Menu;
@@ -10,8 +11,11 @@
1011
import android.widget.ImageButton;
1112

1213
import com.maowubian.baseproject.R;
14+
import com.maowubian.baseproject.api.AppContext;
1315
import com.maowubian.baseproject.component.design.ui.TestWindowActivity;
16+
import com.maowubian.baseproject.component.music.media.MediaUtils;
1417
import com.maowubian.baseproject.databinding.HeaderBinding;
18+
import com.maowubian.commonutils.SpUtils;
1519
import com.orhanobut.logger.Logger;
1620

1721
/**
@@ -45,8 +49,6 @@ public static void init(NavigationView sideBar) {
4549

4650
private void doMenuItemClick(Menu menu) {
4751
for (int i = 0; i < menu.size(); i++) {
48-
49-
Logger.i("哈哈");
5052
MenuItem item = menu.getItem(i);
5153
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
5254
@Override
@@ -67,7 +69,6 @@ public boolean onMenuItemClick(MenuItem item) {
6769
case R.id.about:
6870
doAdvice();
6971
break;
70-
7172
}
7273

7374
return true;
@@ -102,21 +103,8 @@ private void doAdvice() {
102103
public static void loop(View view) {
103104
ImageButton bt = (ImageButton) view;
104105
loop++;
105-
switch (loop % 4) {
106-
case LOOP_LOOP:
107-
bt.setImageResource(R.drawable.loop_loop);
108-
break;
109-
case LOOP_ORDER:
110-
bt.setImageResource(R.drawable.loop_order);
111-
break;
112-
case LOOP_ONE:
113-
bt.setImageResource(R.drawable.loop_one);
114-
break;
115-
case LOOP_SHUFFLE:
116-
bt.setImageResource(R.drawable.loop_shuffle);
117-
break;
118-
119-
}
106+
saveStatus(bt, loop % 4);
107+
SpUtils.saveData(AppContext.mContext, MediaUtils.LOOP_TYPE,loop%4);
120108

121109

122110
}
@@ -140,4 +128,29 @@ public static void favourite(View view) {
140128

141129

142130
}
131+
132+
@BindingAdapter({"app:status"})
133+
public static void setLoopStatus(ImageButton view,int status){
134+
int anInt = SpUtils.getInt(AppContext.mContext, MediaUtils.LOOP_TYPE);
135+
saveStatus(view, anInt);
136+
137+
}
138+
139+
private static void saveStatus(ImageButton view, int anInt) {
140+
switch (anInt) {
141+
case LOOP_LOOP:
142+
view.setImageResource(R.drawable.loop_loop);
143+
break;
144+
case LOOP_ORDER:
145+
view.setImageResource(R.drawable.loop_order);
146+
break;
147+
case LOOP_ONE:
148+
view.setImageResource(R.drawable.loop_one);
149+
break;
150+
case LOOP_SHUFFLE:
151+
view.setImageResource(R.drawable.loop_shuffle);
152+
break;
153+
154+
}
155+
}
143156
}

app/src/main/java/com/maowubian/baseproject/component/music/events/MusicPageEvent.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import android.widget.PopupWindow;
88

99
import com.maowubian.baseproject.R;
10+
import com.maowubian.baseproject.api.AppContext;
11+
import com.maowubian.baseproject.component.music.media.MediaUtils;
1012
import com.maowubian.baseproject.component.music.ui.MusicListActivity;
13+
import com.maowubian.commonutils.SpUtils;
1114

1215
/**
1316
* Created by zhuruqiao on 2016/8/7.
@@ -18,23 +21,26 @@ public final class MusicPageEvent {
1821
public static void toLocalList(View view) {
1922

2023
startActivity(view, R.string.local_list);
24+
SpUtils.saveData(AppContext.mContext, MediaUtils.CURRENT_PLAY_LIST, MediaUtils.LOCAL_LIST);
2125

2226
}
2327

2428
public static void toLocalDownlaodList(View view) {
2529

2630
startActivity(view, R.string.local_download);
27-
31+
SpUtils.saveData(AppContext.mContext, MediaUtils.CURRENT_PLAY_LIST, MediaUtils.LOCAL_LIST);
2832
}
2933

3034
public static void toRecentPlayList(View view) {
3135

3236
startActivity(view, R.string.recent_play);
37+
SpUtils.saveData(AppContext.mContext, MediaUtils.CURRENT_PLAY_LIST, MediaUtils.LOCAL_LIST);
3338
}
3439

3540
public static void toFavouriteList(View view) {
3641

3742
startActivity(view, R.string.favourite);
43+
SpUtils.saveData(AppContext.mContext, MediaUtils.CURRENT_PLAY_LIST, MediaUtils.LOCAL_LIST);
3844

3945
}
4046

app/src/main/java/com/maowubian/baseproject/component/music/media/MediaUtils.java

+77
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import android.content.ContentResolver;
44
import android.database.Cursor;
5+
import android.widget.Switch;
56

67
import com.maowubian.baseproject.api.AppContext;
78
import com.maowubian.baseproject.component.music.media.data.MusicInfo;
9+
import com.maowubian.commonutils.SpUtils;
810
import com.orhanobut.logger.Logger;
911

1012
import java.util.ArrayList;
1113
import java.util.List;
14+
import java.util.Random;
1215

1316
import static android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
1417

@@ -17,6 +20,21 @@
1720
*/
1821
public class MediaUtils {
1922

23+
public static final String LOOP_TYPE = "loop_type";
24+
25+
public static final String CURRENT_PLAY_LIST = "current_play_list";
26+
public static final int LOCAL_LIST = 0;
27+
public static final int RECENT_PLAY = 1;
28+
public static final int FAVOURITE = 2;
29+
30+
private static final int LOOP_LOOP = 0;
31+
32+
private static final int LOOP_ORDER = 1;
33+
34+
private static final int LOOP_ONE = 2;
35+
36+
private static final int LOOP_SHUFFLE = 3;
37+
2038
public static List<MusicInfo> getLocalMusicList() {
2139
ContentResolver contentResolver = AppContext.mContext.getContentResolver();
2240

@@ -61,4 +79,63 @@ public static String getDownloadMusicCount() {
6179

6280
return "(" + getLocalMusicList().size() + ")";
6381
}
82+
83+
public static MusicInfo getMusicByLoopType(String path) {
84+
85+
MusicInfo info = null;
86+
int current = 0;
87+
//获取当前播放列表
88+
List<MusicInfo> currentPlayList = getCurrentPlayList();
89+
for (int i = 0; i < currentPlayList.size(); i++) {
90+
if (currentPlayList.get(i).path.equals(path)) {
91+
info = currentPlayList.get(i);
92+
current = i;
93+
break;
94+
}
95+
}
96+
97+
int type = SpUtils.getInt(AppContext.mContext, LOOP_TYPE);
98+
99+
switch (type) {
100+
case LOOP_ONE:
101+
return info;
102+
case LOOP_LOOP:
103+
if (current == currentPlayList.size() - 1) {
104+
return currentPlayList.get(0);
105+
} else {
106+
return currentPlayList.get(current + 1);
107+
}
108+
case LOOP_ORDER:
109+
if (current == currentPlayList.size() - 1) {
110+
return null;
111+
} else {
112+
return currentPlayList.get(current + 1);
113+
}
114+
case LOOP_SHUFFLE:
115+
Random random = new Random();
116+
while (true) {
117+
int i = random.nextInt(currentPlayList.size());
118+
if (current != i) {
119+
return currentPlayList.get(i);
120+
}
121+
}
122+
123+
}
124+
125+
return null;
126+
}
127+
128+
public static List<MusicInfo> getCurrentPlayList() {
129+
int anInt = SpUtils.getInt(AppContext.mContext, CURRENT_PLAY_LIST);
130+
switch (anInt) {
131+
case LOCAL_LIST:
132+
break;
133+
case RECENT_PLAY:
134+
break;
135+
case FAVOURITE:
136+
break;
137+
}
138+
139+
return getLocalMusicList();
140+
}
64141
}

app/src/main/java/com/maowubian/baseproject/component/music/media/PlayControlImpl.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.os.Binder;
88
import android.util.Log;
99

10+
import com.maowubian.baseproject.component.music.media.data.MusicInfo;
1011
import com.maowubian.baseproject.component.music.media.inter.PlayControl;
1112
import com.maowubian.baseproject.factory.StoreConfigFactory;
1213
import com.orhanobut.logger.Logger;
@@ -41,6 +42,8 @@ public class PlayControlImpl extends Binder implements PlayControl, MediaPlayer.
4142

4243
public static final int COMPLETE = 2;
4344

45+
private String currentPlayPath;
46+
4447

4548
public static PlayControlImpl getInstance() {
4649
return instance;
@@ -60,6 +63,7 @@ public static void init(MediaPlayer mediaPlayer) {
6063

6164
@Override
6265
public void play(String path) {
66+
currentPlayPath=path;
6367
timer = new Timer(true);
6468
if (isPause) {
6569
mediaPlayer.start();
@@ -121,9 +125,12 @@ private void initVIsualizer() {
121125
}
122126
}
123127

128+
//when the song was over,naturally,This method will be callback
124129
@Override
125130
public void onCompletion(MediaPlayer mp) {
126-
eventBus.post(COMPLETE);
131+
MusicInfo musicInfo = MediaUtils.getMusicByLoopType(currentPlayPath);
132+
Logger.i("下一曲:%s",musicInfo.path);
127133

134+
play(musicInfo.path);
128135
}
129136
}

app/src/main/java/com/maowubian/baseproject/component/music/ui/MusicListActivity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
public class MusicListActivity extends BaseActivity<MusicListPageBiding> {
3333

3434
private static final String TITLE = "title";
35-
private int distance = 0;
3635

36+
private int distance = 0;
3737

3838
private List<MusicInfo> musicInfos;
3939

app/src/main/res/layout/header.xml

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
22

3-
<layout xmlns:android="http://schemas.android.com/apk/res/android">
3+
<layout xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto">
45

56
<data class="HeaderBinding">
67

7-
<import type="com.maowubian.baseproject.component.home.events.SideBarEvent"/>
8+
<import type="com.maowubian.baseproject.component.home.events.SideBarEvent" />
89

910
</data>
1011

@@ -44,9 +45,9 @@
4445
android:layout_gravity="center_vertical"
4546
android:layout_weight="1"
4647
android:background="@null"
47-
android:scaleType="fitCenter"
4848
android:onClick="@{SideBarEvent.loop}"
49-
android:src="@drawable/loop_loop" />
49+
android:scaleType="fitCenter"
50+
app:status="@{'0'}"/>
5051

5152
<ImageButton
5253
android:id="@+id/pre"
@@ -55,8 +56,8 @@
5556
android:layout_gravity="center_vertical"
5657
android:layout_weight="1"
5758
android:background="@null"
58-
android:scaleType="fitCenter"
5959
android:onClick="@{SideBarEvent.pre}"
60+
android:scaleType="fitCenter"
6061
android:src="@drawable/pre" />
6162

6263
<ImageButton
@@ -72,11 +73,11 @@
7273
<ImageButton
7374
android:id="@+id/next"
7475
android:layout_width="0dp"
75-
android:onClick="@{SideBarEvent.next}"
7676
android:layout_height="@dimen/dp30"
7777
android:layout_gravity="center_vertical"
7878
android:layout_weight="1"
7979
android:background="@null"
80+
android:onClick="@{SideBarEvent.next}"
8081
android:scaleType="fitCenter"
8182
android:src="@drawable/next" />
8283

@@ -90,11 +91,11 @@
9091
android:id="@+id/favourite"
9192
android:layout_width="@dimen/dp30"
9293
android:layout_height="@dimen/dp30"
93-
android:onClick="@{SideBarEvent.favourite}"
9494
android:layout_centerInParent="true"
9595
android:layout_gravity="center_vertical"
9696
android:background="@drawable/love"
97-
android:button="@null" />
97+
android:button="@null"
98+
android:onClick="@{SideBarEvent.favourite}" />
9899
</RelativeLayout>
99100

100101
</LinearLayout>

0 commit comments

Comments
 (0)