Commit 7f7c465b by 范玉宾

meditation play float view done

parent a3309601
...@@ -27,8 +27,6 @@ class FloatViewTouchListener(val wl: WindowManager.LayoutParams, val windowManag ...@@ -27,8 +27,6 @@ class FloatViewTouchListener(val wl: WindowManager.LayoutParams, val windowManag
x += movedX x += movedX
y += movedY y += movedY
} }
LogUtil.d("Lancet=======","nowY=====$nowY")
LogUtil.d("Lancet=======","movedY=====$movedY")
//更新悬浮窗位置 //更新悬浮窗位置
windowManager.updateViewLayout(view, wl) windowManager.updateViewLayout(view, wl)
} }
......
...@@ -4,13 +4,18 @@ import android.content.Context ...@@ -4,13 +4,18 @@ import android.content.Context
import android.media.AudioAttributes import android.media.AudioAttributes
import android.media.AudioManager import android.media.AudioManager
import android.media.MediaPlayer import android.media.MediaPlayer
import com.yidianling.muse.helper.MediaPlayerManager.MediaPlayCallBack.Companion.TYPE_LIST
import kotlin.properties.Delegates import kotlin.properties.Delegates
class MediaPlayerManager private constructor() { class MediaPlayerManager private constructor() {
private var listener:OnMediaPlayerManagerListener?=null private var listener:OnMediaPlayerManagerListener?=null
fun setAudioPath(path: String){ private var mCurrentListIndex = 0
private var mMediaPlayCallBack:MediaPlayCallBack?=null
private var mAudioPaths = mutableListOf<String>()
fun setAudioPath(path: String,isLoop:Boolean = false){
val attrs = AudioAttributes.Builder() val attrs = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
...@@ -22,10 +27,34 @@ class MediaPlayerManager private constructor() { ...@@ -22,10 +27,34 @@ class MediaPlayerManager private constructor() {
mMediaPlayer?.reset() mMediaPlayer?.reset()
mMediaPlayer?.setAudioAttributes(attrs) mMediaPlayer?.setAudioAttributes(attrs)
mMediaPlayer?.setDataSource(path) mMediaPlayer?.setDataSource(path)
mMediaPlayer?.isLooping = isLoop
mMediaPlayer?.prepareAsync() mMediaPlayer?.prepareAsync()
mMediaPlayer?.setOnPreparedListener { mMediaPlayer?.setOnPreparedListener {
listener?.onPrepared(it) listener?.onPrepared(it)
} }
// mMediaPlayer?.setOnCompletionListener {
// if (it !=null){
// it.stop()
// it.release()
// }
//
// mMediaPlayer = null
// if (mAudioPaths!=null){
// mCurrentListIndex++
// if(mCurrentListIndex<mAudioPaths.size){
// mMediaPlayCallBack?.mediaPlayCallBack(TYPE_LIST,
// MediaPlayCallBack.STATE_CUT, mCurrentListIndex)
// setAudioPath(mAudioPaths[mCurrentListIndex])
// }else{
// mMediaPlayCallBack?.mediaPlayCallBack(TYPE_LIST,
// MediaPlayCallBack.STATE_STOP,mAudioPaths.size
// )
// }
// }
//
// }
} }
fun getMediaPlayer():MediaPlayer? = mMediaPlayer fun getMediaPlayer():MediaPlayer? = mMediaPlayer
...@@ -59,6 +88,22 @@ class MediaPlayerManager private constructor() { ...@@ -59,6 +88,22 @@ class MediaPlayerManager private constructor() {
this.listener = listener this.listener = listener
} }
fun startPlayList(audioPaths:ArrayList<String>,
playCallBack: MediaPlayCallBack,startIndex:Int){
if(audioPaths.isNullOrEmpty() || startIndex>=mAudioPaths.size){
return
}
mMediaPlayCallBack = playCallBack
mAudioPaths = audioPaths
mCurrentListIndex = startIndex
mMediaPlayCallBack?.mediaPlayCallBack(TYPE_LIST,
MediaPlayCallBack.STATE_START,mCurrentListIndex)
setAudioPath(mAudioPaths[mCurrentListIndex])
}
companion object { companion object {
private var mContext: Context by Delegates.notNull() private var mContext: Context by Delegates.notNull()
...@@ -90,4 +135,23 @@ class MediaPlayerManager private constructor() { ...@@ -90,4 +135,23 @@ class MediaPlayerManager private constructor() {
fun onPrepared(mediaPlayer: MediaPlayer) fun onPrepared(mediaPlayer: MediaPlayer)
} }
interface MediaPlayCallBack {
companion object{
const val STATE_START = 0
const val STATE_PLAY = 1
const val STATE_PAUSE = 2
const val STATE_STOP = 3
const val STATE_CUT = 4
const val TYPE_SINGLE = 0
const val TYPE_LIST = 1
}
fun mediaPlayCallBack(type:Int,state:Int,position:Int){}
}
} }
\ No newline at end of file
package com.yidianling.muse.service package com.yidianling.muse.service
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.graphics.PixelFormat import android.graphics.PixelFormat
...@@ -13,6 +17,8 @@ import android.view.ViewGroup.LayoutParams.WRAP_CONTENT ...@@ -13,6 +17,8 @@ import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.view.WindowManager import android.view.WindowManager
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.core.app.NotificationCompat
import androidx.core.content.getSystemService
import androidx.lifecycle.LifecycleService import androidx.lifecycle.LifecycleService
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.ydl.ydlcommon.data.http.ThrowableConsumer import com.ydl.ydlcommon.data.http.ThrowableConsumer
...@@ -51,6 +57,18 @@ class MeditationWindowService : LifecycleService() { ...@@ -51,6 +57,18 @@ class MeditationWindowService : LifecycleService() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel("壹点灵","play",NotificationManager.IMPORTANCE_LOW)
manager.createNotificationChannel(channel)
val notification = NotificationCompat.Builder(this,"壹点灵").build()
startForeground(1,notification)
}
}
EventBus.getDefault().register(this) EventBus.getDefault().register(this)
} }
...@@ -199,17 +217,35 @@ class MeditationWindowService : LifecycleService() { ...@@ -199,17 +217,35 @@ class MeditationWindowService : LifecycleService() {
if (floatRootView != null && floatRootView?.windowToken != null) { if (floatRootView != null && floatRootView?.windowToken != null) {
if (mWindowManager != null) { if (mWindowManager != null) {
windowIsShow = false windowIsShow = false
mTimer.cancel()
mWindowManager.removeViewImmediate(floatRootView) mWindowManager.removeViewImmediate(floatRootView)
} }
} }
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
startForegroundService(intent)
}else{
startService(intent)
}
}
return super.onStartCommand(intent, flags, startId)
}
override fun startForegroundService(service: Intent?): ComponentName? {
return super.startForegroundService(service)
}
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
if (EventBus.getDefault().isRegistered(this)) { if (EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().unregister(this) EventBus.getDefault().unregister(this)
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
stopForeground(true)
}
} }
private fun dp2px(dp: Float): Int { private fun dp2px(dp: Float): Int {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/bg_play_meditation_float_view" android:background="@drawable/play_float_background"
android:layout_marginHorizontal="16dp" android:layout_marginHorizontal="16dp"
android:layout_marginBottom="20dp" android:layout_marginBottom="20dp"
android:layout_width="match_parent" android:layout_width="match_parent"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment