【问题标题】:Sound effects Android音效安卓
【发布时间】:2016-01-18 14:54:53
【问题描述】:

我想为每个玩家事件添加音效,例如死亡。我应该使用 soundpool 还是 meidaplayer,我该怎么做?为了更好地理解以下是我的课程设计,我有一个主要的活动类“游戏”,我将其称为“GamePanel”类,它扩展了surfaceView并绘制了我的整个游戏。任何帮助将不胜感激!游戏类如下。

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class Game extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    View v1 = (new GamePanel(this));
    setContentView(v1);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_game, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);


}

}

还有我想要播放声音的 GamePanel

   player.resetDY();
        if (!reset) {
            newGameCreated = false;
            startReset = System.nanoTime();
            reset = true;
            dissapear = true;
            explosion = new 
            Explosion(BitmapFactory.decodeResource(getResources(), R.drawable.explosion), player.getX(),
                    player.getY() - 30, 100, 100, 25);
             ///here 

【问题讨论】:

    标签: android audio android-mediaplayer


    【解决方案1】:

    对于爆炸、收币等短音效,最好使用SoundPool。

    你只需要创建一个声音池:

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

    在 Lollipop 及更高版本中:

    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 作为声音流。

    请务必在您的 Activity 中设置音量控制,以便用户能够更改正确流的音量:

    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    

    然后,您需要将音效加载到池中并为其提供标识符:

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

    在此处查看完整答案:source

    【讨论】:

    • 我在游戏类的 onCreate 方法下构建了声音池,如何将其链接到我的 Gamepanel 以播放声音?
    • 为避免编辑我的答案和许多评论,请浏览以下所有链接:Sound source 2 2. GUI source1 3.Sound
    • 通过上面的链接答案就在那里
    • 观看此视频link
    • 不客气,别忘了给这个答案投票,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 2013-04-23
    相关资源
    最近更新 更多