android 9插拔多次,充电提示音无声

测试压测,多次插拔充电器,发现充电提示音消失了,需要重新启动才能恢复,但重复操作又会复现。

参考blog:https://www.jianshu.com/p/455da8b1cd79
mediaPlayer未释放资源的问题,会导致其它模块播放media时出现(1,-19)
Ringtone 持有一个对应的mediaplayer,虽然Ringtone 每次播放前都释放资源,但是因为RingtoneManager每次返回的是一个新的Ringtone 对象,所以上面代码中,会不断的创建新的mediaplayer,导致出现无法播放问题。

为了防止出现资源未释放的问题,修改逻辑,使每次释放都有确切的操作:

Index: frameworks/base/services/core/java/com/android/server/power/Notifier.java
===================================================================
--- frameworks/base/services/core/java/com/android/server/power/Notifier.java	(版本 433)
+++ frameworks/base/services/core/java/com/android/server/power/Notifier.java	(工作副本)
@@ -152,6 +152,8 @@
     // True if a user activity message should be sent.
     private boolean mUserActivityPending;
 
+    private Ringtone mRingtone;
+
     public Notifier(Looper looper, Context context, IBatteryStats batteryStats,
             SuspendBlocker suspendBlocker, WindowManagerPolicy policy) {
         mContext = context;
@@ -187,6 +189,8 @@
         try {
             mBatteryStats.noteInteractive(true);
         } catch (RemoteException ex) { }
+
+        initChargingStartedSound();
     }
 
     /**
@@ -738,19 +742,43 @@
      * Plays the wireless charging sound for both wireless and non-wireless charging
      */
     private void playChargingStartedSound() {

+        //final String soundPath = Settings.Global.getString(mContext.getContentResolver(),
+        //        Settings.Global.CHARGING_STARTED_SOUND);
+        //if (isChargingFeedbackEnabled() && soundPath != null) {
+        //    final Uri soundUri = Uri.parse("file://" + soundPath);
+        //    if (soundUri != null) {
+        //        final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri);
+        //        if (sfx != null) {
+        //            sfx.setStreamType(AudioManager.STREAM_SYSTEM);
+        //            sfx.play();
+        //        }
+        //    }
+        //}
+        if (mRingtone != null) {
+            mRingtone.stop();
+        }
+        if (isChargingFeedbackEnabled()) {
+            mRingtone.play();
+        }

+    }
+

+    private void initChargingStartedSound() {
         final String soundPath = Settings.Global.getString(mContext.getContentResolver(),
                 Settings.Global.CHARGING_STARTED_SOUND);
-        if (isChargingFeedbackEnabled() && soundPath != null) {
+        if (soundPath != null) {
             final Uri soundUri = Uri.parse("file://" + soundPath);
             if (soundUri != null) {
-                final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri);
-                if (sfx != null) {
-                    sfx.setStreamType(AudioManager.STREAM_SYSTEM);
-                    sfx.play();
+                mRingtone = RingtoneManager.getRingtone(mContext, soundUri);
+                if (mRingtone != null) {
+                    mRingtone.setStreamType(AudioManager.STREAM_SYSTEM);
                 }
             }
         }
     }

 
     private void showWirelessChargingStarted(int batteryLevel) {
         playWirelessChargingVibration();

你可能感兴趣的:(android_audio)