Reader small image

You're reading from  Android 9 Development Cookbook - Third Edition

Product typeBook
Published inOct 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788991216
Edition3rd Edition
Languages
Tools
Right arrow
Author (1)
Rick Boyer
Rick Boyer
author image
Rick Boyer

Rick Boyer has been programming professionally for over 20 years. He has written apps on Windows, created websites, and coded for various mobile devices, including Windows CE, Windows Phone, and Android. Almost eight years ago, he took the plunge and started his own software consulting business, NightSky Development, focusing exclusively on Android development.
Read more about Rick Boyer

Right arrow

Chapter 12. Multimedia

In this chapter, we will cover the following topics:

  • Playing sound effects with SoundPool
  • Playing audio with MediaPlayer
  • Responding to hardware media controls in your app
  • Taking a photo with the default camera app
  • Taking a picture using the Camera2 API

Introduction


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 clips
  • MediaPlayer: 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).

Playing sound effects with SoundPool


When you needsound effects in your application, SoundPoolis 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.SoundPoolis no exception and the originalSoundPoolconstructor 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...

Playing audio with MediaPlayer


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.)

Getting ready

Create a new project in Android Studio and call itMediaPlayer. Use the default Phone & Tablet options and select Empty Activity when prompted for...

Responding to hardware media controls in your app


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 theSoundPoolexample, this recipe is able to take advantage of another approach, the compatibility library.

This recipe will show you how to set upMediaSessionto respond to the hardware buttons, which will work on Lollipop and later, as well as previous Lollipop versions using theMediaSessionCompatlibrary. (The compatibility library will take care of checking the OS version and using the correct API calls automatically.)

Getting ready

Create a new project in Android Studio and call itHardwareMediaControls.  Use the default Phone & Tablet options and select Empty Activity on the Add an Activityto Mobile dialog.

How to do it...

We...

Taking a photo with the default camera app


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.

Getting ready

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.

How to do it...

We're going to create a layout with an ImageView and button. The button will create an Intent to launch the default Camera app. When the camera app is done, our app will get a callback. We'll check...

Taking a picture using the Camera2 API


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.)

Getting ready

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.

How to do it...

As you'll see, there's a lot of code for this recipe. Start by opening the Android Manifest and following these steps:

  1. Add the following two permissions:
<uses-permission android:name="android.permission.CAMERA...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android 9 Development Cookbook - Third Edition
Published in: Oct 2018Publisher: PacktISBN-13: 9781788991216
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Rick Boyer

Rick Boyer has been programming professionally for over 20 years. He has written apps on Windows, created websites, and coded for various mobile devices, including Windows CE, Windows Phone, and Android. Almost eight years ago, he took the plunge and started his own software consulting business, NightSky Development, focusing exclusively on Android development.
Read more about Rick Boyer