diff options
author | Sascha Haeberling <haeberling@google.com> | 2014-08-27 17:56:08 -0700 |
---|---|---|
committer | Sascha Haeberling <haeberling@google.com> | 2014-08-27 18:18:10 -0700 |
commit | 2d4be2576b265a4abfbabbda20862fcea19c1395 (patch) | |
tree | aa5223bbeff6176554906c79c7f36b6a3db45d64 /src/com | |
parent | 352de757246fa28c6fdfa5d0d3e51659e23f8183 (diff) | |
download | android_packages_apps_Camera2-2d4be2576b265a4abfbabbda20862fcea19c1395.tar.gz android_packages_apps_Camera2-2d4be2576b265a4abfbabbda20862fcea19c1395.tar.bz2 android_packages_apps_Camera2-2d4be2576b265a4abfbabbda20862fcea19c1395.zip |
Add simple sound player to be used in Camera implementations.
Bug: 17205355
Change-Id: I04c4bf1a692c7a4555eafea794737e41a8fc342c
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/android/camera/CameraActivity.java | 12 | ||||
-rw-r--r-- | src/com/android/camera/CaptureModule.java | 6 | ||||
-rw-r--r-- | src/com/android/camera/SoundPlayer.java | 80 | ||||
-rw-r--r-- | src/com/android/camera/app/AppController.java | 4 | ||||
-rw-r--r-- | src/com/android/camera/one/OneCameraManager.java | 2 | ||||
-rw-r--r-- | src/com/android/camera/one/v2/OneCameraManagerImpl.java | 8 |
6 files changed, 102 insertions, 10 deletions
diff --git a/src/com/android/camera/CameraActivity.java b/src/com/android/camera/CameraActivity.java index 112eb4337..43cddd20d 100644 --- a/src/com/android/camera/CameraActivity.java +++ b/src/com/android/camera/CameraActivity.java @@ -251,6 +251,9 @@ public class CameraActivity extends Activity private Menu mActionBarMenu; private Preloader<Integer, AsyncTask> mPreloader; + /** Can be used to play custom sounds. */ + private SoundPlayer mSoundPlayer; + private static final int LIGHTS_OUT_DELAY_MS = 4000; private final int BASE_SYS_UI_VISIBILITY = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN @@ -283,7 +286,7 @@ public class CameraActivity extends Activity } }; - private ActionBar.OnMenuVisibilityListener mOnMenuVisibilityListener = + private final ActionBar.OnMenuVisibilityListener mOnMenuVisibilityListener = new ActionBar.OnMenuVisibilityListener() { @Override public void onMenuVisibilityChanged(boolean isVisible) { @@ -1317,6 +1320,7 @@ public class CameraActivity extends Activity mOnCreateTime = System.currentTimeMillis(); mAppContext = getApplicationContext(); + mSoundPlayer = new SoundPlayer(mAppContext); // TODO: Try to move all the resources allocation to happen as soon as // possible so we can call module.init() at the earliest time. @@ -1855,6 +1859,7 @@ public class CameraActivity extends Activity mButtonManager = null; CameraAgentFactory.recycle(CameraAgentFactory.CameraApi.API_1); CameraAgentFactory.recycle(CameraAgentFactory.CameraApi.AUTO); + mSoundPlayer.release(); super.onDestroy(); } @@ -2175,6 +2180,11 @@ public class CameraActivity extends Activity return mButtonManager; } + @Override + public SoundPlayer getSoundPlayer() { + return mSoundPlayer; + } + /** * Creates an AlertDialog appropriate for choosing whether to enable * location on the first run of the app. diff --git a/src/com/android/camera/CaptureModule.java b/src/com/android/camera/CaptureModule.java index 011ecf1d6..497eb9a47 100644 --- a/src/com/android/camera/CaptureModule.java +++ b/src/com/android/camera/CaptureModule.java @@ -266,12 +266,6 @@ public class CaptureModule extends CameraModule private final File mDebugDataDir; /** CLEAN UP START */ - // private SoundPool mSoundPool; - // private int mCaptureStartSoundId; - // private static final int NO_SOUND_STREAM = -999; - // private final int mCaptureStartSoundStreamId = NO_SOUND_STREAM; - // private int mCaptureDoneSoundId; - // private SoundClips.Player mSoundPlayer; // private boolean mFirstLayout; // private int[] mTargetFPSRanges; // private float mZoomValue; diff --git a/src/com/android/camera/SoundPlayer.java b/src/com/android/camera/SoundPlayer.java new file mode 100644 index 000000000..3f17c5887 --- /dev/null +++ b/src/com/android/camera/SoundPlayer.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.camera; + +import android.content.Context; +import android.media.SoundPool; +import android.util.SparseIntArray; + +/** + * Loads a plays custom sounds. For playing system-standard sounds for various + * camera actions, please refer to {@link SoundClips}. + */ +public class SoundPlayer { + private final Context mAppContext; + private final SoundPool mSoundPool; + /** Keeps a mapping from sound resource ID to sound ID */ + private final SparseIntArray mResourceToSoundId = new SparseIntArray(); + + /** + * Construct a new sound player. + */ + public SoundPlayer(Context appContext) { + mAppContext = appContext; + final int audioType = SoundClips.getAudioTypeForSoundPool(); + mSoundPool = new SoundPool(1 /* max streams */, audioType, 0 /* quality */); + } + + /** + * Load the sound from a resource. + */ + public void loadSound(int resourceId) { + int soundId = mSoundPool.load(mAppContext, resourceId, 1/* priority */); + mResourceToSoundId.put(resourceId, soundId); + } + + /** + * Play the sound with the given resource. The resource has to be loaded + * before it can be played, otherwise an exception will be thrown. + */ + public void play(int resourceId, float volume) { + Integer soundId = mResourceToSoundId.get(resourceId); + if (soundId == null) { + throw new IllegalStateException("Sound not loaded. Must call #loadSound first."); + } + mSoundPool.play(soundId, volume, volume, 0 /* priority */, 0 /* loop */, 1 /* rate */); + } + + /** + * Unload the given sound if it's not needed anymore to release memory. + */ + public void unloadSound(int resourceId) { + Integer soundId = mResourceToSoundId.get(resourceId); + if (soundId == null) { + throw new IllegalStateException("Sound not loaded. Must call #loadSound first."); + } + mSoundPool.unload(soundId); + } + + /** + * Call this if you don't need the SoundPlayer anymore. All memory will be + * released and the object cannot be re-used. + */ + public void release() { + mSoundPool.release(); + } +} diff --git a/src/com/android/camera/app/AppController.java b/src/com/android/camera/app/AppController.java index e09ff4da2..f2739147d 100644 --- a/src/com/android/camera/app/AppController.java +++ b/src/com/android/camera/app/AppController.java @@ -28,6 +28,7 @@ import android.view.View; import android.widget.FrameLayout; import com.android.camera.ButtonManager; +import com.android.camera.SoundPlayer; import com.android.camera.module.ModuleController; import com.android.camera.one.OneCameraManager; import com.android.camera.settings.SettingsManager; @@ -367,6 +368,9 @@ public interface AppController { */ public ButtonManager getButtonManager(); + /** Returns a sound player that can be used to play custom sounds. */ + public SoundPlayer getSoundPlayer(); + /** Whether auto-rotate is enabled.*/ public boolean isAutoRotateScreen(); diff --git a/src/com/android/camera/one/OneCameraManager.java b/src/com/android/camera/one/OneCameraManager.java index dc83d143b..4a4ed6e52 100644 --- a/src/com/android/camera/one/OneCameraManager.java +++ b/src/com/android/camera/one/OneCameraManager.java @@ -88,7 +88,7 @@ public abstract class OneCameraManager { .getMaxAllowedNativeMemoryAllocation(); if (cameraManager != null && isCamera2Supported(cameraManager)) { return new com.android.camera.one.v2.OneCameraManagerImpl(cameraManager, maxMemoryMB, - displayMetrics); + displayMetrics, activity.getSoundPlayer()); } else { return new com.android.camera.one.v1.OneCameraManagerImpl(); } diff --git a/src/com/android/camera/one/v2/OneCameraManagerImpl.java b/src/com/android/camera/one/v2/OneCameraManagerImpl.java index c9ef11527..07112e506 100644 --- a/src/com/android/camera/one/v2/OneCameraManagerImpl.java +++ b/src/com/android/camera/one/v2/OneCameraManagerImpl.java @@ -22,6 +22,7 @@ import android.hardware.camera2.CameraDevice; import android.hardware.camera2.CameraManager; import android.util.DisplayMetrics; +import com.android.camera.SoundPlayer; import com.android.camera.debug.Log; import com.android.camera.debug.Log.Tag; import com.android.camera.one.OneCamera; @@ -38,6 +39,7 @@ public class OneCameraManagerImpl extends OneCameraManager { private final CameraManager mCameraManager; private final int mMaxMemoryMB; private final DisplayMetrics mDisplayMetrics; + private final SoundPlayer mSoundPlayer; /** * Instantiates a new {@link OneCameraManager} for Camera2 API. @@ -47,10 +49,11 @@ public class OneCameraManagerImpl extends OneCameraManager { * during capture and processing, in megabytes. */ public OneCameraManagerImpl(CameraManager cameraManager, int maxMemoryMB, - DisplayMetrics displayMetrics) { + DisplayMetrics displayMetrics, SoundPlayer soundPlayer) { mCameraManager = cameraManager; mMaxMemoryMB = maxMemoryMB; mDisplayMetrics = displayMetrics; + mSoundPlayer = soundPlayer; } @Override @@ -78,7 +81,8 @@ public class OneCameraManagerImpl extends OneCameraManager { .getCameraCharacteristics(device.getId()); // TODO: Set boolean based on whether HDR+ is enabled. OneCamera oneCamera = OneCameraCreator.create(useHdr, device, - characteristics, pictureSize, mMaxMemoryMB, mDisplayMetrics); + characteristics, pictureSize, mMaxMemoryMB, mDisplayMetrics, + mSoundPlayer); openCallback.onCameraOpened(oneCamera); } catch (CameraAccessException e) { Log.d(TAG, "Could not get camera characteristics"); |