@@ -2,16 +2,19 @@ package io.agora.ktvdemo.ui
2
2
3
3
import android.os.Bundle
4
4
import android.text.TextUtils
5
+ import android.util.Log
5
6
import android.view.LayoutInflater
6
7
import android.view.View
7
8
import android.view.ViewGroup
9
+ import android.widget.Toast
8
10
import androidx.navigation.fragment.findNavController
9
11
import io.agora.karaoke_view.v11.KaraokeView
10
12
import io.agora.ktvapi.*
11
13
import io.agora.ktvdemo.BuildConfig
12
- import io.agora.ktvdemo.rtc.RtcEngineController
13
- import io.agora.ktvdemo.databinding.FragmentLivingBinding
14
+ import io.agora.ktvdemo.MyApplication
14
15
import io.agora.ktvdemo.R
16
+ import io.agora.ktvdemo.databinding.FragmentLivingBinding
17
+ import io.agora.ktvdemo.rtc.RtcEngineController
15
18
import io.agora.ktvdemo.utils.DownloadUtils
16
19
import io.agora.ktvdemo.utils.KeyCenter
17
20
import io.agora.ktvdemo.utils.ZipUtils
@@ -38,6 +41,10 @@ class LivingFragment : BaseFragment<FragmentLivingBinding>() {
38
41
initView()
39
42
initKTVApi()
40
43
joinChannel()
44
+
45
+ if (KeyCenter .isLeadSinger() || KeyCenter .isCoSinger()) {
46
+ ktvApi.muteMic(false )
47
+ }
41
48
}
42
49
43
50
override fun onDestroy () {
@@ -51,6 +58,8 @@ class LivingFragment : BaseFragment<FragmentLivingBinding>() {
51
58
private fun initView () {
52
59
binding?.apply {
53
60
karaokeView = KaraokeView (lyricsView,null )
61
+
62
+ // 退出场景
54
63
btnClose.setOnClickListener {
55
64
ktvApi.switchSingerRole(KTVSingRole .Audience , null )
56
65
ktvApi.removeEventHandler(ktvApiEventHandler)
@@ -65,6 +74,162 @@ class LivingFragment : BaseFragment<FragmentLivingBinding>() {
65
74
} else {
66
75
tvSinger.text = getString(R .string.app_audience)
67
76
}
77
+
78
+ // 加入合唱
79
+ btJoinChorus.setOnClickListener {
80
+ if (KeyCenter .isLeadSinger()) {
81
+ Toast .makeText(MyApplication .app(), R .string.app_no_premission, Toast .LENGTH_SHORT ).show()
82
+ } else {
83
+ ktvApi.switchSingerRole(KTVSingRole .CoSinger , null )
84
+ }
85
+ }
86
+
87
+ // 退出合唱
88
+ btLeaveChorus.setOnClickListener {
89
+ if (KeyCenter .isLeadSinger()) {
90
+ Toast .makeText(MyApplication .app(), R .string.app_no_premission, Toast .LENGTH_SHORT ).show()
91
+ } else {
92
+ ktvApi.switchSingerRole(KTVSingRole .Audience , null )
93
+ }
94
+ }
95
+
96
+ // 开原唱:仅领唱和合唱者可以做这项操作
97
+ btOriginal.setOnClickListener {
98
+ if (KeyCenter .isLeadSinger()) {
99
+ ktvApi.getMediaPlayer().selectMultiAudioTrack(0 , 0 )
100
+ } else if (KeyCenter .isCoSinger()) {
101
+ ktvApi.getMediaPlayer().selectAudioTrack(0 )
102
+ } else {
103
+ Toast .makeText(MyApplication .app(), R .string.app_no_premission, Toast .LENGTH_SHORT ).show()
104
+ }
105
+ }
106
+
107
+ // 开伴奏:仅领唱和合唱者可以做这项操作
108
+ btAcc.setOnClickListener {
109
+ if (KeyCenter .isLeadSinger()) {
110
+ ktvApi.getMediaPlayer().selectMultiAudioTrack(1 , 1 )
111
+ } else if (KeyCenter .isCoSinger()) {
112
+ ktvApi.getMediaPlayer().selectAudioTrack(1 )
113
+ } else {
114
+ Toast .makeText(MyApplication .app(), R .string.app_no_premission, Toast .LENGTH_SHORT ).show()
115
+ }
116
+ }
117
+
118
+ // 开导唱:仅领唱可以做这项操作,开启后领唱本地听到歌曲原唱,但观众听到仍为伴奏
119
+ btDaoChang.setOnClickListener {
120
+ if (KeyCenter .isLeadSinger()) {
121
+ ktvApi.getMediaPlayer().selectMultiAudioTrack(0 , 1 )
122
+ } else if (KeyCenter .isCoSinger()) {
123
+ Toast .makeText(MyApplication .app(), R .string.app_no_premission, Toast .LENGTH_SHORT ).show()
124
+ } else {
125
+ Toast .makeText(MyApplication .app(), R .string.app_no_premission, Toast .LENGTH_SHORT ).show()
126
+ }
127
+ }
128
+
129
+ // 加载音乐
130
+ btLoadMusic.setOnClickListener {
131
+ if (KeyCenter .isMcc) {
132
+ // 使用声网版权中心歌单
133
+ val musicConfiguration = KTVLoadMusicConfiguration (
134
+ KeyCenter .songCode.toString(), false , KeyCenter .LeadSingerUid ,
135
+ if (KeyCenter .isAudience()) KTVLoadMusicMode .LOAD_LRC_ONLY else KTVLoadMusicMode .LOAD_MUSIC_AND_LRC
136
+ )
137
+ ktvApi.loadMusic(KeyCenter .songCode, musicConfiguration, object : IMusicLoadStateListener {
138
+ override fun onMusicLoadSuccess (songCode : Long , lyricUrl : String ) {
139
+ Log .d(" Music" , " onMusicLoadSuccess, songCode: $songCode , lyricUrl: $lyricUrl " )
140
+ if (KeyCenter .isLeadSinger()) {
141
+ ktvApi.switchSingerRole(KTVSingRole .LeadSinger , object : ISwitchRoleStateListener {
142
+ override fun onSwitchRoleSuccess () {
143
+ // 加载成功开始播放音乐
144
+ ktvApi.startSing(KeyCenter .songCode, 0 )
145
+ }
146
+
147
+ override fun onSwitchRoleFail (reason : SwitchRoleFailReason ) {
148
+
149
+ }
150
+ })
151
+ } else if (KeyCenter .isCoSinger()) {
152
+ ktvApi.switchSingerRole(KTVSingRole .CoSinger , object : ISwitchRoleStateListener {
153
+ override fun onSwitchRoleSuccess () {
154
+
155
+ }
156
+
157
+ override fun onSwitchRoleFail (reason : SwitchRoleFailReason ) {
158
+
159
+ }
160
+ })
161
+ }
162
+ }
163
+
164
+ override fun onMusicLoadFail (songCode : Long , reason : KTVLoadSongFailReason ) {
165
+ Log .d(" Music" , " onMusicLoadFail, songCode: $songCode , reason: $reason " )
166
+ }
167
+
168
+ override fun onMusicLoadProgress (
169
+ songCode : Long ,
170
+ percent : Int ,
171
+ status : MusicLoadStatus ,
172
+ msg : String? ,
173
+ lyricUrl : String?
174
+ ) {
175
+ lyricsView.setLabelShownWhenNoLyrics(" $percent %" )
176
+ Log .d(" Music" , " onMusicLoadProgress, songCode: $songCode , percent: $percent " )
177
+ }
178
+ })
179
+ } else {
180
+ // 使用本地音乐文件
181
+ val musicConfiguration = KTVLoadMusicConfiguration (
182
+ KeyCenter .songCode.toString(), false , KeyCenter .LeadSingerUid , KTVLoadMusicMode .LOAD_NONE
183
+ )
184
+ val songPath = requireActivity().filesDir.absolutePath + File .separator
185
+ val songName = " 成都"
186
+ ktvApi.loadMusic(" $songPath$songName .mp3" , musicConfiguration)
187
+ val fileLrc = File (" $songPath$songName .xml" )
188
+ val lyricsModel = KaraokeView .parseLyricsData(fileLrc)
189
+ karaokeView?.lyricsData = lyricsModel
190
+ if (KeyCenter .isLeadSinger()) {
191
+ ktvApi.switchSingerRole(KTVSingRole .LeadSinger , object : ISwitchRoleStateListener {
192
+ override fun onSwitchRoleSuccess () {
193
+ ktvApi.startSing(" $songPath$songName .mp3" , 0 )
194
+ }
195
+
196
+ override fun onSwitchRoleFail (reason : SwitchRoleFailReason ) {
197
+
198
+ }
199
+ })
200
+ } else if (KeyCenter .isCoSinger()) {
201
+ ktvApi.switchSingerRole(KTVSingRole .CoSinger , object : ISwitchRoleStateListener {
202
+ override fun onSwitchRoleSuccess () {
203
+
204
+ }
205
+
206
+ override fun onSwitchRoleFail (reason : SwitchRoleFailReason ) {
207
+
208
+ }
209
+ })
210
+ }
211
+ }
212
+ }
213
+
214
+ // 取消加载歌曲并删除本地歌曲缓存
215
+ btRemoveMusic.setOnClickListener {
216
+ if (KeyCenter .isMcc) {
217
+ ktvApi.removeMusic(KeyCenter .songCode)
218
+ lyricsView.reset()
219
+ } else {
220
+ Toast .makeText(MyApplication .app(), R .string.app_no_premission, Toast .LENGTH_SHORT ).show()
221
+ }
222
+ }
223
+
224
+ // 开麦
225
+ btMicOn.setOnClickListener {
226
+ ktvApi.muteMic(false )
227
+ }
228
+
229
+ // 关麦
230
+ btMicOff.setOnClickListener {
231
+ ktvApi.muteMic(true )
232
+ }
68
233
}
69
234
}
70
235
@@ -101,102 +266,25 @@ class LivingFragment : BaseFragment<FragmentLivingBinding>() {
101
266
)
102
267
ktvApi.initialize(ktvApiConfig)
103
268
ktvApi.addEventHandler(ktvApiEventHandler)
104
- ktvApi.renewInnerDataStreamId()
105
269
ktvApi.setLrcView(object : ILrcView {
106
270
override fun onUpdatePitch (pitch : Float? ) {
107
271
}
108
272
109
273
override fun onUpdateProgress (progress : Long? ) {
110
- if ( progress != null ) {
111
- karaokeView?.setProgress(progress )
274
+ progress?. let {
275
+ karaokeView?.setProgress(it )
112
276
}
113
277
}
114
278
115
279
override fun onDownloadLrcData (url : String? ) {
116
- if ( url != null ) {
117
- dealDownloadLrc(url )
280
+ url?. let {
281
+ dealDownloadLrc(it )
118
282
}
119
283
}
120
284
121
285
override fun onHighPartTime (highStartTime : Long , highEndTime : Long ) {
122
286
}
123
287
})
124
- if (KeyCenter .isMcc) {
125
- val musicConfiguration = KTVLoadMusicConfiguration (
126
- KeyCenter .songCode.toString(), false , KeyCenter .LeadSingerUid ,
127
- if (KeyCenter .isAudience()) KTVLoadMusicMode .LOAD_LRC_ONLY else KTVLoadMusicMode .LOAD_MUSIC_AND_LRC
128
- )
129
- ktvApi.loadMusic(KeyCenter .songCode, musicConfiguration, object : IMusicLoadStateListener {
130
- override fun onMusicLoadSuccess (songCode : Long , lyricUrl : String ) {
131
- if (KeyCenter .isLeadSinger()) {
132
- ktvApi.switchSingerRole(KTVSingRole .LeadSinger , object : ISwitchRoleStateListener {
133
- override fun onSwitchRoleSuccess () {
134
- ktvApi.startSing(KeyCenter .songCode, 0 )
135
- }
136
-
137
- override fun onSwitchRoleFail (reason : SwitchRoleFailReason ) {
138
-
139
- }
140
- })
141
- } else if (KeyCenter .isCoSinger()) {
142
- ktvApi.switchSingerRole(KTVSingRole .CoSinger , object : ISwitchRoleStateListener {
143
- override fun onSwitchRoleSuccess () {
144
-
145
- }
146
-
147
- override fun onSwitchRoleFail (reason : SwitchRoleFailReason ) {
148
-
149
- }
150
- })
151
- }
152
- }
153
-
154
- override fun onMusicLoadFail (songCode : Long , reason : KTVLoadSongFailReason ) {
155
-
156
- }
157
-
158
- override fun onMusicLoadProgress (
159
- songCode : Long ,
160
- percent : Int ,
161
- status : MusicLoadStatus ,
162
- msg : String? ,
163
- lyricUrl : String?
164
- ) {
165
-
166
- }
167
- })
168
- } else {
169
- val musicConfiguration = KTVLoadMusicConfiguration (
170
- KeyCenter .songCode.toString(), false , KeyCenter .LeadSingerUid , KTVLoadMusicMode .LOAD_NONE
171
- )
172
- val songPath = requireActivity().filesDir.absolutePath + File .separator
173
- val songName = " 成都"
174
- ktvApi.loadMusic(" $songPath$songName .mp3" , musicConfiguration)
175
- val fileLrc = File (" $songPath$songName .xml" )
176
- val lyricsModel = KaraokeView .parseLyricsData(fileLrc)
177
- karaokeView?.lyricsData = lyricsModel
178
- if (KeyCenter .isLeadSinger()) {
179
- ktvApi.switchSingerRole(KTVSingRole .LeadSinger , object : ISwitchRoleStateListener {
180
- override fun onSwitchRoleSuccess () {
181
- ktvApi.startSing(" $songPath$songName .mp3" , 0 )
182
- }
183
-
184
- override fun onSwitchRoleFail (reason : SwitchRoleFailReason ) {
185
-
186
- }
187
- })
188
- } else if (KeyCenter .isCoSinger()) {
189
- ktvApi.switchSingerRole(KTVSingRole .CoSinger , object : ISwitchRoleStateListener {
190
- override fun onSwitchRoleSuccess () {
191
-
192
- }
193
-
194
- override fun onSwitchRoleFail (reason : SwitchRoleFailReason ) {
195
-
196
- }
197
- })
198
- }
199
- }
200
288
}
201
289
202
290
private fun dealDownloadLrc (url : String ) {
0 commit comments