Skip to content Skip to sidebar Skip to footer

Kotlin Mediaplayer Simple Usage

I'm new to Kotlin and am trying to make a simple AudioManager (wrapping MediaPlayer). I want the class to play the audio. Here is my class: package com.example.myappname import an

Solution 1:

Import Context into AudioManager:

import android.content.Context

Modify class or it's method signature like this:

classAudioManager(privateval context: Context): AudioManagerInput

Now we can pass context to MediaPlayer:

overridefunstartSound() {
    if (mediaPlayer == null) {
        mediaPlayer = MediaPlayer.create(context, R.raw.yourSound);
    }
    mediaPlayer?.start()
}

To init your AudioManager from an Activity:

var audioManager = AudioManager(this)

To manually access raw files: Read/write from res/raw by name.

Post a Comment for "Kotlin Mediaplayer Simple Usage"