在Android上用SoundPool播放声音

时间:2011-06-18 14:32:28

标签: android

我是android的编程新手。 我需要在我的应用程序中播放一个简短的声音。 我写了下面的代码,但我的三星手机上没有声音和奇怪的振动。但同时这段代码在我的android模拟器上运行良好。 我的代码是:

package com.samplers;

import android.app.Activity;
import android.media.SoundPool;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class FixVibroActivity extends Activity {
    /** Called when the activity is first created. */

    private Button white;
    private SoundPool spool;
    private int soundID;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        spool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        soundID = spool.load(this, R.raw.error, 1);

        white = (Button)findViewById(R.id.whiteBtn);
        white.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Sound();
            }
        });
    }

    public void Sound(){
        AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        spool.play(soundID, volume, volume, 1, 0, 1f);
    };
}

0 个答案:

没有答案