Reader small image

You're reading from  Android Programming with Kotlin for Beginners

Product typeBook
Published inApr 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789615401
Edition1st Edition
Languages
Right arrow
Author (1)
John Horton
John Horton
author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton

Right arrow

Chapter 23. Android Sound Effects and the Spinner Widget

In this chapter, we will study the SoundPool class and the different ways we can use it depending on whether we just want to play sounds or go further and keep track of the sounds we are playing. Then, we will put everything we will have learned into action by producing a cool sound demo app, which will also introduce us to a new UI widget: the spinner.

In this chapter, we will do the following:

  • Learn how to use the Android SoundPool class

  • Code a sound-based app using SpinnerView

Let's get started.

The SoundPool class


The SoundPool class allows us to hold and manipulate a collection of sound effects: literally, a pool of sounds. The class handles everything from decompressing a sound file, such as a .wav or a .ogg file, keeping an identifying reference to it via an integer ID, and, of course, playing the sound. When the sound is played, it is played in a non-blocking manner (using a thread behind the scenes) that does not interfere with the smooth running of our app or our user's interaction with it.

The first thing we need to do is add the sound effects to a folder called assets in the main folder of the game project. We will do this shortly.

Next, in our Kotlin code, we declare an object of the SoundPool type and an Int identifier for each sound effect we intend to use, as shown in the following code. We will also declare another Int called nowPlaying, which we can use to track which sound is currently playing; we will see how we do this shortly:

var sp: SoundPool
var idFX1 = -1
nowPlaying...

Sound demo app introducing the Spinner widget


Of course, with all this talk of sound effects, we need some actual sound files. You can make you own with BFXR (as explained in the next section) or use the ones supplied. The sound effects for this app are in the download bundle, and can be found in the assets folder of the Chapter23/Sound Demo folder.

Making sound effects

There is an open source app called BFXR that allows us to make our own sound effects. Here is a very fast guide to making your own sound effects using BFXR. Grab a free copy from www.bfxr.net.

Note

Note that the sound effects for the Sound demo app are supplied to you in the Chapter23/assets folder. You don't have to create your own sound effects unless you want to, but it is still worth getting this free software and learning how to use it.

Follow the simple instructions on the website to set it up. Try out a few of these things to make cool sound effects:

Note

This is a seriously condensed tutorial. You can do so much with BFXR...

Summary


In this chapter, we looked closely at how to use SoundPool, and we used all this knowledge to complete the Sound demo app.

In the next chapter, we will learn about how to make our apps work with multiple different layouts.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Programming with Kotlin for Beginners
Published in: Apr 2019Publisher: PacktISBN-13: 9781789615401
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 AU $19.99/month. Cancel anytime

Author (1)

author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton