Android3D游戏——添加背景音乐与音效
今天终于把主菜单做好了,虽然可能还有些小bug,不过在以后实现栏目功能的时候,应该都能处理好。
今天讲的是添加背景音乐和音效,做游戏怎么能没有声音呢,对不对。
一般添加声音是靠两种方法实现的。一种是MediaPlayer,一种是SoundPool。这两种方法的区别是MediaPlayer可以存储较大的音频文件,载入稍慢,不支持同时播放。而SoundPool只能存储较小的音频文件,载入较快,支持同时播放。这样就很显然了,MediaPlayer用来实现背景音乐,而SoundPool用来实现音效。
下面是我写的两个类,直接加到游戏中就可以用了,由于我开发的是小型的游戏,对声音处理的要求不高,更多的功能也没有写,不过基本功能都在了。
- package my.Game.audio;
- import java.io.IOException;
- import com.threed.jpct.Logger;
- import Android.app.Activity;
- import android.content.res.AssetFileDescriptor;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- public class MyMusic {
- protected static MediaPlayer mediaPlayer;
- protected static boolean mustResume = false;
- private static Activity act;
- public MyMusic(Activity act){
- mediaPlayer = new MediaPlayer();
- MyMusic.act=act;
- }
- public static void prepareMediaPlayer(String file) {
- if(mediaPlayer == null) {
- mediaPlayer = new MediaPlayer();
- }
- AssetFileDescriptor afd = null;
- try {
- afd = act.getAssets().openFd("sound/"+file+".ogg");
- } catch (Exception e) {
- e.printStackTrace();
- Logger.log("Tried creating Music with missing asset ... " + file);
- System.exit(0);
- return;
- }
- try {
- mediaPlayer.reset();
- mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
- mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- afd.close();
- } catch (IOException e) {
- e.printStackTrace();
- Logger.log("Error setting data source in Music.play, IO exception");
- System.exit(0);
- return;
- } catch (IllegalStateException e) {
- e.printStackTrace();
- Logger.log("Error setting data source in Music.play, fail ...");
- return;
- }
- try {
- mediaPlayer.prepare();
- } catch (Exception e) {
- e.printStackTrace();
- Logger.log("Error preparing MediaPlayer");
- System.exit(0);
- return;
- }
- }
- public static void play(boolean loop) {
- if(mediaPlayer == null) {
- Logger.log("error:MediaPlayer hasn't prepared.");
- return;
- }
- mediaPlayer.setLooping(loop);
- mediaPlayer.start();
- }
- public static void stop() {
- if(mediaPlayer == null) {
- Logger.log("error:No mediaPlayer is playing.");
- return;
- }
- mediaPlayer.stop();
- }
- public static void pause() {
- if(mediaPlayer == null) {
- Logger.log("error:No mediaPlayer is playing.");
- return;
- }
- mediaPlayer.pause();
- }
- public static void start(){
- if(mediaPlayer == null) {
- Logger.log("error:No mediaPlayer is prepared.");
- return;
- }
- mediaPlayer.start();
- }
- public static void onPause() {
- if(mediaPlayer == null) {
- Logger.log("error:No mediaPlayer is playing.");
- return;
- }
- if(mediaPlayer.isPlaying()) {
- mediaPlayer.pause();
- mustResume = true;
- }
- }
- public static void onResume() {
- if(mediaPlayer == null) {
- Logger.log("error:No mediaPlayer is playing.");
- return;
- }
- if(mustResume) {
- mediaPlayer.start();
- mustResume = false;
- }
- }
- public static MediaPlayer getMediaPlayer() {
- return mediaPlayer;
- }
- }
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20