Skip to content

Commit

Permalink
bluetooth auto play
Browse files Browse the repository at this point in the history
  • Loading branch information
ZTFtrue committed Nov 8, 2024
1 parent 7a18b1c commit 19cfcfc
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
9 changes: 9 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" /> <!-- <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" /> -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <!-- <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" /> -->
<!-- <uses-permission android:name="android.permission.BLUETOOTH" />-->
<!-- <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />-->
<!-- <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />-->
<queries>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT" />
Expand Down Expand Up @@ -83,6 +87,11 @@
android:resource="@xml/file_paths"
android:value="androidx.startup" />
</provider>
<!-- <receiver android:name=".utils.BluetoothConnectionReceiver" android:exported="false">-->
<!-- <intent-filter>-->
<!-- <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />-->
<!-- </intent-filter>-->
<!-- </receiver>-->
</application>

</manifest>
13 changes: 11 additions & 2 deletions app/src/main/java/com/ztftrue/music/play/PlayService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package com.ztftrue.music.play
import android.annotation.SuppressLint
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.bluetooth.BluetoothDevice
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.BitmapFactory
import android.os.Bundle
import android.os.CountDownTimer
Expand Down Expand Up @@ -49,6 +51,7 @@ import com.ztftrue.music.sqlData.model.MainTab
import com.ztftrue.music.sqlData.model.MusicItem
import com.ztftrue.music.sqlData.model.PlayConfig
import com.ztftrue.music.sqlData.model.SortFiledData
import com.ztftrue.music.utils.BluetoothConnectionReceiver
import com.ztftrue.music.utils.PlayListType
import com.ztftrue.music.utils.SharedPreferencesUtils
import com.ztftrue.music.utils.Utils
Expand Down Expand Up @@ -177,11 +180,16 @@ class PlayService : MediaBrowserServiceCompat() {
var playCompleted = false
var needPlayPause = false
var sleepTime = 0L
var receiver: BluetoothConnectionReceiver? = null
private var countDownTimer: CountDownTimer? = null
override fun onCreate() {
super.onCreate()
// visualizationAudioProcessor = VisualizationAudioProcessor(mediaSession)
initExo(this@PlayService)
receiver = BluetoothConnectionReceiver(exoPlayer)
val filter = IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED)
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED)
filter.addAction(Intent.ACTION_HEADSET_PLUG)
registerReceiver(receiver, filter)
val contentIntent = Intent(this, MainActivity::class.java)
val pendingContentIntent = PendingIntent.getActivity(
this, 0, contentIntent,
Expand Down Expand Up @@ -1141,6 +1149,7 @@ class PlayService : MediaBrowserServiceCompat() {
exoPlayer.release()
} catch (_: Exception) {
}
unregisterReceiver(receiver)
super.onDestroy()
}

Expand Down Expand Up @@ -1281,7 +1290,7 @@ class PlayService : MediaBrowserServiceCompat() {
exoPlayer.setAudioAttributes(
AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
.setContentType(C.AUDIO_CONTENT_TYPE_MOVIE)
.setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
.build(),
true
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.ztftrue.music.utils

import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.media3.exoplayer.ExoPlayer

class BluetoothConnectionReceiver(private val exoPlayer: ExoPlayer) : BroadcastReceiver() {

override fun onReceive(context: Context?, intent: Intent?) {
Log.d("TAG", "onReceive")
if (intent == null) return
if (intent.action == BluetoothDevice.ACTION_ACL_CONNECTED) {
// Check if the connected device is your intended Bluetooth device (optional)
val device: BluetoothDevice? = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
if (device != null) {
Log.d(
"TAG",
"Connected to device: ${device.name}, ${device.address}, ${device.type}, ${device.alias}"
)
// Optionally, filter by device name or address
// if (device.name == "YourBluetoothDeviceName") {
// Autoplay logic
if (!exoPlayer.isPlaying) {
exoPlayer.playWhenReady = true
exoPlayer.prepare()
exoPlayer.play()
}
// }
}
} else if (intent.action == Intent.ACTION_HEADSET_PLUG) {
val state = intent.getIntExtra("state", -1)
val name = intent.getStringExtra("name")
val microphone = intent.getIntExtra("microphone", -1)
/**
* state - 0 for unplugged, 1 for plugged.
* name - Headset type, human readable string
* microphone - 1 if headset has a microphone, 0 otherwise
*/
when (state) {
0 -> {
// Headset is unplugged
Log.d("HeadsetPlugReceiver", "Headset unplugged")
}

1 -> {
// Headset is plugged in
if (!exoPlayer.isPlaying && microphone == 0) {
exoPlayer.playWhenReady = true
exoPlayer.prepare()
exoPlayer.play()
}
}

else -> {
// Unknown state
Log.d("HeadsetPlugReceiver", "Unknown headset state")
}
}
}

}
}

0 comments on commit 19cfcfc

Please sign in to comment.