Sound Manager播放随机文件的问题

时间:2012-04-16 04:48:01

标签: java android ontouchevent

我正在尝试使用SoundManager随机播放30秒的声音,同时启动由onTouch事件触发的动画序列(闪烁图形)。无论出于何种原因,声音咬合在播放约5秒后被剪掉,我无法弄清楚原因。有什么想法吗?

在测试之后,似乎在最初的onTouch事件发生后一分钟左右才会播放。

public class Soundboard extends Activity {
    private SoundManager mSoundManager;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mSoundManager = new SoundManager();
        mSoundManager.initSounds(getBaseContext());
        mSoundManager.addSound(0, R.raw.sound0);
        mSoundManager.addSound(1, R.raw.sound1);
        mSoundManager.addSound(2, R.raw.sound2);
        mSoundManager.addSound(3, R.raw.sound3);
    };

    public boolean onTouchEvent(MotionEvent evt){

        Random r = new Random();
        int x = r.nextInt(3);

        switch (evt.getAction()) 
        {
            case MotionEvent.ACTION_DOWN:
            mSoundManager.playSound(x);
            startAnimating();
            return super.onTouchEvent(evt);

            case MotionEvent.ACTION_UP:
            break;
            default:
            break;
        }
        return true;
    }

    private void startAnimating() {

        ImageView wiub_screen01 = (ImageView) findViewById(R.id.wiub_screen01);
        Animation fadein01 = AnimationUtils.loadAnimation(this, R.anim.fade_in01);
        wiub_screen01.startAnimation(fadein01);

        ImageView wiub_screen00 = (ImageView) findViewById(R.id.wiub_screen00);
        Animation fadein00 = AnimationUtils.loadAnimation(this, R.anim.fade_in00);
        wiub_screen00.startAnimation(fadein00);
    }

}

2 个答案:

答案 0 :(得分:0)

很有可能没有加载音频。如果你真的想要播放一些短片,那么一定要看看SoundPool。 (改编自Google SoundPool示例)

试试这个:

public class Soundboard extends Activity {
    private SoundPool mSoundPool;
    boolean loaded = false;
    private int[] soundIDs;
    private int[] resIDs;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId,
                            int status) {
                    loaded = true;
            }
        });
        resIDs = new int[]{R.raw.sound0,R.raw.sound1,R.raw.sound2.R.raw.sound3 };

        for(int i=0; i<resIDs.length; i++){
            soundIDs[i] = mSoundPool.load(this, resIDs[i], 1);              
        }
    };

    public boolean onTouchEvent(MotionEvent evt){

        Random r = new Random();
        int x = r.nextInt(resIDs.length);

        switch (evt.getAction()) 
        {
            case MotionEvent.ACTION_DOWN:
                AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
                float actualVolume = (float) audioManager
                                .getStreamVolume(AudioManager.STREAM_MUSIC);
                float maxVolume = (float) audioManager
                                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                float volume = actualVolume / maxVolume;
                // Is the sound loaded already?
                if (loaded) {
                        mSoundPool.play(soundIDs[x], volume, volume, 1, 0, 1f);
                        Log.i("Test", "Played sound");
                }
            startAnimating();
            return super.onTouchEvent(evt);

            case MotionEvent.ACTION_UP:
            break;
            default:
            break;
        }
        return true;
    }

    private void startAnimating() {

        ImageView wiub_screen01 = (ImageView) findViewById(R.id.wiub_screen01);
        Animation fadein01 = AnimationUtils.loadAnimation(this, R.anim.fade_in01);
        wiub_screen01.startAnimation(fadein01);

        ImageView wiub_screen00 = (ImageView) findViewById(R.id.wiub_screen00);
        Animation fadein00 = AnimationUtils.loadAnimation(this, R.anim.fade_in00);
        wiub_screen00.startAnimation(fadein00);
    }
}

答案 1 :(得分:0)

尝试将原始文件夹中的文件转换为ogg格式。