生活随笔
收集整理的這篇文章主要介紹了
Android中添加背景音乐的两种方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ? 前些天在嘗試自己寫一個Android小游戲——flybird
? ? ? ? 基本功能實現了,就想添加聲音,然后上網查了查,大多是一樣,可是用到我這,有些卻不可以用,所以我還用了兩種方法。
? ? ? ? 下面談談這兩種方法。
? ? ? ? 方法一:這是使用Java中的多線程,另外new出一個類,用類來啟動音樂。
? ? ? ? ? ? ? ? ? ? ?這個方法,就像當初寫java的小游戲一樣,不過是在電腦上運行的,可以控制每一個動作,比如你的觸碰動作,但是,在我這個游戲中,我需要的不是一點的音樂,雖 ? ? ? ? ? ? ? 然后期會用到,而是一開始就有的。
? ? ? ? ? ? ? ? ? ? ?不過,用這個方法,沒用service,就是開起來的時候,會很卡,而且會閃退。
[java] view plaincopy
package?com.example.flybird_anla;??????import?java.util.HashMap;??import?java.util.Map;??import?java.util.Random;????????import?android.content.Context;??import?android.media.AudioManager;??import?android.media.MediaPlayer;??import?android.media.SoundPool;??????public?class?SoundPlayer?{???????private?static?MediaPlayer?music;?????private?static?SoundPool?soundPool;???????????private?static?boolean?musicSt?=?true;??????private?static?boolean?soundSt?=?true;??????private?static?Context?context;???????????private?static?final?int[]?musicId?=?{R.raw.paopao};?????private?static?Map<Integer,Integer>?soundMap;????????????public?static?void?init(Context?c)?????{?????????context?=?c;???????????initMusic();???????????????????initSound();?????}????????????????private?static?void?initSound()?????{?????????soundPool?=?new?SoundPool(10,AudioManager.STREAM_MUSIC,100);???????????????????soundMap?=?new?HashMap<Integer,Integer>();?????????soundMap.put(R.raw.paopao,?soundPool.load(context,?R.raw.paopao,?1));?????????soundMap.put(R.raw.boung,?soundPool.load(context,?R.raw.boung,?1));?????}????????????????private?static?void?initMusic()?????{?????????int?r?=?new?Random().nextInt(musicId.length);?????????music?=?MediaPlayer.create(context,musicId[r]);?????????music.setLooping(true);?????}????????????????public?static?void?playSound(int?resId)?????{?????????if(soundSt?==?false)?????????????return;???????????????????Integer?soundId?=?soundMap.get(resId);?????????if(soundId?!=?null)?????????????soundPool.play(soundId,?1,?1,?1,?0,?1);?????}??????????????public?static?void?pauseMusic()?????{?????????if(music.isPlaying())?????????????music.pause();?????}??????????????????public?static?void?startMusic()?????{?????????if(musicSt)?????????????music.start();?????}??????????????????public?static?void?changeAndPlayMusic()?????{?????????if(music?!=?null)?????????????music.release();?????????initMusic();?????????startMusic();?????}???????????????????public?static?boolean?isMusicSt()?{?????????return?musicSt;?????}???????????????????public?static?void?setMusicSt(boolean?musicSt)?{?????????SoundPlayer.musicSt?=?musicSt;?????????if(musicSt)?????????????music.start();?????????else?????????????music.stop();?????}???????????????public?static?boolean?isSoundSt()?{?????????return?soundSt;?????}???????????????public?static?void?setSoundSt(boolean?soundSt)?{?????????SoundPlayer.soundSt?=?soundSt;?????}??????????????????public?static?void?boom()?????{?????????playSound(R.raw.boung);?????}??}??
? ? ? ? ? ? 然后,在需要的地方new出類,在調用方法。 ? ? ? ?
方法二:
? ? ? ? ? ? ?這種方法是利用android中的service來開啟,也就是進去的時候就播放出,退出就停止。
? ? ? ? ? ? ?首先,在AndroidManifest中的application節點下添加:
[java] view plaincopy
<activity??????????????android:name=".MainActivity"??????????????android:label="@string/app_name"?>??????????????<intent-filter>??????????????????<action?android:name="android.intent.action.MAIN"?/>????????????????????<category?android:name="android.intent.category.LAUNCHER"?/>??????????????</intent-filter>??????????</activity>??
? ? ? ? ? ? 然后,再在項目中new出一個類,MusicServer:
[java] view plaincopy
package?com.example.flybird_anla;????import?android.app.Service;??import?android.content.Intent;??import?android.media.MediaPlayer;??import?android.os.IBinder;????public?class?MusicServer?extends?Service?{????????private?MediaPlayer?mediaPlayer;????????@Override??????public?IBinder?onBind(Intent?intent)?{????????????????????return?null;??????}????????@Override??????public?void?onStart(Intent?intent,?int?startId)?{??????????super.onStart(intent,?startId);????????????if?(mediaPlayer?==?null)?{??????????????????????????????mediaPlayer?=?MediaPlayer.create(this,?R.raw.paopao);??????????????mediaPlayer.setLooping(true);??????????????mediaPlayer.start();????????????}??????}????????@Override??????public?void?onDestroy()?{????????????????????super.onDestroy();??????????mediaPlayer.stop();??????}??}??
最后,在在activity中,寫兩個函數:
[java] view plaincopy
protected?void?onCreate(Bundle?savedInstanceState)?{??????????Intent?intent?=?new?Intent(MainActivity.this,MusicServer.class);??????????super.onCreate(savedInstanceState);??????????startService(intent);??????????manu();??????}????????????protected?void?onStop(){??????????Intent?intent?=?new?Intent(MainActivity.this,MusicServer.class);??????????stopService(intent);??????????super.onStop();????????????????}??
? ? ? ? ?然后,就ok了。
總結
以上是生活随笔為你收集整理的Android中添加背景音乐的两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。