5138f578057f1bf5b251fd40e422815ecc03d76d.svn-base 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package eVVM.apk.helper;
  2. import android.content.Context;
  3. import android.media.AudioManager;
  4. import android.media.RingtoneManager;
  5. import android.media.SoundPool;
  6. import android.net.Uri;
  7. import android.support.annotation.IntDef;
  8. import android.support.annotation.NonNull;
  9. import android.support.annotation.RawRes;
  10. import java.lang.annotation.Retention;
  11. import java.lang.annotation.RetentionPolicy;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. /**
  15. * <pre>
  16. * author: Chestnut
  17. * blog : http://www.jianshu.com/u/a0206b5f4526
  18. * time : 2017/6/22 17:24
  19. * desc : 封装了SoundPool
  20. * thanks To: http://flycatdeng.iteye.com/blog/2120043
  21. * http://www.2cto.com/kf/201408/325318.html
  22. * https://developer.android.com/reference/android/media/SoundPool.html
  23. * dependent on:
  24. * update log:
  25. * 1. 2017年6月28日10:39:46
  26. * 1)修复了当play指定的RingtoneName为空的时候,触发的一个bug
  27. * 2)增加了一个默认的铃声,当找不到系统的默认铃声时候,会默认加载一个我们提供的一个默认铃声
  28. * </pre>
  29. */
  30. public class SoundPoolHelper {
  31. /*常量*/
  32. public final static int TYPE_MUSIC = AudioManager.STREAM_MUSIC;
  33. public final static int TYPE_ALARM = AudioManager.STREAM_ALARM;
  34. public final static int TYPE_RING = AudioManager.STREAM_RING;
  35. @IntDef({TYPE_MUSIC, TYPE_ALARM, TYPE_RING})
  36. @Retention(RetentionPolicy.SOURCE)
  37. public @interface TYPE {
  38. }
  39. public final static int RING_TYPE_MUSIC = RingtoneManager.TYPE_ALARM;
  40. public final static int RING_TYPE_ALARM = RingtoneManager.TYPE_NOTIFICATION;
  41. public final static int RING_TYPE_RING = RingtoneManager.TYPE_RINGTONE;
  42. @IntDef({RING_TYPE_MUSIC, RING_TYPE_ALARM, RING_TYPE_RING})
  43. @Retention(RetentionPolicy.SOURCE)
  44. public @interface RING_TYPE {
  45. }
  46. /*变量*/
  47. private SoundPool soundPool;
  48. private int NOW_RINGTONE_TYPE = RingtoneManager.TYPE_NOTIFICATION;
  49. private int maxStream;
  50. private Map<String, Integer> ringtoneIds;
  51. /*方法*/
  52. public SoundPoolHelper() {
  53. this(1, TYPE_MUSIC);
  54. }
  55. public SoundPoolHelper(int maxStream) {
  56. this(maxStream, TYPE_ALARM);
  57. }
  58. public SoundPoolHelper(int maxStream, @TYPE int streamType) {
  59. soundPool = new SoundPool(maxStream, streamType, 1);
  60. this.maxStream = maxStream;
  61. ringtoneIds = new HashMap<>();
  62. }
  63. /**
  64. * 设置RingtoneType,这只是关系到加载哪一个默认音频
  65. * 需要在load之前调用
  66. *
  67. * @param ringtoneType ringtoneType
  68. * @return this
  69. */
  70. public SoundPoolHelper setRingtoneType(@RING_TYPE int ringtoneType) {
  71. NOW_RINGTONE_TYPE = ringtoneType;
  72. return this;
  73. }
  74. /**
  75. * 加载音频资源
  76. *
  77. * @param context 上下文
  78. * @param resId 资源ID
  79. * @return this
  80. */
  81. public SoundPoolHelper load(Context context, @NonNull String ringtoneName, @RawRes int resId) {
  82. if (maxStream == 0)
  83. return this;
  84. maxStream--;
  85. ringtoneIds.put(ringtoneName, soundPool.load(context, resId, 1));
  86. return this;
  87. }
  88. /**
  89. * 加载默认的铃声
  90. *
  91. * @param context 上下文
  92. * @return this
  93. */
  94. public SoundPoolHelper loadDefault(Context context) {
  95. Uri uri = getSystemDefaultRingtoneUri(context);
  96. // if (uri==null)
  97. // load(context,"default", R.raw.reminder);
  98. // else
  99. // load(context,"default",ConvertUtils.uri2Path(context,uri));
  100. return this;
  101. }
  102. /**
  103. * 加载铃声
  104. *
  105. * @param context 上下文
  106. * @param ringtoneName 自定义铃声名称
  107. * @param ringtonePath 铃声路径
  108. * @return this
  109. */
  110. public SoundPoolHelper load(Context context, @NonNull String ringtoneName, @NonNull String ringtonePath) {
  111. if (maxStream == 0)
  112. return this;
  113. maxStream--;
  114. ringtoneIds.put(ringtoneName, soundPool.load(ringtonePath, 1));
  115. return this;
  116. }
  117. /**
  118. * int play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) :
  119. * 1)该方法的第一个参数指定播放哪个声音;
  120. * 2) leftVolume 、
  121. * 3) rightVolume 指定左、右的音量:
  122. * 4) priority 指定播放声音的优先级,数值越大,优先级越高;
  123. * 5) loop 指定是否循环, 0 为不循环, -1 为循环;
  124. * 6) rate 指定播放的比率,数值可从 0.5 到 2 , 1 为正常比率。
  125. */
  126. public void play(@NonNull String ringtoneName, boolean isLoop) {
  127. if (ringtoneIds.containsKey(ringtoneName)) {
  128. soundPool.play(ringtoneIds.get(ringtoneName), 1, 1, 1, isLoop ? -1 : 0, 1);
  129. }
  130. }
  131. public void playDefault() {
  132. play("default", false);
  133. }
  134. /**
  135. * 释放资源
  136. */
  137. public void release() {
  138. if (soundPool != null)
  139. soundPool.release();
  140. }
  141. /**
  142. * 获取系统默认铃声的Uri
  143. *
  144. * @param context 上下文
  145. * @return uri
  146. */
  147. private Uri getSystemDefaultRingtoneUri(Context context) {
  148. try {
  149. return RingtoneManager.getActualDefaultRingtoneUri(context, NOW_RINGTONE_TYPE);
  150. } catch (Exception e) {
  151. return null;
  152. }
  153. }
  154. }