Skip to content

Commit 6cefda8

Browse files
CYGCYG
authored andcommitted
[Android]fix cosinger demo bugs
1 parent decf65f commit 6cefda8

File tree

1 file changed

+87
-18
lines changed

1 file changed

+87
-18
lines changed

KTVAPI/Android/app/src/main/java/io/agora/ktvdemo/ui/LivingFragment.kt

Lines changed: 87 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,86 @@ class LivingFragment : BaseFragment<FragmentLivingBinding>() {
101101
if (KeyCenter.role == KTVSingRole.LeadSinger) {
102102
toast(getString(R.string.app_no_premission))
103103
} else {
104-
ktvApi.switchSingerRole(KTVSingRole.CoSinger, object : ISwitchRoleStateListener {
105-
override fun onSwitchRoleSuccess() {
106-
mainHandler.post {
107-
toast("加入合唱成功,自动开麦")
108-
ktvApi.muteMic(false)
109-
btMicStatus.text = "麦克风开"
110-
tvSinger.text = getString(R.string.app_co_singer)
111-
KeyCenter.role = KTVSingRole.CoSinger
104+
if (KeyCenter.isMcc) {
105+
// 使用声网版权中心歌单
106+
val musicConfiguration = KTVLoadMusicConfiguration(
107+
KeyCenter.songCode.toString(), // 需要传入唯一的歌曲id,demo 简化逻辑传了songCode
108+
KeyCenter.LeadSingerUid,
109+
KTVLoadMusicMode.LOAD_MUSIC_ONLY
110+
)
111+
ktvApi.loadMusic(KeyCenter.songCode, musicConfiguration, object : IMusicLoadStateListener {
112+
override fun onMusicLoadSuccess(songCode: Long, lyricUrl: String) {
113+
Log.d("Music", "onMusicLoadSuccess, songCode: $songCode, lyricUrl: $lyricUrl")
114+
// 切换身份为合唱者
115+
ktvApi.switchSingerRole(KTVSingRole.CoSinger, object : ISwitchRoleStateListener {
116+
override fun onSwitchRoleSuccess() {
117+
mainHandler.post {
118+
toast("加入合唱成功,自动开麦")
119+
ktvApi.muteMic(false)
120+
btMicStatus.text = "麦克风开"
121+
tvSinger.text = getString(R.string.app_co_singer)
122+
KeyCenter.role = KTVSingRole.CoSinger
123+
}
124+
}
125+
126+
override fun onSwitchRoleFail(reason: SwitchRoleFailReason) {
127+
mainHandler.post {
128+
toast("加入合唱失败")
129+
}
130+
}
131+
})
112132
}
113-
}
114133

115-
override fun onSwitchRoleFail(reason: SwitchRoleFailReason) {
116-
mainHandler.post {
117-
toast("加入合唱失败")
134+
override fun onMusicLoadFail(songCode: Long, reason: KTVLoadMusicFailReason) {
135+
Log.d("Music", "onMusicLoadFail, songCode: $songCode, reason: $reason")
118136
}
119-
}
120-
})
137+
138+
override fun onMusicLoadProgress(
139+
songCode: Long,
140+
percent: Int,
141+
status: MusicLoadStatus,
142+
msg: String?,
143+
lyricUrl: String?
144+
) {
145+
Log.d("Music", "onMusicLoadProgress, songCode: $songCode, percent: $percent")
146+
mainHandler.post {
147+
binding?.btLoadProgress?.text = "下载进度:$percent%"
148+
}
149+
}
150+
})
151+
} else {
152+
// 使用本地音乐文件
153+
val musicConfiguration = KTVLoadMusicConfiguration(
154+
KeyCenter.songCode.toString(), // 需要传入唯一的歌曲id,demo 简化逻辑传了songCode
155+
KeyCenter.LeadSingerUid,
156+
KTVLoadMusicMode.LOAD_NONE
157+
)
158+
val songPath = requireActivity().filesDir.absolutePath + File.separator
159+
val songName = "不如跳舞"
160+
ktvApi.loadMusic("$songPath$songName.mp4", musicConfiguration)
161+
val fileLrc = File("$songPath$songName.xml")
162+
val lyricsModel = KaraokeView.parseLyricsData(fileLrc)
163+
karaokeView?.lyricsData = lyricsModel
164+
165+
// 切换身份为合唱者
166+
ktvApi.switchSingerRole(KTVSingRole.CoSinger, object : ISwitchRoleStateListener {
167+
override fun onSwitchRoleSuccess() {
168+
mainHandler.post {
169+
toast("加入合唱成功,自动开麦")
170+
ktvApi.muteMic(false)
171+
btMicStatus.text = "麦克风开"
172+
tvSinger.text = getString(R.string.app_co_singer)
173+
KeyCenter.role = KTVSingRole.CoSinger
174+
}
175+
}
176+
177+
override fun onSwitchRoleFail(reason: SwitchRoleFailReason) {
178+
mainHandler.post {
179+
toast("加入合唱失败")
180+
}
181+
}
182+
})
183+
}
121184
}
122185
}
123186

@@ -153,7 +216,8 @@ class LivingFragment : BaseFragment<FragmentLivingBinding>() {
153216
if (KeyCenter.isMcc) {
154217
// 使用声网版权中心歌单
155218
val musicConfiguration = KTVLoadMusicConfiguration(
156-
KeyCenter.songCode.toString(), KeyCenter.LeadSingerUid,
219+
KeyCenter.songCode.toString(), // 需要传入唯一的歌曲id,demo 简化逻辑传了songCode
220+
KeyCenter.LeadSingerUid,
157221
if (KeyCenter.role == KTVSingRole.Audience) KTVLoadMusicMode.LOAD_LRC_ONLY else KTVLoadMusicMode.LOAD_MUSIC_AND_LRC
158222
)
159223
ktvApi.loadMusic(KeyCenter.songCode, musicConfiguration, object : IMusicLoadStateListener {
@@ -204,7 +268,9 @@ class LivingFragment : BaseFragment<FragmentLivingBinding>() {
204268
} else {
205269
// 使用本地音乐文件
206270
val musicConfiguration = KTVLoadMusicConfiguration(
207-
KeyCenter.songCode.toString(), KeyCenter.LeadSingerUid, KTVLoadMusicMode.LOAD_NONE
271+
KeyCenter.songCode.toString(), // 需要传入唯一的歌曲id,demo 简化逻辑传了songCode
272+
KeyCenter.LeadSingerUid,
273+
KTVLoadMusicMode.LOAD_NONE
208274
)
209275
val songPath = requireActivity().filesDir.absolutePath + File.separator
210276
val songName = "不如跳舞"
@@ -381,7 +447,8 @@ class LivingFragment : BaseFragment<FragmentLivingBinding>() {
381447
if (KeyCenter.isMcc) {
382448
// 使用声网版权中心歌单
383449
val musicConfiguration = KTVLoadMusicConfiguration(
384-
KeyCenter.songCode.toString(), KeyCenter.LeadSingerUid,
450+
KeyCenter.songCode.toString(), // 需要传入唯一的歌曲id,demo 简化逻辑传了songCode
451+
KeyCenter.LeadSingerUid,
385452
if (KeyCenter.role == KTVSingRole.Audience) KTVLoadMusicMode.LOAD_LRC_ONLY else KTVLoadMusicMode.LOAD_MUSIC_AND_LRC
386453
)
387454
ktvApi.loadMusic(KeyCenter.songCode, musicConfiguration, object : IMusicLoadStateListener {
@@ -422,7 +489,9 @@ class LivingFragment : BaseFragment<FragmentLivingBinding>() {
422489
} else {
423490
// 使用本地音乐文件
424491
val musicConfiguration = KTVLoadMusicConfiguration(
425-
KeyCenter.songCode.toString(), KeyCenter.LeadSingerUid, KTVLoadMusicMode.LOAD_NONE
492+
KeyCenter.songCode.toString(), // 需要传入唯一的歌曲id,demo 简化逻辑传了songCode
493+
KeyCenter.LeadSingerUid,
494+
KTVLoadMusicMode.LOAD_NONE
426495
)
427496
val songPath = requireActivity().filesDir.absolutePath + File.separator
428497
val songName = "不如跳舞"

0 commit comments

Comments
 (0)