You're reading from Android 9 Development Cookbook - Third Edition
Now that we've explored graphics and animations in the previous chapters, it's time to look at the sound options available in Android. The two most popular options to play sound are the following:
SoundPool
: This is for short sound clipsMediaPlayer
: This isdesigned for larger sound files (such as music) and video files
The first two recipes will look at using these libraries. We'll also look at how to use hardware related to sound, such as the volume controls and media playback controls (play, pause, next and previous, often available on headphones).
The rest of the chapter will focus on using the camera, both indirectly through Intents (to pass the camera request to the default camera application) and directly using the camera APIs. We'll show a complete example using the Camera2 APIs released with Android 5.0 Lollipop (API 21).
When you needsound effects in your application, SoundPool
is usually a good starting point.
SoundPool
is interesting in that it allows us to create special effects with our sounds by changing the play rate and by allowing multiple sounds to play simultaneously.
Popular audio file types supported include:
- 3GPP (
.3gp
) - 3GPP (
.3gp
) - FLAC (
.flac
) - MP3 (
.mp3
) - MIDI Type 0 and 1 (
.mid
,.xmf
, and.mxmf
) - Ogg (
.ogg
) - WAVE (
.wav
)
See the Supported Media Formats link for a complete list, including network protocols.
As is common in Android, new releases to the OS bring changes to the APIs.SoundPool
is no exception and the originalSoundPool
constructor was deprecated in Lollipop (API 21). Rather than setting our minimum API to 21 or relying on deprecated code (which may stop working at some point), we'll implement both the old and the new approach and check the OS version at runtime to use the appropriate method.
This recipe will demonstrate how to play sound effects using the Android...
MediaPlayeris probably one of the most important classes for adding multimedia capability to your applications. It supports the following media sources:
- Project resources
- Local files
- External resources (such as URLs, including streaming)
MediaPlayer supports the following popularaudio files:
- 3GPP (
.3gp
) - 3GPP (
.3gp
) - FLAC (
.flac
) - MP3 (
.mp3
) - MIDI Type 0 and 1 (
.mid
,.xmf
, and.mxmf
) - Ogg (
.ogg
) - WAVE (
.wav
)
And it supports these popularfile types:
- 3GPP (
.3gp
) - Matroska (
.mkv
) - WebM (
.webm
) - MPEG-4 (
.mp4
,.m4a
)
See the Supported Media Formats link for a complete list, including network protocols.
This recipe willdemonstratehow to set up MediaPlayer
in your app to play a sound included with your project. (For a complete review of the full capability offered by MediaPlayer
, see the Developer Docs link at the end of this recipe.)
Having your app respond tomedia controls (like on headphones), such as Play, Pause, Skip, and so on, is a nice touch your users will appreciate.Android makes this possible through the media library. As with the Playing sound effects with SoundPool recipe earlier, the Lollipop release changed how this is done. Unlike theSoundPool
example, this recipe is able to take advantage of another approach, the compatibility library.
This recipe will show you how to set upMediaSession
to respond to the hardware buttons, which will work on Lollipop and later, as well as previous Lollipop versions using theMediaSessionCompat
library. (The compatibility library will take care of checking the OS version and using the correct API calls automatically.)
Create a new project in Android Studio and call itHardwareMediaControls
. Use the default Phone & Tablet
options and select Empty Activity
on the Add an Activity
to Mobile
dialog.
If your application needs animage from the camera, but is not a camerareplacement app, it may be better to allow the default camera app to take the picture. This also respects your user's preferred camera application.
When you take a photo, unless it is specific to your application, it's considered good practice to make the photo publicly available. (This allows it to be included in the user's photo gallery.) This recipe will demonstrate using the default photo application to click a picture, save it to the public folder, and display the image.
Create a new project in Android Studio and call itUsingTheDefaultCameraApp
. Use the default Phone & Tablet
options and select Empty Activity
on the Add an Activity
to Mobile
dialog.
The previous recipe demonstrated how to use an Intent to call the default photo application. If you only need a quick photo, the Intent is probably the ideal solution. If not, and you need more control over the camera, this recipe will show you how to use the camera directly with the Camera2 API.
Now that 85% of devices are using Android 5.0 or later, this recipe focuses only on the Camera2 API. (Google has already deprecated the original Camera API.)
Create a new project in Android Studio and call itCamera2API. In the Target Android
Devices dialog, select the Phone & Tablet
option and choose API 21: Android 5.0 (Lollipop)
, or later, for the minimum SDK. Select Empty Activity on the Add an Activity to Mobile dialog.