在Android的游戏音效 [英] Game Sound effects in Android

查看:144
本文介绍了在Android的游戏音效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发Android的一个简单的游戏。我想添加声音效果为每个触摸 事件。不过,我有添加背景贯穿游戏的声音效果。但 如何才能增加微小的声音效果接触游戏的任何字符。为了更好地理解 以下是我的课的设计: 我有一个主要活动在那里的观点,我打电话给我的,扩展GameView类 surfaceView。对于bacground声音我刚刚创建的声音mainActivity然后叫 这GameView类波纹管:

I am developing a simple game in Android. I want to add sound effects for each of the touch events. However I have add background sound effect that runs throughout the game. But how can add tiny sound effect for touching any character of the game. For better understanding following is my class design : I have a main activity from where as view I'm calling my GameView class that extends surfaceView. For the bacground sound I just created the sound at mainActivity and then called that GameView class as bellow:

public class MainActivity extends Activity {
    MediaPlayer backgroundMusic;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
         backgroundMusic = MediaPlayer.create(MainActivity.this, R.raw.bg);


         backgroundMusic.setLooping(true);
         backgroundMusic.setVolume(10.0f, 3.0f);
         backgroundMusic.start();

        setContentView(new GameView(this));
    }
}

和以下是我的GameView类。我想在这里补充这一类的onTouchEvent音效波纹管:

And following is my GameView class. I want to add sound effect here in this class onTouchEvent as bellow:

public class GameView extends SurfaceView {
 @Override
 public boolean onTouchEvent(MotionEvent event) {
   //checking condition I want to give different sound here.

  }
}

我试图做它作为mainActivity(正在使用MediaPlayer.creat()),但它显示错误。 任何人都知道如何添加我的类设计的基础上,这样的声音效果?

I tried to do it as mainActivity (that is using MediaPlayer.creat() ), but it shows error. Anybody knows how to add such sound effect on the basis of my class design ?

推荐答案

对于像爆炸,钱币收藏品等短声的效果,最好是使用的Soundpool

For short sound effect like explosions, coin collections etc, it is better to use SoundPool.

您只需要创造一个良好的池:

You just need to create a sound pool :

SoundPool sp = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);

在棒棒糖及更高版本:

AudioAttributes attrs = new AudioAttributes.Builder()
        .setUsage(AudioAttributes.USAGE_GAME)
        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
        .build();
SoundPool sp = new SoundPool.Builder()
        .setMaxStreams(10)
        .setAudioAttributes(attrs)
        .build();

这创造了最大声池。 10的声音流(即有多少同时声音效果可在任一时刻播放),并使用 AudioManager.STREAM_MUSIC 的声音流。

This creates sound pool for max. of 10 sound streams (i.e. how many simultaneous sound effects can be played at any one time) and uses AudioManager.STREAM_MUSIC as sounds stream.

此外,请务必设置音量控制在活动,因此用户能够改变正确的流体积:

Be sure to also set the volume control in your Activity, so the user is able to change the volume of the proper stream:

setVolumeControlStream(AudioManager.STREAM_MUSIC);

比,你需要加载的声音效果到游泳池和给他们的标识符:

Than, you need to load sound effects into pool and give them their identifiers:

int soundIds[] = new int[10];
soundIds[0] = sp.load(context, R.raw.your_sound, 1);
//rest of sounds goes here

您需要通过上下文来加载方法,所以无论你做你的活动里面,或者得到的是从somwhere人。

You need to pass a context to load method, so either you do this inside your activity, or get is from somwhere else.

和最后一步来播放声音是调用播放方法:

And final step to play sound is to call play method:

sp.play(soundIds[0], 1, 1, 1, 0, 1.0);

参数是:

  • soundID 的由负载()函数返回一个soundID

  • soundID a soundID returned by the load() function

leftVolume 的左侧音量值(范围= 0.0〜1.0)

leftVolume left volume value (range = 0.0 to 1.0)

rightVolume 的右侧音量值(范围= 0.0〜1.0)

rightVolume right volume value (range = 0.0 to 1.0)

优先级的数据流优先级(0 =最低优先级)

priority stream priority (0 = lowest priority)

循环的循环模式(0 =无环路,-1 =无限循环)

loop loop mode (0 = no loop, -1 = loop forever)

的播放速率(1.0 =正常播放,范围0.5〜2.0)

rate playback rate (1.0 = normal playback, range 0.5 to 2.0)

您需要记住,的Soundpool 不应超过1MB使用的媒体文件,这些文件越小,疗效越好,并表现你。

You need to remember, that SoundPool should not use media files over 1MB, the smaller the files, the better effect and performance you have.

请务必发布的Soundpool 当您完成,或在 Activity.onDestroy

Be sure to release the SoundPool when you are done, or in Activity.onDestroy.

sp.release();

希望这有助于

Hope this helps

这篇关于在Android的游戏音效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆