【问题标题】:SoundPool puse works only one timeSoundPool puse 只能使用一次
【发布时间】:2016-09-28 12:18:25
【问题描述】:

这是我的代码:

    int SC5;
    sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
    SC5  = sp.load(this, R.raw.c5, 1);

    C5.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()) {

                case MotionEvent.ACTION_DOWN:
                    // PRESSED
                    sp.play(SC5, 1, 1, 0, 0, 1);
                    return true; 


                case MotionEvent.ACTION_UP:
                    // RELEASED
                    sp.pause(SC5);

                    return true; 
            }
            return false;
        }
    });

我希望当我释放按钮时我的声音池停止播放,这段代码有效,但只有一次。当我第二次按下按钮时,我无法停止声音 我不能使用autoPause();,因为我在这个声音池中有其他文件

请帮忙

【问题讨论】:

    标签: java android android-studio soundpool


    【解决方案1】:

    解决方案

    int streamId = -1;
    C5.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()) {
    
                case MotionEvent.ACTION_DOWN:
                    // PRESSED
                    streamId = sp.play(SC5, 1, 1, 0, 0, 1);
                    return true; 
    
    
                case MotionEvent.ACTION_UP:
                    // RELEASED
                    sp.pause(streamId);
    
                    return true; 
            }
            return false;
        }
    });
    

    解释

    您不能使用 SC5 来暂停声音,因为 SC5 原来是一个 soundId。

    有关更多信息,您应该查看 SoundPool 的文档,并注意您用于播放 (soundId) 或暂停 (streamId) 声音的 ID

     /**
     * Play a sound from a sound ID.
     *
     * Play the sound specified by the soundID. This is the value 
     * returned by the load() function. Returns a non-zero streamID
     * if successful, zero if it fails. The streamID can be used to
     * further control playback. Note that calling play() may cause
     * another sound to stop playing if the maximum number of active
     * streams is exceeded. A loop value of -1 means loop forever,
     * a value of 0 means don't loop, other values indicate the
     * number of repeats, e.g. a value of 1 plays the audio twice.
     * The playback rate allows the application to vary the playback
     * rate (pitch) of the sound. A value of 1.0 means play back at
     * the original frequency. A value of 2.0 means play back twice
     * as fast, and a value of 0.5 means playback at half speed.
     *
     * @param soundID a soundID returned by the load() function
     * @param leftVolume left volume value (range = 0.0 to 1.0)
     * @param rightVolume right volume value (range = 0.0 to 1.0)
     * @param priority stream priority (0 = lowest priority)
     * @param loop loop mode (0 = no loop, -1 = loop forever)
     * @param rate playback rate (1.0 = normal playback, range 0.5 to 2.0)
     * @return non-zero streamID if successful, zero if failed
     */
    public final int play(int soundID, float leftVolume, float rightVolume,
            int priority, int loop, float rate);
    
    /**
     * Pause a playback stream.
     *
     * Pause the stream specified by the streamID. This is the
     * value returned by the play() function. If the stream is
     * playing, it will be paused. If the stream is not playing
     * (e.g. is stopped or was previously paused), calling this
     * function will have no effect.
     *
     * @param streamID a streamID returned by the play() function
     */
    public final void pause(int streamID); 
    

    【讨论】:

    • 对不起,我编辑我的帖子。现在你可以看得更清楚了。我唯一的问题是我的暂停只能工作一次,当我再次播放时,我无法暂停,但其他事情正常
    • 我知道,正如我所说,你使用 soundId 来暂停你的声音,这是不正确的!使用 streamId 暂停工作正常。
    • 呃......为了更清楚,我已经更新了我的答案。我用你的代码做了我自己的演示,效果很好.....
    • 谢谢伙计,我又试了一次,它奏效了。非常感谢
    猜你喜欢
    • 2015-04-19
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多