/
MainActivity.java
157 lines (133 loc) · 5.98 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.lzx.musiclib;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.lzx.starrysky.manager.MediaSessionConnection;
import com.lzx.starrysky.manager.MusicManager;
import com.lzx.starrysky.manager.OnPlayerEventListener;
import com.lzx.starrysky.model.SongInfo;
import com.lzx.starrysky.playback.download.ExoDownload;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
boolean isFavorite = false;
boolean isChecked = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SongInfo s1 = new SongInfo();
s1.setSongId("111");
s1.setSongUrl("http://music.163.com/song/media/outer/url?id=317151.mp3");
s1.setSongCover("https://www.qqkw.com/d/file/p/2018/04-21/c24fd86006670f964e63cb8f9c129fc6.jpg");
s1.setSongName("心雨");
s1.setArtist("贤哥");
SongInfo s2 = new SongInfo();
s2.setSongId("222");
s2.setSongUrl("http://music.163.com/song/media/outer/url?id=281951.mp3");
s2.setSongCover("https://n.sinaimg.cn/sinacn13/448/w1024h1024/20180504/7b5f-fzyqqiq8228305.jpg");
s2.setSongName("我曾用心爱着你");
s2.setArtist("潘美辰");
SongInfo s3 = new SongInfo();
s3.setSongId("333");
s3.setSongUrl("http://music.163.com/song/media/outer/url?id=25906124.mp3");
s3.setSongCover("http://cn.chinadaily.com.cn/img/attachement/jpg/site1/20180211/509a4c2df41d1bea45f73b.jpg");
s3.setSongName("不要说话");
s3.setArtist("陈奕迅");
List<SongInfo> songInfos = new ArrayList<>();
songInfos.add(s1);
songInfos.add(s2);
songInfos.add(s3);
findViewById(R.id.play).setOnClickListener(v -> MusicManager.getInstance().playMusic(songInfos, 0));
findViewById(R.id.pause).setOnClickListener(v -> MusicManager.getInstance().pauseMusic());
findViewById(R.id.resum).setOnClickListener(v -> MusicManager.getInstance().playMusic());
findViewById(R.id.stop).setOnClickListener(v -> MusicManager.getInstance().stopMusic());
findViewById(R.id.pre).setOnClickListener(v -> MusicManager.getInstance().skipToPrevious());
findViewById(R.id.next).setOnClickListener(v -> MusicManager.getInstance().skipToNext());
findViewById(R.id.fastForward).setOnClickListener(v -> MusicManager.getInstance().fastForward());
findViewById(R.id.rewind).setOnClickListener(v -> MusicManager.getInstance().rewind());
findViewById(R.id.currSong).setOnClickListener(v -> {
SongInfo songInfo = MusicManager.getInstance().getNowPlayingSongInfo();
if (songInfo == null) {
Toast.makeText(MainActivity.this, "songInfo is null", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "curr SongInfo = " + songInfo.getSongId(), Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.currSongId).setOnClickListener(v -> {
String songId = MusicManager.getInstance().getNowPlayingSongId();
if (TextUtils.isEmpty(songId)) {
Toast.makeText(MainActivity.this, "songId is null", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "songId = " + songId, Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.currSongIndex).setOnClickListener(v -> {
int index = MusicManager.getInstance().getNowPlayingIndex();
Toast.makeText(MainActivity.this, "index = " + index, Toast.LENGTH_SHORT).show();
});
findViewById(R.id.sendFavorite).setOnClickListener(v -> {
if (isFavorite) {
MusicManager.getInstance().updateFavoriteUI(false);
isFavorite = false;
} else {
MusicManager.getInstance().updateFavoriteUI(true);
isFavorite = true;
}
});
findViewById(R.id.sendLyrics).setOnClickListener(v -> {
if (isChecked) {
MusicManager.getInstance().updateLyricsUI(false);
isChecked = false;
} else {
MusicManager.getInstance().updateLyricsUI(true);
isChecked = true;
}
});
findViewById(R.id.cacheSize).setOnClickListener(v -> {
String size = ExoDownload.getInstance().getCachedSize() + "";
Toast.makeText(MainActivity.this, "大小:" + size, Toast.LENGTH_SHORT).show();
});
MusicManager.getInstance().addPlayerEventListener(new OnPlayerEventListener() {
@Override
public void onMusicSwitch(SongInfo songInfo) {
}
@Override
public void onPlayerStart() {
Log.i("xian", "--onPlayerStart--");
}
@Override
public void onPlayerPause() {
Log.i("xian", "--onPlayerPause--");
}
@Override
public void onPlayerStop() {
Log.i("xian", "--onPlayerStop--");
}
@Override
public void onPlayCompletion() {
Log.i("xian", "--onPlayCompletion--");
}
@Override
public void onBuffering() {
Log.i("xian", "--onBuffering--");
}
@Override
public void onError(int errorCode, String errorMsg) {
Log.i("xian", "--onError-- errorCode=" + errorCode + " errorMsg = " + errorMsg);
}
});
}
@Override
protected void onStart() {
super.onStart();
MediaSessionConnection.getInstance(this).connect();
}
@Override
protected void onStop() {
super.onStop();
MediaSessionConnection.getInstance(this).disconnect();
}
}