add android support
This commit is contained in:
3
android/oral-trainer-sdk/src/main/AndroidManifest.xml
Normal file
3
android/oral-trainer-sdk/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,3 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
</manifest>
|
||||
@@ -0,0 +1,48 @@
|
||||
package cn.learningpad.oraltrainer.sdk
|
||||
|
||||
import android.net.Uri
|
||||
|
||||
data class ImitationAssessmentRequest @JvmOverloads constructor(
|
||||
val mediaId: String,
|
||||
val sentence: SentenceBoundary,
|
||||
val recordingUri: Uri,
|
||||
val referenceAudioUri: Uri? = null,
|
||||
val locale: String? = null,
|
||||
val metadata: Map<String, String> = emptyMap(),
|
||||
)
|
||||
|
||||
data class ImitationAssessmentResult @JvmOverloads constructor(
|
||||
val overallScore: Float,
|
||||
val pronunciationScore: Float? = null,
|
||||
val fluencyScore: Float? = null,
|
||||
val completenessScore: Float? = null,
|
||||
val feedback: String? = null,
|
||||
val details: Map<String, String> = emptyMap(),
|
||||
)
|
||||
|
||||
fun interface CancellableAssessment {
|
||||
fun cancel()
|
||||
}
|
||||
|
||||
interface ImitationAssessmentCallback {
|
||||
fun onResult(result: ImitationAssessmentResult)
|
||||
|
||||
fun onError(error: Throwable)
|
||||
}
|
||||
|
||||
interface ImitationQualityAssessor {
|
||||
fun assess(
|
||||
request: ImitationAssessmentRequest,
|
||||
callback: ImitationAssessmentCallback,
|
||||
): CancellableAssessment
|
||||
}
|
||||
|
||||
object NoopImitationQualityAssessor : ImitationQualityAssessor {
|
||||
override fun assess(
|
||||
request: ImitationAssessmentRequest,
|
||||
callback: ImitationAssessmentCallback,
|
||||
): CancellableAssessment {
|
||||
callback.onError(UnsupportedOperationException("No imitation quality assessor is configured."))
|
||||
return CancellableAssessment {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,372 @@
|
||||
@file:androidx.media3.common.util.UnstableApi
|
||||
|
||||
package cn.learningpad.oraltrainer.sdk
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import androidx.media3.common.C
|
||||
import androidx.media3.common.MediaItem
|
||||
import androidx.media3.common.PlaybackException
|
||||
import androidx.media3.common.Player
|
||||
import androidx.media3.exoplayer.ExoPlayer
|
||||
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
|
||||
import java.util.concurrent.CopyOnWriteArraySet
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
class OralTrainerController internal constructor(
|
||||
context: Context,
|
||||
private val sdkConfig: OralTrainerSdkConfig,
|
||||
initialConfig: PlayerConfig,
|
||||
private val imitationAssessor: ImitationQualityAssessor,
|
||||
) {
|
||||
private val appContext = context.applicationContext
|
||||
private val listeners = CopyOnWriteArraySet<OralTrainerListener>()
|
||||
private val mediaItems = mutableListOf<TrainingMediaItem>()
|
||||
private val mainHandler = Handler(Looper.getMainLooper())
|
||||
private var released = false
|
||||
private var lastSentenceIndex: Int? = null
|
||||
|
||||
var config: PlayerConfig = initialConfig
|
||||
private set
|
||||
|
||||
var loopMode: LoopMode = LoopMode.ALL
|
||||
private set
|
||||
|
||||
val player: ExoPlayer
|
||||
|
||||
private val ticker = object : Runnable {
|
||||
override fun run() {
|
||||
if (released) {
|
||||
return
|
||||
}
|
||||
notifySentenceIfChanged()
|
||||
notifySnapshot()
|
||||
mainHandler.postDelayed(this, 250L)
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
val mediaSourceFactory = DefaultMediaSourceFactory(
|
||||
StreamingCache.dataSourceFactory(appContext, sdkConfig)
|
||||
)
|
||||
player = ExoPlayer.Builder(appContext)
|
||||
.setMediaSourceFactory(mediaSourceFactory)
|
||||
.build()
|
||||
.also { exoPlayer ->
|
||||
exoPlayer.repeatMode = Player.REPEAT_MODE_ALL
|
||||
exoPlayer.playWhenReady = initialConfig.autoPlay
|
||||
exoPlayer.addListener(object : Player.Listener {
|
||||
override fun onPlaybackStateChanged(playbackState: Int) {
|
||||
notifySnapshot()
|
||||
}
|
||||
|
||||
override fun onIsPlayingChanged(isPlaying: Boolean) {
|
||||
notifySnapshot()
|
||||
}
|
||||
|
||||
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
|
||||
lastSentenceIndex = null
|
||||
listeners.forEach { it.onMediaChanged(currentTrainingItem()) }
|
||||
notifySentenceIfChanged(force = true)
|
||||
notifySnapshot()
|
||||
}
|
||||
|
||||
override fun onPositionDiscontinuity(
|
||||
oldPosition: Player.PositionInfo,
|
||||
newPosition: Player.PositionInfo,
|
||||
reason: Int,
|
||||
) {
|
||||
notifySentenceIfChanged(force = true)
|
||||
notifySnapshot()
|
||||
}
|
||||
|
||||
override fun onPlayerError(error: PlaybackException) {
|
||||
listeners.forEach { it.onPlayerError(error) }
|
||||
}
|
||||
})
|
||||
}
|
||||
mainHandler.post(ticker)
|
||||
}
|
||||
|
||||
fun addListener(listener: OralTrainerListener) {
|
||||
listeners.add(listener)
|
||||
}
|
||||
|
||||
fun removeListener(listener: OralTrainerListener) {
|
||||
listeners.remove(listener)
|
||||
}
|
||||
|
||||
fun updateConfig(config: PlayerConfig) {
|
||||
this.config = config
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
fun loadCourse(
|
||||
course: TrainingCourse,
|
||||
startIndex: Int = 0,
|
||||
startPositionMs: Long = 0L,
|
||||
) {
|
||||
require(course.items.isNotEmpty()) { "TrainingCourse must contain at least one media item." }
|
||||
loadItems(course.items, startIndex, startPositionMs)
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
fun loadItems(
|
||||
items: List<TrainingMediaItem>,
|
||||
startIndex: Int = 0,
|
||||
startPositionMs: Long = 0L,
|
||||
) {
|
||||
require(items.isNotEmpty()) { "At least one TrainingMediaItem is required." }
|
||||
val safeIndex = startIndex.coerceIn(0, items.lastIndex)
|
||||
mediaItems.clear()
|
||||
mediaItems.addAll(items)
|
||||
lastSentenceIndex = null
|
||||
player.setMediaItems(items.map { it.toMedia3Item() }, safeIndex, max(0L, startPositionMs))
|
||||
player.prepare()
|
||||
player.playWhenReady = config.autoPlay
|
||||
listeners.forEach { it.onMediaChanged(currentTrainingItem()) }
|
||||
notifySentenceIfChanged(force = true)
|
||||
notifySnapshot()
|
||||
}
|
||||
|
||||
fun loadItem(item: TrainingMediaItem) {
|
||||
loadItems(listOf(item))
|
||||
}
|
||||
|
||||
fun play() {
|
||||
player.play()
|
||||
}
|
||||
|
||||
fun pause() {
|
||||
player.pause()
|
||||
}
|
||||
|
||||
fun togglePlayPause() {
|
||||
if (player.isPlaying) {
|
||||
pause()
|
||||
} else {
|
||||
play()
|
||||
}
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
player.stop()
|
||||
}
|
||||
|
||||
fun seekTo(positionMs: Long) {
|
||||
player.seekTo(max(0L, positionMs))
|
||||
}
|
||||
|
||||
fun seekRelative(deltaMs: Long) {
|
||||
seekTo(player.currentPosition + deltaMs)
|
||||
}
|
||||
|
||||
fun rewind(stepMs: Long = config.defaultSeekStepMs) {
|
||||
seekRelative(-max(0L, stepMs))
|
||||
}
|
||||
|
||||
fun fastForward(stepMs: Long = config.defaultSeekStepMs) {
|
||||
seekRelative(max(0L, stepMs))
|
||||
}
|
||||
|
||||
fun previousItem() {
|
||||
player.seekToPreviousMediaItem()
|
||||
}
|
||||
|
||||
fun nextItem() {
|
||||
player.seekToNextMediaItem()
|
||||
}
|
||||
|
||||
fun setPlaybackSpeed(speed: Float) {
|
||||
val clamped = min(config.maxPlaybackSpeed, max(config.minPlaybackSpeed, speed))
|
||||
player.setPlaybackSpeed(clamped)
|
||||
}
|
||||
|
||||
fun setVolume(volume: Float) {
|
||||
player.volume = volume.coerceIn(0f, 1f)
|
||||
}
|
||||
|
||||
fun setLoopMode(loopMode: LoopMode) {
|
||||
this.loopMode = loopMode
|
||||
player.repeatMode = when (loopMode) {
|
||||
LoopMode.OFF -> Player.REPEAT_MODE_OFF
|
||||
LoopMode.ONE -> Player.REPEAT_MODE_ONE
|
||||
LoopMode.ALL -> Player.REPEAT_MODE_ALL
|
||||
}
|
||||
}
|
||||
|
||||
fun setSentenceMode(enabled: Boolean) {
|
||||
config = config.copy(sentenceMode = enabled)
|
||||
}
|
||||
|
||||
fun currentTrainingItem(): TrainingMediaItem? {
|
||||
val index = player.currentMediaItemIndex
|
||||
return mediaItems.getOrNull(index)
|
||||
}
|
||||
|
||||
fun currentSentence(): SentenceBoundary? {
|
||||
val item = currentTrainingItem() ?: return null
|
||||
val position = player.currentPosition
|
||||
return item.sentences.lastOrNull { sentence ->
|
||||
position >= sentence.startMs && position < sentence.endMs
|
||||
}
|
||||
}
|
||||
|
||||
fun seekToPreviousSentence(): Boolean {
|
||||
val item = currentTrainingItem() ?: return false
|
||||
if (item.sentences.isEmpty()) {
|
||||
return false
|
||||
}
|
||||
val position = player.currentPosition
|
||||
val target = item.sentences
|
||||
.asReversed()
|
||||
.firstOrNull { it.startMs < position - PREVIOUS_SENTENCE_TOLERANCE_MS }
|
||||
?: item.sentences.first()
|
||||
seekTo(target.startMs)
|
||||
return true
|
||||
}
|
||||
|
||||
fun seekToNextSentence(): Boolean {
|
||||
val item = currentTrainingItem() ?: return false
|
||||
if (item.sentences.isEmpty()) {
|
||||
return false
|
||||
}
|
||||
val position = player.currentPosition
|
||||
val target = item.sentences.firstOrNull {
|
||||
it.startMs > position + NEXT_SENTENCE_TOLERANCE_MS
|
||||
} ?: return false
|
||||
seekTo(target.startMs)
|
||||
return true
|
||||
}
|
||||
|
||||
fun performSwipeAction(action: SwipeAction) {
|
||||
when (action) {
|
||||
SwipeAction.NONE -> Unit
|
||||
SwipeAction.REWIND -> rewind()
|
||||
SwipeAction.FORWARD -> fastForward()
|
||||
SwipeAction.PREVIOUS_SENTENCE -> seekToPreviousSentence()
|
||||
SwipeAction.NEXT_SENTENCE -> seekToNextSentence()
|
||||
SwipeAction.PREVIOUS_SENTENCE_OR_REWIND -> {
|
||||
if (!config.sentenceMode || !seekToPreviousSentence()) {
|
||||
rewind()
|
||||
}
|
||||
}
|
||||
SwipeAction.NEXT_SENTENCE_OR_FORWARD -> {
|
||||
if (!config.sentenceMode || !seekToNextSentence()) {
|
||||
fastForward()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun dispatchGesture(event: GestureEvent) {
|
||||
listeners.forEach { it.onGesture(event) }
|
||||
}
|
||||
|
||||
fun assessCurrentSentence(
|
||||
recordingUri: Uri,
|
||||
callback: ImitationAssessmentCallback,
|
||||
referenceAudioUri: Uri? = null,
|
||||
locale: String? = null,
|
||||
metadata: Map<String, String> = emptyMap(),
|
||||
): CancellableAssessment {
|
||||
val item = currentTrainingItem()
|
||||
?: return callback.unsupported("No media item is currently loaded.")
|
||||
val sentence = currentSentence()
|
||||
?: return callback.unsupported("No active sentence is available at the current position.")
|
||||
val request = ImitationAssessmentRequest(
|
||||
mediaId = item.id,
|
||||
sentence = sentence,
|
||||
recordingUri = recordingUri,
|
||||
referenceAudioUri = referenceAudioUri,
|
||||
locale = locale,
|
||||
metadata = metadata,
|
||||
)
|
||||
return imitationAssessor.assess(request, callback)
|
||||
}
|
||||
|
||||
fun snapshot(): PlaybackSnapshot {
|
||||
val item = currentTrainingItem()
|
||||
val duration = player.duration.takeUnless { it == C.TIME_UNSET } ?: -1L
|
||||
return PlaybackSnapshot(
|
||||
mediaId = item?.id,
|
||||
positionMs = max(0L, player.currentPosition),
|
||||
durationMs = duration,
|
||||
bufferedPositionMs = max(0L, player.bufferedPosition),
|
||||
isPlaying = player.isPlaying,
|
||||
playbackState = player.playbackState.toOralTrainerState(),
|
||||
playbackSpeed = player.playbackParameters.speed,
|
||||
sentenceIndex = currentSentence()?.index,
|
||||
)
|
||||
}
|
||||
|
||||
fun release() {
|
||||
if (released) {
|
||||
return
|
||||
}
|
||||
released = true
|
||||
mainHandler.removeCallbacks(ticker)
|
||||
listeners.clear()
|
||||
player.release()
|
||||
}
|
||||
|
||||
private fun notifySnapshot() {
|
||||
if (released) {
|
||||
return
|
||||
}
|
||||
val snapshot = snapshot()
|
||||
listeners.forEach { it.onPlaybackSnapshot(snapshot) }
|
||||
}
|
||||
|
||||
private fun notifySentenceIfChanged(force: Boolean = false) {
|
||||
val sentence = currentSentence()
|
||||
val index = sentence?.index
|
||||
if (!force && index == lastSentenceIndex) {
|
||||
return
|
||||
}
|
||||
lastSentenceIndex = index
|
||||
listeners.forEach { it.onSentenceChanged(sentence) }
|
||||
}
|
||||
|
||||
private fun TrainingMediaItem.toMedia3Item(): MediaItem {
|
||||
val builder = MediaItem.Builder()
|
||||
.setMediaId(id)
|
||||
.setUri(uri)
|
||||
mimeType?.let(builder::setMimeType)
|
||||
customCacheKey?.let(builder::setCustomCacheKey)
|
||||
subtitleUri?.let { uri ->
|
||||
builder.setSubtitleConfigurations(
|
||||
listOf(
|
||||
MediaItem.SubtitleConfiguration.Builder(uri)
|
||||
.setMimeType(subtitleMimeType)
|
||||
.setLanguage(subtitleLanguage)
|
||||
.setSelectionFlags(C.SELECTION_FLAG_DEFAULT)
|
||||
.build()
|
||||
)
|
||||
)
|
||||
}
|
||||
return builder.build()
|
||||
}
|
||||
|
||||
private fun Int.toOralTrainerState(): OralTrainerPlaybackState {
|
||||
return when (this) {
|
||||
Player.STATE_BUFFERING -> OralTrainerPlaybackState.BUFFERING
|
||||
Player.STATE_READY -> OralTrainerPlaybackState.READY
|
||||
Player.STATE_ENDED -> OralTrainerPlaybackState.ENDED
|
||||
else -> OralTrainerPlaybackState.IDLE
|
||||
}
|
||||
}
|
||||
|
||||
private fun ImitationAssessmentCallback.unsupported(message: String): CancellableAssessment {
|
||||
onError(IllegalStateException(message))
|
||||
return CancellableAssessment {}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val PREVIOUS_SENTENCE_TOLERANCE_MS = 250L
|
||||
const val NEXT_SENTENCE_TOLERANCE_MS = 150L
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package cn.learningpad.oraltrainer.sdk
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.view.GestureDetector
|
||||
import android.view.MotionEvent
|
||||
import android.widget.FrameLayout
|
||||
import androidx.media3.ui.PlayerView
|
||||
import kotlin.math.abs
|
||||
|
||||
class OralTrainerPlayerView @JvmOverloads constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
defStyleAttr: Int = 0,
|
||||
) : FrameLayout(context, attrs, defStyleAttr) {
|
||||
val playerView: PlayerView = PlayerView(context).apply {
|
||||
useController = false
|
||||
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
|
||||
}
|
||||
|
||||
private var controller: OralTrainerController? = null
|
||||
private var gestureControls = GestureControlsConfig()
|
||||
private val density = resources.displayMetrics.density
|
||||
|
||||
private val gestureDetector = GestureDetector(
|
||||
context,
|
||||
object : GestureDetector.SimpleOnGestureListener() {
|
||||
override fun onDown(e: MotionEvent): Boolean = true
|
||||
|
||||
override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
|
||||
val activeController = controller ?: return false
|
||||
if (!gestureControls.enabled || !gestureControls.tapTogglesPlayPause) {
|
||||
return false
|
||||
}
|
||||
activeController.togglePlayPause()
|
||||
activeController.dispatchGesture(GestureEvent(GestureKind.SINGLE_TAP))
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onFling(
|
||||
e1: MotionEvent?,
|
||||
e2: MotionEvent,
|
||||
velocityX: Float,
|
||||
velocityY: Float,
|
||||
): Boolean {
|
||||
val activeController = controller ?: return false
|
||||
val start = e1 ?: return false
|
||||
if (!gestureControls.enabled) {
|
||||
return false
|
||||
}
|
||||
val dx = e2.x - start.x
|
||||
val dy = e2.y - start.y
|
||||
val minDistancePx = gestureControls.minSwipeDistanceDp * density
|
||||
val minVelocityPx = gestureControls.minSwipeVelocityDpPerSecond * density
|
||||
if (abs(dx) < abs(dy) || abs(dx) < minDistancePx || abs(velocityX) < minVelocityPx) {
|
||||
return false
|
||||
}
|
||||
val isRight = dx > 0
|
||||
val action = if (isRight) {
|
||||
gestureControls.rightSwipeAction
|
||||
} else {
|
||||
gestureControls.leftSwipeAction
|
||||
}
|
||||
activeController.performSwipeAction(action)
|
||||
activeController.dispatchGesture(
|
||||
GestureEvent(
|
||||
kind = if (isRight) GestureKind.SWIPE_RIGHT else GestureKind.SWIPE_LEFT,
|
||||
action = action,
|
||||
)
|
||||
)
|
||||
return true
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
init {
|
||||
addView(playerView)
|
||||
isClickable = true
|
||||
isFocusable = true
|
||||
}
|
||||
|
||||
fun bind(controller: OralTrainerController) {
|
||||
this.controller = controller
|
||||
this.gestureControls = controller.config.gestureControls
|
||||
playerView.player = controller.player
|
||||
}
|
||||
|
||||
fun unbind() {
|
||||
playerView.player = null
|
||||
controller = null
|
||||
}
|
||||
|
||||
fun updateGestureControls(config: GestureControlsConfig) {
|
||||
gestureControls = config
|
||||
}
|
||||
|
||||
override fun onTouchEvent(event: MotionEvent): Boolean {
|
||||
return gestureDetector.onTouchEvent(event) || super.onTouchEvent(event)
|
||||
}
|
||||
|
||||
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
|
||||
return gestureDetector.onTouchEvent(ev) || super.dispatchTouchEvent(ev)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package cn.learningpad.oraltrainer.sdk
|
||||
|
||||
import android.content.Context
|
||||
|
||||
class OralTrainerSdk private constructor(
|
||||
context: Context,
|
||||
val config: OralTrainerSdkConfig,
|
||||
) {
|
||||
private val appContext = context.applicationContext
|
||||
|
||||
val cache: OralTrainerCache = OralTrainerCache(appContext, config)
|
||||
|
||||
@JvmOverloads
|
||||
fun createController(
|
||||
playerConfig: PlayerConfig = PlayerConfig(),
|
||||
imitationAssessor: ImitationQualityAssessor = NoopImitationQualityAssessor,
|
||||
): OralTrainerController {
|
||||
return OralTrainerController(
|
||||
context = appContext,
|
||||
sdkConfig = config,
|
||||
initialConfig = playerConfig,
|
||||
imitationAssessor = imitationAssessor,
|
||||
)
|
||||
}
|
||||
|
||||
fun release() {
|
||||
StreamingCache.release()
|
||||
}
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var instance: OralTrainerSdk? = null
|
||||
|
||||
@JvmStatic
|
||||
@JvmOverloads
|
||||
fun init(
|
||||
context: Context,
|
||||
config: OralTrainerSdkConfig = OralTrainerSdkConfig(),
|
||||
): OralTrainerSdk {
|
||||
return synchronized(this) {
|
||||
instance ?: OralTrainerSdk(context.applicationContext, config).also {
|
||||
instance = it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun get(): OralTrainerSdk {
|
||||
return instance ?: error("Call OralTrainerSdk.init(context) before using the SDK.")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package cn.learningpad.oraltrainer.sdk
|
||||
|
||||
import java.io.File
|
||||
|
||||
data class OralTrainerSdkConfig @JvmOverloads constructor(
|
||||
val cacheDirectory: File? = null,
|
||||
val maxCacheBytes: Long = 512L * 1024L * 1024L,
|
||||
val userAgent: String = "OralTrainerSdk/0.1.0",
|
||||
val connectTimeoutMs: Int = 15_000,
|
||||
val readTimeoutMs: Int = 30_000,
|
||||
)
|
||||
@@ -0,0 +1,42 @@
|
||||
package cn.learningpad.oraltrainer.sdk
|
||||
|
||||
data class PlaybackSnapshot(
|
||||
val mediaId: String?,
|
||||
val positionMs: Long,
|
||||
val durationMs: Long,
|
||||
val bufferedPositionMs: Long,
|
||||
val isPlaying: Boolean,
|
||||
val playbackState: OralTrainerPlaybackState,
|
||||
val playbackSpeed: Float,
|
||||
val sentenceIndex: Int?,
|
||||
)
|
||||
|
||||
enum class OralTrainerPlaybackState {
|
||||
IDLE,
|
||||
BUFFERING,
|
||||
READY,
|
||||
ENDED,
|
||||
}
|
||||
|
||||
data class GestureEvent(
|
||||
val kind: GestureKind,
|
||||
val action: SwipeAction? = null,
|
||||
)
|
||||
|
||||
enum class GestureKind {
|
||||
SINGLE_TAP,
|
||||
SWIPE_LEFT,
|
||||
SWIPE_RIGHT,
|
||||
}
|
||||
|
||||
interface OralTrainerListener {
|
||||
fun onPlaybackSnapshot(snapshot: PlaybackSnapshot) = Unit
|
||||
|
||||
fun onMediaChanged(item: TrainingMediaItem?) = Unit
|
||||
|
||||
fun onSentenceChanged(sentence: SentenceBoundary?) = Unit
|
||||
|
||||
fun onGesture(event: GestureEvent) = Unit
|
||||
|
||||
fun onPlayerError(error: Throwable) = Unit
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package cn.learningpad.oraltrainer.sdk
|
||||
|
||||
data class PlayerConfig @JvmOverloads constructor(
|
||||
val sentenceMode: Boolean = true,
|
||||
val defaultSeekStepMs: Long = 10_000L,
|
||||
val autoPlay: Boolean = false,
|
||||
val minPlaybackSpeed: Float = 0.5f,
|
||||
val maxPlaybackSpeed: Float = 2.0f,
|
||||
val gestureControls: GestureControlsConfig = GestureControlsConfig(),
|
||||
)
|
||||
|
||||
data class GestureControlsConfig @JvmOverloads constructor(
|
||||
val enabled: Boolean = true,
|
||||
val tapTogglesPlayPause: Boolean = true,
|
||||
val leftSwipeAction: SwipeAction = SwipeAction.PREVIOUS_SENTENCE_OR_REWIND,
|
||||
val rightSwipeAction: SwipeAction = SwipeAction.NEXT_SENTENCE_OR_FORWARD,
|
||||
val minSwipeDistanceDp: Float = 48f,
|
||||
val minSwipeVelocityDpPerSecond: Float = 160f,
|
||||
)
|
||||
|
||||
enum class SwipeAction {
|
||||
NONE,
|
||||
REWIND,
|
||||
FORWARD,
|
||||
PREVIOUS_SENTENCE,
|
||||
NEXT_SENTENCE,
|
||||
PREVIOUS_SENTENCE_OR_REWIND,
|
||||
NEXT_SENTENCE_OR_FORWARD,
|
||||
}
|
||||
|
||||
enum class LoopMode {
|
||||
OFF,
|
||||
ONE,
|
||||
ALL,
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
@file:androidx.media3.common.util.UnstableApi
|
||||
|
||||
package cn.learningpad.oraltrainer.sdk
|
||||
|
||||
import android.content.Context
|
||||
import androidx.media3.database.StandaloneDatabaseProvider
|
||||
import androidx.media3.datasource.DataSource
|
||||
import androidx.media3.datasource.cache.CacheDataSource
|
||||
import androidx.media3.datasource.cache.LeastRecentlyUsedCacheEvictor
|
||||
import androidx.media3.datasource.cache.SimpleCache
|
||||
import androidx.media3.datasource.DefaultHttpDataSource
|
||||
import java.io.File
|
||||
|
||||
internal object StreamingCache {
|
||||
private var databaseProvider: StandaloneDatabaseProvider? = null
|
||||
private var cache: SimpleCache? = null
|
||||
|
||||
@Synchronized
|
||||
fun get(context: Context, config: OralTrainerSdkConfig): SimpleCache {
|
||||
cache?.let { return it }
|
||||
val appContext = context.applicationContext
|
||||
val provider = StandaloneDatabaseProvider(appContext)
|
||||
val directory = config.cacheDirectory ?: File(appContext.cacheDir, "oral_trainer_media_cache")
|
||||
val evictor = LeastRecentlyUsedCacheEvictor(config.maxCacheBytes)
|
||||
return SimpleCache(directory, evictor, provider).also {
|
||||
databaseProvider = provider
|
||||
cache = it
|
||||
}
|
||||
}
|
||||
|
||||
fun dataSourceFactory(
|
||||
context: Context,
|
||||
config: OralTrainerSdkConfig,
|
||||
): DataSource.Factory {
|
||||
val upstreamFactory = DefaultHttpDataSource.Factory()
|
||||
.setUserAgent(config.userAgent)
|
||||
.setConnectTimeoutMs(config.connectTimeoutMs)
|
||||
.setReadTimeoutMs(config.readTimeoutMs)
|
||||
.setAllowCrossProtocolRedirects(true)
|
||||
|
||||
return CacheDataSource.Factory()
|
||||
.setCache(get(context, config))
|
||||
.setUpstreamDataSourceFactory(upstreamFactory)
|
||||
.setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun release() {
|
||||
cache?.release()
|
||||
cache = null
|
||||
databaseProvider = null
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun clear(context: Context, config: OralTrainerSdkConfig) {
|
||||
val appContext = context.applicationContext
|
||||
val directory = config.cacheDirectory ?: File(appContext.cacheDir, "oral_trainer_media_cache")
|
||||
val provider = databaseProvider ?: StandaloneDatabaseProvider(appContext)
|
||||
cache?.release()
|
||||
cache = null
|
||||
SimpleCache.delete(directory, provider)
|
||||
databaseProvider = null
|
||||
}
|
||||
}
|
||||
|
||||
class OralTrainerCache internal constructor(
|
||||
private val context: Context,
|
||||
private val config: OralTrainerSdkConfig,
|
||||
) {
|
||||
fun sizeBytes(): Long = StreamingCache.get(context, config).cacheSpace
|
||||
|
||||
fun clear() {
|
||||
StreamingCache.clear(context, config)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package cn.learningpad.oraltrainer.sdk
|
||||
|
||||
import android.net.Uri
|
||||
import androidx.media3.common.MimeTypes
|
||||
|
||||
data class SentenceBoundary @JvmOverloads constructor(
|
||||
val index: Int,
|
||||
val startMs: Long,
|
||||
val endMs: Long,
|
||||
val text: String? = null,
|
||||
) {
|
||||
init {
|
||||
require(index >= 0) { "Sentence index must be non-negative." }
|
||||
require(startMs >= 0) { "Sentence startMs must be non-negative." }
|
||||
require(endMs > startMs) { "Sentence endMs must be greater than startMs." }
|
||||
}
|
||||
}
|
||||
|
||||
data class TrainingMediaItem @JvmOverloads constructor(
|
||||
val id: String,
|
||||
val title: String,
|
||||
val uri: Uri,
|
||||
val sentences: List<SentenceBoundary> = emptyList(),
|
||||
val subtitleUri: Uri? = null,
|
||||
val subtitleMimeType: String = MimeTypes.APPLICATION_SUBRIP,
|
||||
val subtitleLanguage: String? = null,
|
||||
val mimeType: String? = null,
|
||||
val customCacheKey: String? = null,
|
||||
)
|
||||
|
||||
data class TrainingCourse(
|
||||
val id: String,
|
||||
val title: String,
|
||||
val items: List<TrainingMediaItem>,
|
||||
)
|
||||
Reference in New Issue
Block a user