如何通过Kotlin中的参数除外的方法?
我希望通过一种实现mediarecorder.oninfolistener
的方法。
目前,我使用代码A传递实现Mediarecorder.oninfolistener
的方法。
还有其他方法吗?也许就像代码B一样。
代码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)
}
}
}
}
代码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 ?
}
}
}
}
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 ?
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码A
在将lambda作为参数传递方面看起来不错。但是我会做出一些改进。I would encapsulate the work with
MediaRecorder
in one class, egRecordService
,HomeViewModel
doesn't need to know anything about what toolsRecordService
用于制作记录。而且,如果您需要在录音机的事件上做出反应,则可以为此创建一个其他接口。带有所有接口和事件的示例代码看起来如下:最好使用接口而不是混凝土实现。另外,我不建议在命名变量时使用
m
前缀,它是 java 语言的样式,而不是 kotlin 。PS
Flow
可用于发射录音机的事件,而不是RecordserviceEventListener
,但这是另一个故事:)The
Code A
looks good in terms of passing a lambda as parameter. But I would make a couple of improvements.I would encapsulate the work with
MediaRecorder
in one class, e.g.RecordService
,HomeViewModel
doesn't need to know anything about what toolsRecordService
uses to make records. And if you need to react on recorder's events you can create an additional interface for that. The sample code with all interfaces and events will look like the following:It is better to work with interfaces rather than with concrete implementations. Also I wouldn't suggest to use
m
prefix when naming variables, it is a style of Java language, not Kotlin.P.S.
Flow
can be used to emit events of a recorder instead ofRecordServiceEventListener
, but this is another story :)干得好:
Here you go: