Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f992cbb9 authored by Dave Sparks's avatar Dave Sparks
Browse files

Add SoundPool API to pause and resume all active streams. Bug 2426531.

parent 146c4a53
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -320,6 +320,28 @@ public class SoundPool
     */
    public native final void resume(int streamID);

    /**
     * Pause all active streams.
     *
     * Pause all streams that are currently playing. This function
     * iterates through all the active streams and pauses any that
     * are playing. It also sets a flag so that any streams that
     * are playing can be resumed by calling autoResume().
     *
     * @hide
     */
    public native final void autoPause();

    /**
     * Resume all previously active streams.
     *
     * Automatically resumes all streams that were paused in previous
     * calls to autoPause().
     *
     * @hide
     */
    public native final void autoResume();

    /**
     * Stop a playback stream.
     *
+43 −0
Original line number Diff line number Diff line
@@ -294,6 +294,16 @@ void SoundPool::pause(int channelID)
    }
}

void SoundPool::autoPause()
{
    LOGV("pauseAll()");
    Mutex::Autolock lock(&mLock);
    for (int i = 0; i < mMaxChannels; ++i) {
        SoundChannel* channel = &mChannelPool[i];
        channel->autoPause();
    }
}

void SoundPool::resume(int channelID)
{
    LOGV("resume(%d)", channelID);
@@ -304,6 +314,16 @@ void SoundPool::resume(int channelID)
    }
}

void SoundPool::autoResume()
{
    LOGV("pauseAll()");
    Mutex::Autolock lock(&mLock);
    for (int i = 0; i < mMaxChannels; ++i) {
        SoundChannel* channel = &mChannelPool[i];
        channel->autoResume();
    }
}

void SoundPool::stop(int channelID)
{
    LOGV("stop(%d)", channelID);
@@ -710,12 +730,35 @@ void SoundChannel::pause()
    }
}

void SoundChannel::autoPause()
{
    Mutex::Autolock lock(&mLock);
    if (mState == PLAYING) {
        LOGV("pause track");
        mState = PAUSED;
        mAutoPaused = true;
        mAudioTrack->pause();
    }
}

void SoundChannel::resume()
{
    Mutex::Autolock lock(&mLock);
    if (mState == PAUSED) {
        LOGV("resume track");
        mState = PLAYING;
        mAutoPaused = false;
        mAudioTrack->start();
    }
}

void SoundChannel::autoResume()
{
    Mutex::Autolock lock(&mLock);
    if (mAutoPaused && (mState == PAUSED)) {
        LOGV("resume track");
        mState = PLAYING;
        mAutoPaused = false;
        mAudioTrack->start();
    }
}
+7 −1
Original line number Diff line number Diff line
@@ -117,7 +117,8 @@ protected:
class SoundChannel : public SoundEvent {
public:
    enum state { IDLE, RESUMING, STOPPING, PAUSED, PLAYING };
    SoundChannel() : mAudioTrack(0), mState(IDLE), mNumChannels(1), mPos(0), mToggle(0) {}
    SoundChannel() : mAudioTrack(0), mState(IDLE), mNumChannels(1),
            mPos(0), mToggle(0), mAutoPaused(false) {}
    ~SoundChannel();
    void init(SoundPool* soundPool);
    void play(const sp<Sample>& sample, int channelID, float leftVolume, float rightVolume,
@@ -127,7 +128,9 @@ public:
    void stop_l();
    void stop();
    void pause();
    void autoPause();
    void resume();
    void autoResume();
    void setRate(float rate);
    int state() { return mState; }
    void setPriority(int priority) { mPriority = priority; }
@@ -151,6 +154,7 @@ private:
    int                 mPos;
    int                 mAudioBufferSize;
    unsigned long       mToggle;
    bool                mAutoPaused;
};

// application object for managing a pool of sounds
@@ -166,7 +170,9 @@ public:
    int play(int sampleID, float leftVolume, float rightVolume, int priority,
            int loop, float rate);
    void pause(int channelID);
    void autoPause();
    void resume(int channelID);
    void autoResume();
    void stop(int channelID);
    void setVolume(int channelID, float leftVolume, float rightVolume);
    void setPriority(int channelID, int priority);
+26 −0
Original line number Diff line number Diff line
@@ -101,6 +101,24 @@ android_media_SoundPool_resume(JNIEnv *env, jobject thiz, jint channelID)
    ap->resume(channelID);
}

static void
android_media_SoundPool_autoPause(JNIEnv *env, jobject thiz)
{
    LOGV("android_media_SoundPool_autoPause");
    SoundPool *ap = MusterSoundPool(env, thiz);
    if (ap == NULL) return;
    ap->autoPause();
}

static void
android_media_SoundPool_autoResume(JNIEnv *env, jobject thiz)
{
    LOGV("android_media_SoundPool_autoResume");
    SoundPool *ap = MusterSoundPool(env, thiz);
    if (ap == NULL) return;
    ap->autoResume();
}

static void
android_media_SoundPool_stop(JNIEnv *env, jobject thiz, jint channelID)
{
@@ -223,6 +241,14 @@ static JNINativeMethod gMethods[] = {
        "(I)V",
        (void *)android_media_SoundPool_resume
    },
    {   "autoPause",
        "()V",
        (void *)android_media_SoundPool_autoPause
    },
    {   "autoResume",
        "()V",
        (void *)android_media_SoundPool_autoResume
    },
    {   "stop",
        "(I)V",
        (void *)android_media_SoundPool_stop
+36 −9
Original line number Diff line number Diff line
@@ -52,7 +52,6 @@ public class SoundPoolTest extends Activity
        R.raw.test5
    };

    private final static int MAX_STREAMS = 1;
    private final static float SEMITONE = 1.059463094f;
    private final static float DEFAULT_VOLUME = 0.707f;
    private final static float MAX_VOLUME = 1.0f;
@@ -70,6 +69,7 @@ public class SoundPoolTest extends Activity
        private boolean mRunning;
        private SoundPool mSoundPool = null;
        private int mLastSample;
        private int mMaxStreams;
        private int mLoadStatus;
        private int[] mSounds;
        private float mScale[];
@@ -101,17 +101,18 @@ public class SoundPoolTest extends Activity
            return id;
        }

        private int initSoundPool() throws java.lang.InterruptedException {
        private int initSoundPool(int numStreams) throws java.lang.InterruptedException {

            if (mSoundPool != null) {
                if (mLoadStatus == 0) return mLoadStatus;
                if ((mMaxStreams == numStreams) && (mLoadStatus == 0)) return mLoadStatus;
                mSoundPool.release();
                mSoundPool = null;
            }

            // create sound pool
            mLoadStatus = 0;
            mSoundPool = new SoundPool(MAX_STREAMS, AudioSystem.STREAM_MUSIC, 0);
            mMaxStreams = numStreams;
            mSoundPool = new SoundPool(numStreams, AudioSystem.STREAM_MUSIC, 0);
            mSoundPool.setOnLoadCompleteListener(new LoadCompleteCallback());
            int numSounds = mTestFiles.length;
            mSounds = new int[numSounds];
@@ -262,6 +263,31 @@ public class SoundPoolTest extends Activity

            mSoundPool.stop(id);

            // play 5 sounds, forces one to be stolen
            int ids[] = new int[5];
            for (int i = 0; i < 5; i++) {
                ids[i] = mSoundPool.play(mSounds[0], DEFAULT_VOLUME, DEFAULT_VOLUME,
                        NORMAL_PRIORITY, DEFAULT_LOOP, mScale[i]);
                if (DEBUG) Log.d(LOG_TAG, "Start note " + ids[i]);
                if (ids[i] == 0) {
                    Log.e(LOG_TAG, "Error occurred starting note");
                    return false;
                }
                sleep(250);
            }

            // pause and resume sound a few times
            for (int count = 0; count < 5; count++) {
                mSoundPool.autoPause();
                sleep(250);
                mSoundPool.autoResume();
                sleep(250);
            }

            for (int i = 0; i < 5; i++) {
                mSoundPool.stop(ids[i]);
            }

            if (DEBUG) Log.d(LOG_TAG, "End pause/resume test");
            return result;
        }
@@ -309,17 +335,18 @@ public class SoundPoolTest extends Activity

            try {

                // load sound pool
                initSoundPool();

                // do tests
                // do single stream tests
                initSoundPool(1);
                if (!TestSounds()) failures = failures + 1;
                if (!TestScales()) failures = failures + 1;
                if (!TestRates()) failures = failures + 1;
                if (!TestPriority()) failures = failures + 1;
                if (!TestPauseResume()) failures = failures + 1;
                if (!TestVolume()) failures = failures + 1;

                // do multiple stream tests
                initSoundPool(4);
                if (!TestPauseResume()) failures = failures + 1;

            } catch (java.lang.InterruptedException e) {
                if (DEBUG) Log.d(LOG_TAG, "Test interrupted");
                failures = failures + 1;