Catch failure to set screenshot sound

In some cases, MediaPlayer.create fails and propagates up an
IllegalStateException, which causes the screenshot process to
crash. Catch this error (and just don't play the screenshot sound
if it fails to load).

Bug: 239236526
Fix: 239236526
Test: forced the setDataSource to throw an IOException (which is
silently swallowed by MediaPlayer; see bug; and then thrown
as an IllegalStateException since the data source was never
actually set)

Change-Id: I7ae6ae4cc69b8e3eccfbb7df7558787e50db755f
This commit is contained in:
Miranda Kephart
2022-07-25 10:06:25 -04:00
parent 59bf347867
commit 527b2124a1

View File

@@ -843,14 +843,20 @@ public class ScreenshotController {
// The media player creation is slow and needs on the background thread.
return CallbackToFutureAdapter.getFuture((completer) -> {
mBgExecutor.execute(() -> {
MediaPlayer player = MediaPlayer.create(mContext,
Uri.fromFile(new File(mContext.getResources().getString(
com.android.internal.R.string.config_cameraShutterSound))), null,
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build(), AudioSystem.newAudioSessionId());
completer.set(player);
try {
MediaPlayer player = MediaPlayer.create(mContext,
Uri.fromFile(new File(mContext.getResources().getString(
com.android.internal.R.string.config_cameraShutterSound))),
null,
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build(), AudioSystem.newAudioSessionId());
completer.set(player);
} catch (IllegalStateException e) {
Log.w(TAG, "Screenshot sound initialization failed", e);
completer.set(null);
}
});
return "ScreenshotController#loadCameraSound";
});