首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ACRCloud与安卓应用程序的集成

ACRCloud与安卓应用程序的集成
EN

Stack Overflow用户
提问于 2018-04-07 07:30:01
回答 1查看 453关注 0票数 0

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

代码语言:javascript
复制
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)

}

}

现在,当我启动服务时,我会收到以下错误:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-09 08:44:07

我检查了ACRCloudClient的实现及其扩展的安卓活动。另外,ACRCloudClient使用共享首选项(这就是为什么我得到一个空指针异常)。

因为在服务中保持对活动的引用并不是一个好主意,所以最好在活动中实现上面的代码。识别的所有实现都是在ACRCloudClient类中的一个单独线程中完成的,因此没有必要为此创建另一个服务。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49705028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档