Skip to content

Commit

Permalink
Add back player code
Browse files Browse the repository at this point in the history
  • Loading branch information
Xwilarg committed Aug 6, 2024
1 parent 18f96c1 commit 3d6f669
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 60 deletions.
38 changes: 6 additions & 32 deletions android/app/src/main/java/com/example/euphonia/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.ComponentName
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.os.Handler
Expand All @@ -17,18 +16,17 @@ import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.content.getSystemService
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import androidx.media3.session.MediaController
import androidx.media3.session.SessionToken
import androidx.media3.ui.PlayerView
import com.example.euphonia.data.ExtendedSong
import com.example.euphonia.data.MusicData
import com.example.euphonia.data.Song
import com.google.common.util.concurrent.ListenableFuture
import com.google.common.util.concurrent.MoreExecutors
import com.google.gson.Gson
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
import java.net.URL
Expand Down Expand Up @@ -77,22 +75,6 @@ class MainActivity : AppCompatActivity() {
}
}

fun onPlay(v: View) {
if (controllerFuture.get().isPlaying) {
controllerFuture.get().pause()
} else {
controllerFuture.get().play()
}
}

fun onPrevious(v: View) {
controllerFuture.get().seekToPreviousMediaItem()
}

fun onNext(v: View) {
controllerFuture.get().seekToNextMediaItem()
}

fun onRandom(v: View) {
val filteredData = downloaded.filter { currentPlaylist == null || it.playlist == currentPlaylist }
val selectedMusics = filteredData.map { songToItem(data, it) }.shuffled().toMutableList()
Expand All @@ -117,20 +99,12 @@ class MainActivity : AppCompatActivity() {
controllerFuture.get().play()
}

// Get a default thumbnail when none is available
/*fun getDefaultThumbnail(): ByteArray {
val bmp = ResourcesCompat.getDrawable(getResources(), R.drawable.album, null)!!.toBitmap(512, 512)
val stream = ByteArrayOutputStream()
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream)
return stream.toByteArray()
}*/

fun songToItem(data: MusicData, song: Song): MediaItem {
val albumPath = data.albums[song.album]?.path
val builder = MediaMetadata.Builder()
if (song.album != null) {
builder.setArtist(song.artist)
builder.setArtworkUri(Uri.fromFile(File("${filesDir}/${currUrl}icon/${albumPath}")))
builder.setArtworkUri(Uri.parse("file:///${filesDir}/${currUrl}icon/${albumPath}"))
} else {
builder.setArtist(null)
builder.setArtworkUri(Uri.parse("https://${currUrl}img/CD.png"))
Expand Down Expand Up @@ -159,15 +133,15 @@ class MainActivity : AppCompatActivity() {
}

// Init media session
//val videoView = findViewById<PlayerView>(R.id.player)
val videoView = findViewById<PlayerView>(R.id.player)
val sessionToken = SessionToken(this, ComponentName(this, PlaybackService::class.java))
controllerFuture = MediaController.Builder(this, sessionToken).buildAsync()
/*controllerFuture.addListener(
controllerFuture.addListener(
{
videoView.player = controllerFuture.get()
},
MoreExecutors.directExecutor()
)*/
)

// Update JSON info
val executor: ExecutorService = Executors.newSingleThreadExecutor()
Expand Down
35 changes: 7 additions & 28 deletions android/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,16 @@

</ListView>

<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onPlay"
android:text="Play"
<androidx.media3.ui.PlayerView
android:id="@+id/player"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="V,1:1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline2" />

<Button
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onPrevious"
android:text="Prev."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline2" />
app:layout_constraintTop_toTopOf="@+id/guideline2">

<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onNext"
android:text="Next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintTop_toTopOf="@+id/guideline2" />
</androidx.media3.ui.PlayerView>

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 3d6f669

Please sign in to comment.