Skip to content

Commit

Permalink
Merge pull request #1 from kkostov/add-play-remote-audio
Browse files Browse the repository at this point in the history
Add play remote audio
  • Loading branch information
kkostov authored Oct 30, 2024
2 parents 6a38802 + b70fe9e commit c527a93
Show file tree
Hide file tree
Showing 20 changed files with 198 additions and 100 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ jobs:
os: macos-latest
- target: jvmTest
os: ubuntu-latest
- target: linuxX64Test
os: ubuntu-latest
- target: testDebugUnitTest
os: ubuntu-latest
- target: testReleaseUnitTest
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
with:
distribution: 'zulu'
java-version: 21
- name: Set release version
run: bash ./setversion.sh ${{ github.ref_name }} gadulka/build.gradle.kts
- name: Publish to MavenCentral
run: ./gradlew publishToMavenCentral --no-configuration-cache
env:
Expand Down
26 changes: 24 additions & 2 deletions gadulka/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.androidLibrary)
alias(libs.plugins.vanniktech.mavenPublish)
alias(libs.plugins.osdetector)
}

group = "eu.iamkonstantin.kotlin"
version = "1.0.0"
version = "1.0.5"

kotlin {
jvm()
Expand All @@ -23,7 +24,6 @@ kotlin {
iosX64()
iosArm64()
iosSimulatorArm64()
linuxX64()

sourceSets {
val commonMain by getting {
Expand All @@ -36,6 +36,28 @@ kotlin {
implementation(libs.kotlin.test)
}
}
val jvmMain by getting {
dependencies {
val fxSuffix = when (osdetector.classifier) {
"linux-x86_64" -> "linux"
"linux-aarch_64" -> "linux-aarch64"
"windows-x86_64" -> "win"
"osx-x86_64" -> "mac"
"osx-aarch_64" -> "mac-aarch64"
else -> throw IllegalStateException("Unknown OS: ${osdetector.classifier}")
}
implementation("org.openjfx:javafx-base:19:${fxSuffix}")
implementation("org.openjfx:javafx-graphics:19:${fxSuffix}")
implementation("org.openjfx:javafx-controls:19:${fxSuffix}")
implementation("org.openjfx:javafx-swing:19:${fxSuffix}")
implementation("org.openjfx:javafx-web:19:${fxSuffix}")
implementation("org.openjfx:javafx-media:19:${fxSuffix}")
}
}

androidMain.dependencies {
implementation(libs.androix.media3.exploplayer)
}
}
}

Expand Down
53 changes: 53 additions & 0 deletions gadulka/src/androidMain/kotlin/GadulkaAudioPlayer.android.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package eu.iamkonstantin.kotlin.gadulka

import android.media.MediaPlayer

import android.content.Context
import androidx.media3.common.MediaItem
import androidx.media3.exoplayer.ExoPlayer

//
//actual class AudioPlayer(private val context: Context) {
//
// private val mediaPlayer = ExoPlayer.Builder(context).build()
// private val mediaItems = soundResList.map {
// MediaItem.fromUri(Res.getUri(it))
// }
//
// init {
// mediaPlayer.prepare()
// }
//
// @OptIn(ExperimentalResourceApi::class)
// actual fun playSound(id: Int) {
// mediaPlayer.setMediaItem(mediaItems[id])
// mediaPlayer.play()
// }
//
// actual fun release() {
// mediaPlayer.release()
// }
//}
//
actual class GadulkaPlayer(private val context: Context) {
private val mediaPlayer = ExoPlayer.Builder(context).build()

init {
mediaPlayer.prepare()
}

actual fun play(url: String) {
if (mediaPlayer.isPlaying) mediaPlayer.pause()
val mediaItem = MediaItem.fromUri(url)
mediaPlayer.setMediaItem(mediaItem)
mediaPlayer.play()
}

actual fun release() {
mediaPlayer.release()
}

actual fun stop() {
mediaPlayer.pause()
}
}
4 changes: 0 additions & 4 deletions gadulka/src/androidMain/kotlin/fibiprops.android.kt

This file was deleted.

12 changes: 0 additions & 12 deletions gadulka/src/androidUnitTest/kotlin/AndroidFibiTest.kt

This file was deleted.

17 changes: 0 additions & 17 deletions gadulka/src/commonMain/kotlin/CustomFibi.kt

This file was deleted.

9 changes: 9 additions & 0 deletions gadulka/src/commonMain/kotlin/GadulkaAudioPlayer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package eu.iamkonstantin.kotlin.gadulka

expect class GadulkaPlayer {
fun play(url: String)

fun stop()

fun release()
}
12 changes: 0 additions & 12 deletions gadulka/src/commonTest/kotlin/FibiTest.kt

This file was deleted.

25 changes: 25 additions & 0 deletions gadulka/src/iosMain/kotlin/GadulkaAudioPlayer.ios.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package eu.iamkonstantin.kotlin.gadulka

import platform.AVFoundation.*
import platform.Foundation.NSURL

actual class GadulkaPlayer {
private var player: AVPlayer? = null

actual fun play(url: String) {
release()

val nsUrl = NSURL(string = url)
player = AVPlayer.playerWithURL(nsUrl)
player?.play()
}

actual fun release() {
player?.pause()
player = null
}

actual fun stop() {
player?.pause()
}
}
4 changes: 0 additions & 4 deletions gadulka/src/iosMain/kotlin/fibiprops.ios.kt

This file was deleted.

12 changes: 0 additions & 12 deletions gadulka/src/iosTest/kotlin/IosFibiTest.kt

This file was deleted.

49 changes: 49 additions & 0 deletions gadulka/src/jvmMain/kotlin/GadulkaAudioPlayer.jvm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package eu.iamkonstantin.kotlin.gadulka

import javafx.application.Platform
import javafx.embed.swing.JFXPanel
import javafx.scene.media.Media
import javafx.scene.media.MediaPlayer
import java.net.URI

actual class GadulkaPlayer {
var playerState: MediaPlayer? = null

init {
// Ensure JavaFX runtime is initialized
JFXPanel()
}

actual fun play(url: String) {
release()

Platform.runLater {
try {
val media = Media(URI(url).toString())
playerState = MediaPlayer(media).apply {
setOnReady {
println("Ready, playing..")
play()
}
setOnEndOfMedia {
println("End of media")
}
setOnError {
println("Error occurred: ${this.error?.message}")
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}

actual fun release() {
playerState?.stop()
playerState = null
}

actual fun stop() {
playerState?.stop()
}
}
4 changes: 0 additions & 4 deletions gadulka/src/jvmMain/kotlin/fibiprops.jvm.kt

This file was deleted.

12 changes: 0 additions & 12 deletions gadulka/src/jvmTest/kotlin/JvmFibiTest.kt

This file was deleted.

4 changes: 0 additions & 4 deletions gadulka/src/linuxX64Main/kotlin/fibiprops.linuxX64.kt

This file was deleted.

12 changes: 0 additions & 12 deletions gadulka/src/linuxX64Test/kotlin/LinuxFibiTest.kt

This file was deleted.

2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ kotlin.mpp.enableCInteropCommonization=true
#Android
android.useAndroidX=true
android.nonTransitiveRClass=true
#iOS
kotlin.apple.xcodeCompatibility.nowarn=true
10 changes: 7 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
[versions]
agp = "8.5.0"
kotlin = "2.0.20"
agp = "8.5.2"
kotlin = "2.0.21"
android-minSdk = "24"
android-compileSdk = "34"
media3_version = "1.4.1"

[libraries]
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
androix-media3-exploplayer = { group = "androidx.media3", name = "media3-exoplayer", version.ref = "media3_version" }


[plugins]
androidLibrary = { id = "com.android.library", version.ref = "agp" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
vanniktech-mavenPublish = { id = "com.vanniktech.maven.publish", version = "0.29.0" }
vanniktech-mavenPublish = { id = "com.vanniktech.maven.publish", version = "0.29.0" }
osdetector = { id = "com.google.osdetector", version = "1.7.3" }
27 changes: 27 additions & 0 deletions setversion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

# Sets and commits the version coordinate of the library based on the provided input.
# The script expects the version to be a valid semversion e.g. 1.0.0


# Check if two arguments are passed
if [ -z "$1" ] || [ -z "$2" ]; then
echo "No input provided. Usage: ./setversion.sh <semver> file/to/update"
exit 1
fi

# Assign the first argument to a variable
NEW_VERSION="$1"
FILE_PATH="$2"

echo "Updating veresion to '${NEW_VERSION}' in '${FILE_PATH}'."

if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS requires an empty string argument after -i to edit in place without backup
sed -i '' "s/^version = .*$/version = \"${NEW_VERSION}\"/g" "${FILE_PATH}"
else
# Linux
sed -i "/version = /c\version = \"${NEW_VERSION}\"" "${FILE_PATH}"
fi

echo "Version updated to ${NEW_VERSION}."

0 comments on commit c527a93

Please sign in to comment.