当我点击ImageButton
时发出声音。但我需要release()
所以我可以播放所有按钮声音。但是,当我点击两次按钮时,我的应用已停止。
这是我的代码:
ImageButton btnda = (ImageButton) findViewById(MyActivity.this, R.id.daa);
btnda.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final MediaPlayer soundda = MediaPlayer.create(DaftarHuruf.this);
soundda.start();
soundda.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
}
});
}
});
这里是从android监视器错误显示:
07-20 13:32:32.053 25594-25594/com.example.john.lontaraapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.john.lontaraapp, PID: 25594
java.lang.IllegalStateException
at android.media.MediaPlayer._start(Native Method)
at android.media.MediaPlayer.start(MediaPlayer.java:1177)
at com.example.john.lontaraapp.DaftarHuruf$8.onClick(DaftarHuruf.java:100)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19761)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:949)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:744)
答案 0 :(得分:0)
调用release()
会释放播放器及相关资源。如果你想再玩一次,你应该这样做:soundda = new MediaPlayer();
并再次初始化...所以设置监听器等等。
答案 1 :(得分:0)
移动你的
final MediaPlayer soundda = MediaPlayer.create(this, R.raw.da);
之前的onClick()
内的
soundda.start();
因此,无论您是否发布了MediaPlayer
,或者尚未创建this
,都需要先创建<YourActivityName>.this
。
同样不使用OnClickListener
,而是使用this
,因为您将在OnClickListener
内移动代码,而Context
将被视为func refreshData(completionHandler:@escaping (Bool)->() ) {
//I ignore some unrelated statements here
//....
//Reloading the database
ref.observe(.value, with: { snapshot in
var newItems: [Task] = []
self.num_of_tasks = Int(snapshot.childrenCount)
for item in snapshot.children {
let taskItem = Task(snapshot: item as! DataSnapshot)
newItems.append(taskItem!)
}
let merged = drafts + newItems
self.items = merged
self.tableView.reloadData()
completionHandler(true) //The line causes error
})
}
对象,并且你需要一个override func viewDidLoad() {
super.viewDidLoad()
self.refreshControl = UIRefreshControl()
self.refreshControl!.addTarget(self, action: #selector(refreshData),for: .valueChanged)
self.refreshControl!.attributedTitle = NSAttributedString(string: "Update the data")
refreshData{ _ in
self.refreshControl!.endRefreshing()
}
}
对象