gpt4 book ai didi

java - 通过按返回或主页按钮停止mediaplayer声音

转载 作者:行者123 更新时间:2023-12-03 02:34:18 24 4
gpt4 key购买 nike

我得到了切换和新的布局,谢谢大家。最后一个问题。如果用户按下“后退”或“主页”按钮,我希望音乐停止播放。这是代码,我不知道我缺少什么。

package com.androidsleepmachine.gamble;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class Ocean extends Activity implements View.OnClickListener {
public static final Integer[] TIME_IN_MINUTES = { 30, 45, 60, 180, 360 };
public MediaPlayer mediaPlayer;
public Handler handler = new Handler();
public Button button1;
public Spinner spinner1;

// Initialize the activity
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.ocean);

button1 = (Button) findViewById(R.id.btn1);
button1.setOnClickListener(this);
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,
android.R.layout.simple_spinner_item, TIME_IN_MINUTES);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
}

// Play the sound and start the timer
private void playSound(int resourceId) {
// Cleanup any previous sound files
cleanup();
// Create a new media player instance and start it
mediaPlayer = MediaPlayer.create(this, resourceId);
mediaPlayer.start();
// Create the timer to stop the sound after x number of milliseconds
int selectedTime = TIME_IN_MINUTES[spinner1.getSelectedItemPosition()];
handler.postDelayed(runnable, selectedTime * 60 * 1000);
}

// Handle button callback
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
playSound(R.raw.ocean_birds);
break;
}
}

// Stop the sound and cleanup the media player
public void cleanup() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
// Cancel any previously running tasks
handler.removeCallbacks(runnable);
}

// Runnable task used by the handler to stop the sound
public Runnable runnable = new Runnable() {
public void run() {
cleanup();
}
};
}

Logcat
09-18 00:28:59.000: E/AndroidRuntime(1627): android.app.SuperNotCalledException:  
Activity {com.androidsleepmachine.gamble/com.androidsleepmachine.gamble.Ship} did not
call through to super.onStop()
09-18 00:28:59.000: E/AndroidRuntime(1627): at
android.app.Activity.performStop(Activity.java:5148)
09-18 00:28:59.000: E/AndroidRuntime(1627): at
android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3232)
09-18 00:28:59.000: E/AndroidRuntime(1627): at
android.os.Handler.dispatchMessage(Handler.java:99)
09-18 00:28:59.000: E/AndroidRuntime(1627): at
android.os.Looper.loop(Looper.java:137)
09-18 00:28:59.000: E/AndroidRuntime(1627): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-18 00:28:59.000: E/AndroidRuntime(1627): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

最佳答案

您需要检查是用户关闭了应用程序还是只是切换到另一个 Activity 。
您可以查看我的答案

Answer1

Answer2

希望能帮助到你!!

编辑

ship类中检查这两种方法。

 @Override
protected void onStop() {
Log.d(TAG, "App stopped");

super.onStop();
}

@Override
protected void onDestroy() {
Log.d(TAG, "App destoryed");

super.onDestroy();
}

关于java - 通过按返回或主页按钮停止mediaplayer声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18863257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com