summaryrefslogtreecommitdiffstats
path: root/tests/src/com/cyngn/audiofx/util/TestDuckingMediaPlayer.java
blob: fc67cc243658da270603fcb4aa631fc8bcb9197e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.cyngn.audiofx.util;

import android.content.Context;
import android.media.AudioManager;
import android.util.Log;

import com.cyngn.audiofx.tests.R;

import static junit.framework.Assert.assertNotNull;

/**
 * Created by roman on 3/4/16.
 */
public class TestDuckingMediaPlayer extends TestMediaPlayer {

    private static final String TAG = TestDuckingMediaPlayer.class.getSimpleName();
    private AudioManager mAudioManager;

    private AudioManager.OnAudioFocusChangeListener mAudioFocusChangeListener
            = new AudioManager.OnAudioFocusChangeListener() {
        public void onAudioFocusChange(int focusChange) {
            Log.i(TAG, "onAudioFocusChange() called with " + "focusChange = [" + focusChange + "]");
            switch(focusChange) {
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    setVolume(0.4f);
                    break;

                case AudioManager.AUDIOFOCUS_GAIN:
                    setVolume(1.f);
                    break;
            }
        }
    };

    public int start(int focus) throws IllegalStateException {
        int result = mAudioManager.requestAudioFocus(mAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
                focus);
        super.start();
        return result;
    }

    @Override
    public void stop() throws IllegalStateException {
        mAudioManager.abandonAudioFocus(mAudioFocusChangeListener);
        super.stop();
    }

    public TestDuckingMediaPlayer(Context context) {
        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    }

    public TestDuckingMediaPlayer(Context context, int withResource) throws Exception {
        super(context, withResource);
        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    }



}