kinda works

This commit is contained in:
Isaac Iwasaki 2024-09-26 01:43:56 +07:00
parent fdfb0aa679
commit b78a1cb407
8 changed files with 95 additions and 65 deletions

View file

@ -6,15 +6,17 @@ import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
@ -26,12 +28,14 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import usr.empty.player.ui.theme.PlayerTheme
@ -72,6 +76,7 @@ fun uriToPath(uri: Uri): String {
@Composable
fun MainLayout(modifier: Modifier = Modifier) {
val context = LocalContext.current
val notas = remember { mutableStateListOf<NotaDescriptor>() }
val pickAudioLauncher = rememberLauncherForActivityResult(
@ -92,12 +97,38 @@ fun MainLayout(modifier: Modifier = Modifier) {
}
Column(modifier.fillMaxSize()) {
Button(shape = RectangleShape, colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.secondaryContainer, contentColor = MaterialTheme.colorScheme.secondary
), modifier = Modifier.align(Alignment.CenterHorizontally), onClick = {
pickAudioLauncher.launch("audio/*")
}) {
Text("add track")
Row(
horizontalArrangement = Arrangement.Absolute.SpaceEvenly,
modifier = Modifier.fillMaxWidth(),
) {
Button(shape = RectangleShape, colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.secondaryContainer, contentColor = MaterialTheme.colorScheme.secondary
), modifier = Modifier, onClick = {
pickAudioLauncher.launch("audio/*")
}) {
Text("add track")
}
// VerticalDivider()
Button(shape = RectangleShape, colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.secondaryContainer, contentColor = MaterialTheme.colorScheme.secondary
), modifier = Modifier, onClick = {
context.startService(Intent(context, PlayerService::class.java).apply {
putExtra("check", "empty")
putExtra("type", "pause")
})
}) {
Text("| pause |")
}
Button(shape = RectangleShape, colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.secondaryContainer, contentColor = MaterialTheme.colorScheme.secondary
), modifier = Modifier, onClick = {
context.startService(Intent(context, PlayerService::class.java).apply {
putExtra("check", "empty")
putExtra("type", "play")
})
}) {
Text("| play >")
}
}
NotaList(notas)
}
@ -105,13 +136,20 @@ fun MainLayout(modifier: Modifier = Modifier) {
@Composable
fun NotaList(notas: List<NotaDescriptor>, modifier: Modifier = Modifier) {
val context = LocalContext.current
LazyColumn(
modifier = modifier,
) {
items(notas) { nota ->
Column(modifier = Modifier
.fillParentMaxWidth()
.clickable { }
.clickable {
context.startService(Intent(context, PlayerService::class.java).apply {
putExtra("check", "empty")
putExtra("type", "nota")
putExtra("nota", Json.encodeToString(nota))
})
}
.padding(4.dp)
.padding(start = 16.dp)) {
Text(
@ -121,7 +159,6 @@ fun NotaList(notas: List<NotaDescriptor>, modifier: Modifier = Modifier) {
nota.artist, fontSize = 12.sp
)
}
Log.d("nya", nota.toString())
}
}
}

View file

@ -0,0 +1,13 @@
package usr.empty.player
import androidx.media3.common.MediaItem
import androidx.media3.exoplayer.ExoPlayer
class Nota(val descriptor: NotaDescriptor) {
val mediaSource = MediaItem.fromUri(descriptor.source)
fun prepare(player: ExoPlayer) {
player.setMediaItem(mediaSource)
}
}

View file

@ -8,11 +8,14 @@ import androidx.media3.session.MediaSession
import androidx.media3.session.MediaSessionService
import com.google.common.util.concurrent.Futures
import com.google.common.util.concurrent.ListenableFuture
import kotlinx.serialization.json.Json
import java.util.concurrent.ConcurrentLinkedDeque
class PlayerService : MediaSessionService() {
private var mediaSession: MediaSession? = null
var player: ExoPlayer? = null
private lateinit var mediaSession: MediaSession
lateinit var player: ExoPlayer
private var virtualQueue = ConcurrentLinkedDeque<Nota>()
override fun onCreate() {
super.onCreate()
@ -20,26 +23,35 @@ class PlayerService : MediaSessionService() {
player = this
mediaSession = MediaSession.Builder(this@PlayerService, this).setCallback(MediaSessionCallback()).build()
}
Log.d("meow", "memememe")
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d("nya", "start!!!")
intent?.run {
if (getStringExtra("check") != "empty") return@run
handleCommand(getStringExtra("type")!!, this)
}
return super.onStartCommand(intent, flags, startId)
}
private fun start() {
private fun handleCommand(type: String, intent: Intent) {
when (type) {
"nota" -> {
virtualQueue.add(Nota(Json.decodeFromString<NotaDescriptor>(intent.getStringExtra("nota")!!)))
// virtualQueue.first.prepare(player)
virtualQueue.last.prepare(player)
player.prepare()
player.play()
Log.d("nya", virtualQueue.last.mediaSource.toString())
}
"pause" -> player.pause()
"play" -> player.play()
}
}
override fun onDestroy() {
player?.release()
mediaSession?.release()
player = null
mediaSession = null
Log.d("meow", "owowowow")
player.release()
mediaSession.release()
super.onDestroy()
}