美文网首页
自己的APP控制系统的暂停播放上一曲下一曲(间接控制第三方音乐)

自己的APP控制系统的暂停播放上一曲下一曲(间接控制第三方音乐)

作者: 郭儿特洛夫斯基 | 来源:发表于2022-01-11 09:52 被阅读0次

自己的APP控制系统的暂停播放上一曲下一曲(间接控制第三方音乐,QQ音乐、网易云音乐)

package com.example.myapplication

import android.content.BroadcastReceiver

import android.content.Context

import android.media.AudioManager

import android.os.Bundle

import android.view.KeyEvent

import android.view.KeyEvent.ACTION_DOWN

import android.widget.Button

import androidx.appcompat.app.AppCompatActivity

import android.content.Intent

import android.os.SystemClock

import android.content.ComponentName

import android.support.v4.media.session.MediaControllerCompat

import android.support.v4.media.session.MediaSessionCompat

/**

*

* Create by Guo Tingzhu on 2022/1/10 at ShenZhen

*

*/

class MusicTestControlActivity : AppCompatActivity() {

private var eventtime: Long =0

    private lateinit var mAudioMg: AudioManager

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

mAudioMg = getSystemService(Context.AUDIO_SERVICE)as AudioManager

setContentView(R.layout.music_control_layout)

eventtime = SystemClock.uptimeMillis()

findViewById(R.id.btn_pause)?.setOnClickListener{

            if (eventtime <=0)return@setOnClickListener

            if (mAudioMg.isMusicActive) {

val downEvent =

KeyEvent(eventtime, eventtime, ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE, 0)

mAudioMg?.dispatchMediaKeyEvent(downEvent)

val upEvent =

KeyEvent(

eventtime,

                        eventtime,

                        KeyEvent.ACTION_UP,

                        KeyEvent.KEYCODE_MEDIA_PAUSE,

                        0

                    )

mAudioMg?.dispatchMediaKeyEvent(upEvent)

}

}

        findViewById(R.id.btn_play)?.setOnClickListener{

            if (eventtime <=0)return@setOnClickListener

            val downEvent =

KeyEvent(eventtime, eventtime, ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY, 0)

mAudioMg?.dispatchMediaKeyEvent(downEvent)

val upEvent =

KeyEvent(

eventtime,

                    eventtime,

                    KeyEvent.ACTION_UP,

                    KeyEvent.KEYCODE_MEDIA_PLAY,

                    0

                )

mAudioMg?.dispatchMediaKeyEvent(upEvent)

}

        findViewById(R.id.btn_high)?.setOnClickListener{

            mAudioMg.adjustStreamVolume(

AudioManager.STREAM_MUSIC,

                AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI

            )

}

        findViewById(R.id.btn_low)?.setOnClickListener{

            mAudioMg.adjustStreamVolume(

AudioManager.STREAM_MUSIC,

                AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI

            )

}

        findViewById(R.id.btn_prev)?.setOnClickListener{

            if (eventtime <=0)return@setOnClickListener

            val downEvent =

KeyEvent(eventtime, eventtime, ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0)

mAudioMg?.dispatchMediaKeyEvent(downEvent)

val upEvent =

KeyEvent(

eventtime,

                    eventtime,

                    KeyEvent.ACTION_UP,

                    KeyEvent.KEYCODE_MEDIA_PREVIOUS,

                    0

                )

mAudioMg?.dispatchMediaKeyEvent(upEvent)

}

        findViewById(R.id.btn_next)?.setOnClickListener{

            if (eventtime <=0)return@setOnClickListener

            val downEvent =

KeyEvent(eventtime, eventtime, ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0)

mAudioMg?.dispatchMediaKeyEvent(downEvent)

val upEvent =

KeyEvent(

eventtime,

                    eventtime,

                    KeyEvent.ACTION_UP,

                    KeyEvent.KEYCODE_MEDIA_NEXT,

                    0

                )

mAudioMg?.dispatchMediaKeyEvent(upEvent)

}

    }

}

相关文章

网友评论

      本文标题:自己的APP控制系统的暂停播放上一曲下一曲(间接控制第三方音乐)

      本文链接:https://www.haomeiwen.com/subject/gwrbcrtx.html