Search in sources :

Example 1 with OnLoadCompleteListener

use of android.media.SoundPool.OnLoadCompleteListener in project coursera-android by aporter.

the class AudioVideoAudioManagerActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // Get reference to the AudioManager
    mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    // Display current volume level in TextView
    final TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText(String.valueOf(mVolume));
    // Set up Button to increase the volume
    final Button upButton = (Button) findViewById(R.id.button2);
    upButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // Play key click sound
            mAudioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
            if (mVolume < mVolumeMax) {
                mVolume += 2;
                tv.setText(String.valueOf(mVolume));
            }
        }
    });
    // // Set up Button to decrease the volume
    final Button downButton = (Button) findViewById(R.id.button1);
    downButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // Play key click sound
            mAudioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
            if (mVolume > mVolumeMin) {
                mVolume -= 2;
                tv.setText(String.valueOf(mVolume));
            }
        }
    });
    final Button playButton = (Button) findViewById(R.id.button3);
    // Disable the Play Button so user can't click it before sounds are
    // ready
    playButton.setEnabled(false);
    // Create a SoundPool
    mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    // Load bubble popping sound into the SoundPool
    mSoundId = mSoundPool.load(this, R.raw.slow_whoop_bubble_pop, 1);
    // Set an OnLoadCompleteListener on the SoundPool
    mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {

        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
            // If sound loading was successful enable the play Button
            if (0 == status) {
                playButton.setEnabled(true);
            } else {
                Log.i(TAG, "Unable to load sound");
                finish();
            }
        }
    });
    // Play the sound using a SoundPool
    playButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mCanPlayAudio)
                mSoundPool.play(mSoundId, (float) mVolume / mVolumeMax, (float) mVolume / mVolumeMax, 1, 0, 1.0f);
        }
    });
    // Request audio focus
    int result = mAudioManager.requestAudioFocus(afChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    // Set to true if app has audio foucs
    mCanPlayAudio = AudioManager.AUDIOFOCUS_REQUEST_GRANTED == result;
}
Also used : Button(android.widget.Button) OnLoadCompleteListener(android.media.SoundPool.OnLoadCompleteListener) OnClickListener(android.view.View.OnClickListener) TextView(android.widget.TextView) SoundPool(android.media.SoundPool) TextView(android.widget.TextView) View(android.view.View)

Aggregations

SoundPool (android.media.SoundPool)1 OnLoadCompleteListener (android.media.SoundPool.OnLoadCompleteListener)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 Button (android.widget.Button)1 TextView (android.widget.TextView)1