fc0b7e9e169e7c1d57e7290807a9fc51913dbd0c.svn-base 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. private static volatile int streamID = 0;
  43. @IntDef({RING_TYPE_MUSIC, RING_TYPE_ALARM, RING_TYPE_RING})
  44. @Retention(RetentionPolicy.SOURCE)
  45. public @interface RING_TYPE {
  46. }
  47. /*变量*/
  48. private SoundPool soundPool;
  49. private int NOW_RINGTONE_TYPE = RingtoneManager.TYPE_NOTIFICATION;
  50. private int maxStream;
  51. private Map<String, Integer> ringtoneIds;
  52. /*方法*/
  53. public SoundPoolHelper() {
  54. this(1, TYPE_MUSIC);
  55. }
  56. public SoundPoolHelper(int maxStream) {
  57. this(maxStream, TYPE_ALARM);
  58. }
  59. public SoundPoolHelper(int maxStream, @TYPE int streamType) {
  60. soundPool = new SoundPool(maxStream, streamType, 1);
  61. this.maxStream = maxStream;
  62. ringtoneIds = new HashMap<>();
  63. }
  64. /**
  65. * 设置RingtoneType,这只是关系到加载哪一个默认音频
  66. * 需要在load之前调用
  67. *
  68. * @param ringtoneType ringtoneType
  69. * @return this
  70. */
  71. public SoundPoolHelper setRingtoneType(@RING_TYPE int ringtoneType) {
  72. NOW_RINGTONE_TYPE = ringtoneType;
  73. return this;
  74. }
  75. /**
  76. * 加载音频资源
  77. *
  78. * @param context 上下文
  79. * @param resId 资源ID
  80. * @return this
  81. */
  82. public SoundPoolHelper load(Context context, @NonNull String ringtoneName, @RawRes int resId) {
  83. if (maxStream == 0)
  84. return this;
  85. maxStream--;
  86. ringtoneIds.put(ringtoneName, soundPool.load(context, resId, 1));
  87. return this;
  88. }
  89. /**
  90. * 加载默认的铃声
  91. *
  92. * @param context 上下文
  93. * @return this
  94. */
  95. public SoundPoolHelper loadDefault(Context context) {
  96. Uri uri = getSystemDefaultRingtoneUri(context);
  97. // if (uri==null)
  98. // load(context,"default", R.raw.reminder);
  99. // else
  100. // load(context,"default",ConvertUtils.uri2Path(context,uri));
  101. return this;
  102. }
  103. /**
  104. * 加载铃声
  105. *
  106. * @param context 上下文
  107. * @param ringtoneName 自定义铃声名称
  108. * @param ringtonePath 铃声路径
  109. * @return this
  110. */
  111. public SoundPoolHelper load(Context context, @NonNull String ringtoneName, @NonNull String ringtonePath) {
  112. if (maxStream == 0)
  113. return this;
  114. maxStream--;
  115. ringtoneIds.put(ringtoneName, soundPool.load(ringtonePath, 1));
  116. return this;
  117. }
  118. /**
  119. * int play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) :
  120. * 1)该方法的第一个参数指定播放哪个声音;
  121. * 2) leftVolume 、
  122. * 3) rightVolume 指定左、右的音量:
  123. * 4) priority 指定播放声音的优先级,数值越大,优先级越高;
  124. * 5) loop 指定是否循环, 0 为不循环, -1 为循环;
  125. * 6) rate 指定播放的比率,数值可从 0.5 到 2 , 1 为正常比率。
  126. */
  127. public void play(@NonNull String ringtoneName, boolean isLoop) {
  128. if (ringtoneIds.containsKey(ringtoneName)) {
  129. streamID = soundPool.play(ringtoneIds.get(ringtoneName), 1, 1, 1, isLoop ? -1 : 0, 1);
  130. }
  131. }
  132. public void playDefault() {
  133. play("default", false);
  134. }
  135. /**
  136. * 释放资源
  137. */
  138. public void stop() {
  139. if (soundPool != null&&streamID!=0)
  140. soundPool.stop(streamID);
  141. }
  142. /**
  143. * 释放资源
  144. */
  145. public void release() {
  146. if (soundPool != null)
  147. soundPool.release();
  148. }
  149. /**
  150. * 获取系统默认铃声的Uri
  151. *
  152. * @param context 上下文
  153. * @return uri
  154. */
  155. private Uri getSystemDefaultRingtoneUri(Context context) {
  156. try {
  157. return RingtoneManager.getActualDefaultRingtoneUri(context, NOW_RINGTONE_TYPE);
  158. } catch (Exception e) {
  159. return null;
  160. }
  161. }
  162. }