13208671aa4539c27717aa88a7a8dbbf2c22ce59.svn-base 5.2 KB

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