generated from Kotlin/multiplatform-library-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from kkostov/feat/improvements
Experimental WASM support, Android improvements
- Loading branch information
Showing
8 changed files
with
3,035 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,50 @@ | ||
package eu.iamkonstantin.kotlin.gadulka | ||
|
||
/** | ||
* A minimalistic audio player | ||
* | ||
* | ||
* Example: | ||
* | ||
* ```kotlin | ||
val player = GadulkaPlayer() | ||
player.play(url = "...") | ||
player.stop() | ||
player.release() | ||
``` | ||
*/ | ||
expect class GadulkaPlayer { | ||
/** | ||
* Start playback of the audio resource at the provided [url]. | ||
* | ||
* ### Resource URI | ||
* | ||
* Can be a remote HTTP(s) url, or a `files` URI obtained via `Res.getUri("files/sample.mp3")`. | ||
* | ||
* Check the [JetBrains docs on how to store raw](https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-multiplatform-resources-usage.html#raw-files) files as part of multiplatform project resources. | ||
* | ||
* On Android, you can resolve the resource URI using something like: | ||
* | ||
* ```kotlin | ||
* val uri = Uri.Builder() | ||
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) | ||
.appendPath("${R.raw.name_of_your_resource}") | ||
.build().toString() | ||
* ``` | ||
* | ||
*/ | ||
fun play(url: String) | ||
|
||
/** | ||
* Stop playback. | ||
* | ||
* Note: If the player is currently not playing, this action has no effect. | ||
*/ | ||
fun stop() | ||
|
||
|
||
/** | ||
* Pause and attempts to perform cleanup in order to dispose of any player resources. | ||
* | ||
*/ | ||
fun release() | ||
} |
47 changes: 47 additions & 0 deletions
47
gadulka/src/wasmJsMain/kotlin/GadulkaAudioPlayer.wasmJs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package eu.iamkonstantin.kotlin.gadulka | ||
|
||
import kotlinx.browser.document | ||
import kotlinx.browser.window | ||
import kotlinx.dom.appendElement | ||
import kotlinx.dom.appendText | ||
import org.w3c.dom.Element | ||
import org.w3c.dom.HTMLAudioElement | ||
import org.w3c.dom.HTMLInputElement | ||
|
||
|
||
actual class GadulkaPlayer(val htmlId: String) { | ||
actual fun play(url: String) { | ||
release() | ||
document.body?.appendElement("audio") { | ||
this as HTMLAudioElement | ||
this.id = htmlId | ||
this.src = url | ||
} | ||
|
||
val playerEl = getPlayerElement() | ||
playerEl?.play() | ||
} | ||
|
||
/** | ||
* Stop playback. | ||
* | ||
* Note: the player element remains in the DOM. | ||
*/ | ||
actual fun stop() { | ||
val playerEl = getPlayerElement() | ||
playerEl?.pause() | ||
} | ||
|
||
/** | ||
* Stops playback and removes the player element from the DOM. | ||
*/ | ||
actual fun release() { | ||
val playerEl = getPlayerElement() | ||
playerEl?.pause() | ||
} | ||
|
||
private fun getPlayerElement(): HTMLAudioElement? { | ||
return document.getElementById(htmlId) as? HTMLAudioElement | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.