开始Start界面
开始界面只使用了两个控件,一个是ImageView,另一个是Button按钮
两个控件都有onclick事件
Button的点击事件
<span style="font-size:18px;">public void onclick(View v) { switch (v.getId()) { case R.id.start_bt: Intent intent = new Intent(StratActivity.this, MainActivity.class); startActivity(intent); break; } } </span>
接着就是写背景音乐服务
<span style="font-size:18px;">public class MyMusicService extends Service { // 声明操作媒体的 public MediaPlayer mediaPlayer; // 记录播放的位置 private int pos = 0; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub // 绑定时进行播放 System.out.println("AAAAAAAAAAAA"); playMusic(); return new MyBinder(); } public class MyBinder extends Binder { public MyMusicService getMyMusicService() { return MyMusicService.this; } } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); if (mediaPlayer == null) { mediaPlayer = MediaPlayer.create(MyMusicService.this, R.raw.background); // 设置单曲循环 mediaPlayer.setLooping(true); } // 播放完毕 进行监听回调 mediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub // 释放资源 mediaPlayer.release(); } }); }</span>然后就是来控制声音的暂停,播放,停止
<span style="font-size:18px;">// 播放音乐 public void playMusic() { // 如果mediaPlayer不为空 并且不是正在播放 if (mediaPlayer != null && !mediaPlayer.isPlaying()) { try { if (pos != 0) { mediaPlayer.seekTo(pos);// 根据指定的位置进行播放 mediaPlayer.start(); } else { mediaPlayer.stop(); mediaPlayer.prepare();// 启动之前必须 prepare mediaPlayer.start(); } } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } // 暂停音乐 public void pasueMusic() { if (mediaPlayer != null && mediaPlayer.isPlaying()) { pos = mediaPlayer.getCurrentPosition(); mediaPlayer.pause(); } } // 停止音乐 public void stopMusic() { if (mediaPlayer != null) { mediaPlayer.stop();// 停止 mediaPlayer.reset();// 资源重置 } } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub return super.onUnbind(intent); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); }</span>
完成声音的控制之后,便可以在开始界面调用该方法来控制音乐
public class MyServiceConn implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub MyBinder myBinder = (MyBinder) service; musicService = myBinder.getMyMusicService(); } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub conn = null; } }
ImageView的点击事件
<span style="font-size:18px;"> public void onclickImage(View v) { if (flag==0) { if (musicService == null) { Intent intent1 = new Intent(StratActivity.this, MyMusicService.class); System.out.println("ass"); bindService(intent1, conn, Context.BIND_AUTO_CREATE); } else { musicService.playMusic(); } flag=1; iv1.setImageResource(R.drawable.music); } else { if (musicService != null) { System.out.println("bbb"); musicService.stopMusic(); musicService = null; unbindService(conn); } flag=0; iv1.setImageResource(R.drawable.music1); } }</span>
<span style="font-size:18px;">R.drawable.music1 </span><pre name="code" class="java"><pre name="code" class="java"><span style="font-size:18px;">R.drawable.music</span>是两张相同的png图片,背景色透明,颜色不同,来区分ImageView是否点击
因为背景音乐暂停之后可以重新从头开始,所以只调用
<span style="font-size:18px;">MyMusicService</span>的stop和start
onCreate方法获取布局,并且判断开始的时候是否音乐开启,没有开启的话开启背景音乐,即背景音乐默认是开启状态
@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.start); am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); am.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI); iv1=(ImageView) findViewById(R.id.start_iv1); conn = new MyServiceConn(); if (musicService == null) { Intent intent1 = new Intent(StratActivity.this, MyMusicService.class); bindService(intent1, conn, Context.BIND_AUTO_CREATE); } else { musicService.playMusic(); }
其中
<span style="font-size:18px;">am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); am.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);</span>是用来控制调用音量键控制背景音乐大小
更多大家可以查看一下点击打开链接
双击退出程序,这个大家可以记一下,在很多地方都可以用到
// 双击返回键退出程序 /** * 菜单、返回键响应 */ @Override @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub switch (keyCode) { case KeyEvent.KEYCODE_BACK: exitBy2Click(); // 调用双击退出函数 break; case KeyEvent.KEYCODE_VOLUME_DOWN: am.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI); break; case KeyEvent.KEYCODE_VOLUME_UP: am.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI); break; case KeyEvent.KEYCODE_VOLUME_MUTE: am.setStreamMute(AudioManager.STREAM_MUSIC, true); break; default: break; } return false; } /** * 双击退出函数 */ private void exitBy2Click() { Timer tExit = null; if (isExit == false) { isExit = true; // 准备退出 Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show(); tExit = new Timer(); tExit.schedule(new TimerTask() { @Override public void run() { isExit = false; // 取消退出 } }, 2000); // 如果2秒钟内没有按下返回键,则启动定时器取消掉刚才执行的任务 } else { finish(); System.exit(0); } }
背景音乐开启状态
背景音乐开启状态
Main 界面功能的实现
<span style="font-size:18px;">public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onclick(View v) { switch (v.getId()) { case R.id.main_iv1: Intent intent1 = new Intent(MainActivity.this, EasyActivity.class); startActivity(intent1); break; case R.id.main_iv2: Intent intent2 = new Intent(MainActivity.this, SimpleActivity.class); startActivity(intent2); break; case R.id.main_iv3: Intent intent3 = new Intent(MainActivity.this, HardActivity.class); startActivity(intent3); break; default: break; } } }</span>