整场比赛期间播放背景音乐

时间:2011-12-20 08:39:34

标签: android

我正在开发一款记忆游戏,我想在开始游戏时播放背景音乐。

我使用了声音池类并成功执行了它,但是我在播放时遇到了问题。我添加了两分钟的.mp3音频文件,但它的播放时间仅为15秒。

有谁能说我有什么问题?这是我的声音类文件:

public class Soundclass {
    private SoundPool soundpool;
    private HashMap<Integer, Integer> soundpoolmap;
    private AudioManager audiomanager;
    private Context context;

    public void initSounds(Context thecontext) {
        context = thecontext;
        soundpool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
        soundpoolmap = new HashMap<Integer, Integer>();
        audiomanager = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);
    }

    public Soundclass() {

    }

    public void addSound(int Index, int soundID) {
        soundpoolmap.put(Index, soundpool.load(context, soundID, 1));
    }

    public void playSound(int Index) {
        float streamVolume = audiomanager
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        streamVolume = streamVolume
                / audiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        soundpool.play(soundpoolmap.get(Index), streamVolume, streamVolume, 1,
                0, 1f);
    }

    public void playLoopedSound(int Index) {
        float streamVolume = audiomanager
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        streamVolume = streamVolume
                / audiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        soundpool.play(soundpoolmap.get(Index), streamVolume, streamVolume, 1,
                -1, 1f);
    }

}

我将这个类用于给定的activitycalss .in oncreate function

Button btn = (Button) findViewById(R.id.sound);
        soundclass = new Soundclass();

        soundclass.initSounds(getBaseContext());
        soundclass.addSound(1, R.raw.jinglebells);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                soundclass.playSound(1);

            }
        });

1 个答案:

答案 0 :(得分:5)

Soundpool不适合用于背景音乐。它有许多限制,例如文件大小加载和播放时间。它主要用于音效。 使用MediaPlayer类进行背景音乐。

相关问题