0
votes

I hope to pass a method which implement MediaRecorder.OnInfoListener.

At present I use Code A to pass the method which implement MediaRecorder.OnInfoListener.

Is there any other way to do it? maybe just like Code B.

Code A

class HomeViewModel(...) : AndroidViewModel(mApplication) {

    fun startRecord() {
        startRecord(savedfilename){
             mr, what, extra ->
                 if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
                     stopRecord()
                 }            
        }

    }

}


class RecordService : Service() {  

    fun startRecord(filename:String, mOnInfoListener: MediaRecorder.OnInfoListener){
       mRecorder = MediaRecorder()
        mRecorder?.let {    
            with(it) {                  
                setOnInfoListener(mOnInfoListener)    
            }
        }
    }    
}

Code B

class HomeViewModel(...) : AndroidViewModel(mApplication) {

{
    //I save  mOnInfoListener to memory ?   
    val mOnInfoListener = object: MediaRecorder.OnInfoListener{
        mr, what, extra ->
           if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
              stopRecord()
          }
    }

    fun startRecord() {
        startRecord(savedfilename)    
    }

}


class RecordService : Service() {  

    fun startRecord(filename:String){
       mRecorder = MediaRecorder()
        mRecorder?.let {    
            with(it) {
                setOnInfoListener(mOnInfoListener)  //  I get mOnInfoListener from memory ?    
            }
        }
    }    
}
Are you asking how to change Code B to make it compile?Tenfour04