aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/pheelicks/utils/TunnelPlayerWorkaround.java
blob: c25caf0ca4ae39766d741e134b44f5f850507767 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
 * Copyright 2013, Haruki Hasegawa
 *
 * Licensed under the MIT license:
 * http://creativecommons.org/licenses/MIT/
 */

package com.pheelicks.utils;

import java.io.IOException;

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

import com.pheelicks.visualizer.R;

public class TunnelPlayerWorkaround {
  private static final String TAG = "TunnelPlayerWorkaround";

  private static final String SYSTEM_PROP_TUNNEL_DECODE_ENABLED = "tunnel.decode";

  private TunnelPlayerWorkaround()
  {
  }

  /**
   * Obtain "tunnel.decode" system property value
   * 
   * @param context Context
   * @return Whether tunnel player is enabled
   */
  public static boolean isTunnelDecodeEnabled(Context context)
  {
    return SystemPropertiesProxy.getBoolean(
        context, SYSTEM_PROP_TUNNEL_DECODE_ENABLED, false);
  }

  /**
   * Create silent MediaPlayer instance to avoid tunnel player issue
   * 
   * @param context Context
   * @return MediaPlayer instance
   */
  public static MediaPlayer createSilentMediaPlayer(Context context)
  {
    boolean result = false;

    MediaPlayer mp = null;
    try {
      mp = MediaPlayer.create(context, R.raw.workaround_1min);
      mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

      // NOTE: start() is no needed
      // mp.start();

      result = true;
    } catch (RuntimeException e) {
      Log.e(TAG, "createSilentMediaPlayer()", e);
    } finally {
      if (!result && mp != null) {
        try {
          mp.release();
        } catch (IllegalStateException e) {
        }
      }
    }

    return mp;
  }
}