ACRCloud 与 android 应用的集成

ACRCloud integration to android app

我有以下音乐识别代码。我正在使用意图服务来完成服务中的所有音乐识别。我已经完成了所有基本步骤,例如添加所有必需的权限并在项目中添加 ACRCloud android SDK。

class SongIdentifyService(discoverPresenter : DiscoverPresenter? = null) : IACRCloudListener , IntentService("SongIdentifyService") {

private val callback : SongIdentificationCallback? = discoverPresenter
private val mClient : ACRCloudClient by lazy { ACRCloudClient() }
private val mConfig : ACRCloudConfig by lazy { ACRCloudConfig() }
private var initState : Boolean = false
private var mProcessing : Boolean = false


override fun onHandleIntent(intent: Intent?) {

    Log.d("SongIdentifyService", "onHandeIntent called" )

    setUpConfig()
    addConfigToClient()

    if (callback != null) {
        startIdentification(callback)
    }

}


public fun setUpConfig(){

    Log.d("SongIdentifyService", "setupConfig called")

    this.mConfig.acrcloudListener = this@SongIdentifyService

    this.mConfig.host = "some-host"
    this.mConfig.accessKey = "some-accesskey"
    this.mConfig.accessSecret = "some-secret"
    this.mConfig.protocol = ACRCloudConfig.ACRCloudNetworkProtocol.PROTOCOL_HTTP // PROTOCOL_HTTPS
    this.mConfig.reqMode = ACRCloudConfig.ACRCloudRecMode.REC_MODE_REMOTE

}

// Called to start identifying/discovering the song that is currently playing
fun startIdentification(callback: SongIdentificationCallback)
{

    Log.d("SongIdentifyService", "startIdentification called")

    if(!initState)
    {
        Log.d("AcrCloudImplementation", "init error")
    }
    if(!mProcessing) {

        mProcessing = true
        if (!mClient.startRecognize()) {

            mProcessing = false
            Log.d("AcrCloudImplementation" , "start error")

        }
    }
}

// Called to stop identifying/discovering song
fun stopIdentification()
{

    Log.d("SongIdentifyService", "stopIdentification called")
    if(mProcessing)
    {
        mClient.stopRecordToRecognize()
    }

    mProcessing = false
}

fun cancelListeningToIdentifySong()
{
    if(mProcessing)
    {
        mProcessing = false
        mClient.cancel()
    }
}

fun addConfigToClient(){

    Log.d("SongIdentifyService", "addConfigToClient called")


    this.initState = this.mClient.initWithConfig(this.mConfig)

    if(this.initState)
    {
        this.mClient.startPreRecord(3000)
    }
}


override fun onResult(result: String?) {

    Log.d("SongIdentifyService", "onResult called")
    Log.d("SongIdentifyService",result)

    mClient.cancel()
    mProcessing = false

    val result = Gson().fromJson(result, SongIdentificationResult :: class.java)

    if(result.status.code == 3000)
    {
        callback!!.onOfflineError()
    }
    else if(result.status.code == 1001)
    {
        callback!!.onSongNotFound()
    }
    else if(result.status.code == 0 )
    {
        callback!!.onSongFound(MusicDataMapper().convertFromDataModel(result))

        //callback!!.onSongFound(Song("", "", ""))
    }
    else
    {
        callback!!.onGenericError()
    }


}

override fun onVolumeChanged(p0: Double) {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}


interface SongIdentificationCallback {

    // Called when the user is offline and music identification failed
    fun onOfflineError()

    // Called when a generic error occurs and music identification failed
    fun onGenericError()

    // Called when music identification completed but couldn't identify the song
    fun onSongNotFound()

    // Called when identification completed and a matching song was found
    fun onSongFound(song: Song)

}

}

现在,当我启动服务时,出现以下错误:

我检查了 ACRCloudClient 及其扩展的实现 android Activity。此外,ACRCloudClient 使用共享首选项(这就是我收到空指针异常的原因)。

由于在服务中保留对 activity 的引用不是一个好主意,因此最好在 activity 中实现上述代码。无论如何,识别的所有实现都是在 ACRCloudClient class 中的单独线程中完成的,因此没有必要为此创建另一个服务。