我得到了开关和新布局,谢谢大家。最后一个问题。如果用户按下后面或主页按钮,我希望音乐停止播放。这是代码,我不知道我错过了什么。
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)
答案 0 :(得分:0)
您需要检查用户是关闭应用程序还是仅切换到另一个活动。 你可以查看我的答案
希望它有所帮助!!
修改强>
在ship
课程中检查这两种方法。
@Override
protected void onStop() {
Log.d(TAG, "App stopped");
super.onStop();
}
@Override
protected void onDestroy() {
Log.d(TAG, "App destoryed");
super.onDestroy();
}
答案 1 :(得分:-2)
在按键上使用
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
stop sound!
return true;
}
return super.onKeyUp(keyCode, event);
}