用Kotlin开发的一个安卓在线音乐播放器

  在学习Android开发基础一个月后,进入实战开发!用Kotlin开发一个音乐播放器。博客上搜集大量资料后,在一些前辈的基础上加以改进,开发一个自己的音乐播放器。生成的APK文件我给它命名为平头音乐,应用中有些功能待实现,有些功能待改进。

该项目主要特点有:

(1)整体项目采用MVVM架构模式(Model-View-ViewModel),逻辑清晰

(2)用到了安卓开发四大组件中的Activity、BroadcastReceiver、Service

(3)用酷狗API和MediaPlayer类实现在线音乐播放

(4)用SharedPreferences实现存储功能

(5)用LyricViewX类实现歌词显示

部分界面展示如下:

1.主界面,主要用到了RotateAnimation类展示一个旋转动画,点击音乐Button会发送一条广播

val imageView: ImageView = findViewById(R.id.rotateimg)
        val animation = RotateAnimation(0f,360f,320f,320f)
        animation.setDuration(20000)
        animation.setRepeatCount(Animation.INFINITE)
        animation.setRepeatMode(Animation.RESTART)
        imageView.startAnimation(animation)

        val intent=Intent(this,MyService::class.java)
        startService(intent)

        val musBtn:Button=findViewById(R.id.musBtn)
        musBtn.setOnClickListener {
            val intent= Intent(this,Activity_music::class.java)
            val intent1=Intent("com.sunnyweather.auaviplayer.MY_BROADCAST1")
            intent1.setPackage(packageName)
            sendBroadcast(intent1)
            startActivity(intent)

2.音乐歌单界面,收到点击音乐Button发送的广播用Toast弹出消息,界面隐藏状态栏

 class MyBroadcastReceiver: BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            Toast.makeText(context,"已进入音乐功能",Toast.LENGTH_SHORT).show()
        }
    }
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN)
        setContentView(R.layout.activity_music)

3.通知栏有前台Service显示,通过Notification类实现大小图标的显示,但这几个ImageButton功能还没实现

 val manager=getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            val channel=NotificationChannel("my_service","前台Servive通知",
                NotificationManager.IMPORTANCE_DEFAULT)
            manager.createNotificationChannel(channel)
        }

        val intent= Intent(this, MainActivity::class.java)
        val pi= PendingIntent.getActivity(this,0,intent,FLAG_IMMUTABLE)
        val remoteViews= RemoteViews(packageName, R.layout.activity_notification)
        val remoteViews1=RemoteViews(packageName,R.layout.notifi_small)
        val notification= NotificationCompat.Builder(this,"my_service")
            .setCustomContentView(remoteViews1)
            .setCustomBigContentView(remoteViews)
            .setContentIntent(pi)
            .setSmallIcon(R.drawable.rotate)
            .build()
        startForeground(1,notification)

4.搜索界面,根据输入内容实时显示搜索结果,用LiveData进行观察

fun searchSong(key:String)=liveData(Dispatchers.IO){
        val result=try {
            val Songlist= ArrayList<Song>()
            search(key).data?.forEach{Songlist.add(it)}
            if (search(key).msg=="操作成功"){
                Result.success(Songlist)
            }else{
                Result.failure(RuntimeException("response status is ${search(key).msg}"))
            }
        }catch (e:Exception){
            Result.failure<ArrayList<Song>>(e)
        }
        emit(result)
    }

5.歌词显示界面,用LyricViewX类实现歌词实时滚动显示

lyricViewX.loadLyric(mediaInformation.now_song.lrc?.trimIndent())
        lyricViewX.setDraggable(true, object : OnPlayClickListener {
            override fun onPlayClick(time: Long): Boolean {
                lyricViewX.updateTime(time)
                mediaInformation.mediaPlayer.seekTo(time.toInt())
                return true
            }
        })

​​

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值