【问题标题】:How to add Sound Effects with Kotlin in Android如何在 Android 中使用 Kotlin 添加音效
【发布时间】:2020-12-03 12:24:38
【问题描述】:

你如何在 kotlin android 中编写声音效果 看遍了 Yt 和 Java

【问题讨论】:

  • 复制java代码并在android studio中按ctrl + shift + alt + k (Windows), cmd + shift + alt + k (Mac),它会将你的java转换成Kotlin
  • “添加音效”是什么意思?

标签: android kotlin soundeffect


【解决方案1】:

将您的音效文件添加/导入到您的项目中并像这样使用 MediaPlayer

私有变量 BgMusic:MediaPlayer? = 空

fun playBgMusic(){
    BgMusic = MediaPlayer.create(this, R.raw.gamebgmusic)
    BgMusic?.setOnPreparedListener{
        BgMusic?.start()
        musicPlaying = true
    }
    BgMusic?.setOnCompletionListener {
        BgMusic?.start()
    }
}

fun pauseBgMusic(){
    BgMusic!!.pause()
}

fun resumeBgMusic(){
    BgMusic!!.start()
}

【讨论】:

    【解决方案2】:

    将您的音效文件添加到应用项目的“资源”文件夹中,然后从您希望使用 SoundPool 执行的任何活动或片段中调用它,SoundPool 是一个公共类,可让您将音频资源加载到内存中玩吧!

    我将 SoundPool documentationblogpost 链接到此处,以显示它的实际效果。

    这里还有一篇 Kotlin tutorialspoint 文章。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 2016-07-16
      • 2014-04-03
      • 2021-08-21
      相关资源
      最近更新 更多