我正在为Flutter开发音频处理库,但遇到了我的原生Kotlin类之一的问题。在下面的代码示例中,编译器抱怨copyInto数组上的samples调用是未解析的引用。据我所知,我已经确保它是IntArray,并且当我键入“samples”时,Android Studio中的Intellisense甚至会显示copyInto作为选项。

这是我的类(class)代码:

package com.----.audio_channels

import kotlin.math.abs
import kotlin.math.min

abstract class AudioTrackBuffer constructor(val loop: Boolean) {
    var position: Int = 0

    abstract fun getSamples(sampleCount: Int): IntArray
    abstract fun isComplete(): Boolean
    abstract fun dispose()
}

class RawAudioBuffer constructor(private val samples: IntArray, loop: Boolean, delay: Int): AudioTrackBuffer(loop) {

    init {
        position = -delay
    }

    override fun getSamples(sampleCount: Int): IntArray {
        val slice: IntArray

        if (position < 0) {
            slice = IntArray(sampleCount) { 0 }

            if (position + sampleCount < 0) {
                position += sampleCount
                return slice
            }

            val offset = abs(position)
            samples.copyInto(slice, offset, 0, min(sampleCount - offset, samples.size)) // Error
        } else {
            slice = samples.copyOfRange(position, min(position + sampleCount, samples.size))
            position += slice.size
        }

        return slice
    }

    override fun isComplete(): Boolean {
        return position >= samples.size
    }

    override fun dispose() {}
}

这是编译错误:
Launching lib\main.dart on SM N960U in debug mode...
Initializing gradle...
Resolving dependencies...
Running Gradle task 'assembleDebug'...
e: E:\flutter\workspace\audio_channels\android\src\main\kotlin\com\----\audio_channels\AudioTrackBuffer.kt: (33, 21): Unresolved reference: copyInto

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':audio_channels:compileDebugKotlin'.
> Compilation error. See log for more details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
Finished with error: Gradle task assembleDebug failed with exit code 1

最佳答案

从kotlin copyInto开始可用的1.3函数。当前,您正在使用1.2.71,然后出现错误Unresolved reference: copyInto
因此,将您的应用kotlin版本更新为> = 1.3将解决您的问题

10-06 06:37