Book Image

Android NDK: Beginner's Guide

By : Sylvain Ratabouil
Book Image

Android NDK: Beginner's Guide

By: Sylvain Ratabouil

Overview of this book

Table of Contents (18 chapters)
Android NDK Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – creating and playing a sound buffer queue


Let's use OpenSL ES to play an explosion sound stored in a memory buffer:

  1. Update jni/Resource.hpp again to add a new method getLength(), which provides the size in bytes of an asset file:

    ...
    class Resource {
    public:
        ...
    
        ResourceDescriptor descriptor();
        off_t getLength();
        ...
    };
    
    #endif
  2. Implement this method in jni/Resource.cpp:

    ...
    off_t Resource::getLength() {
        return AAsset_getLength(mAsset);
    }
    ...
  3. Create jni/Sound.hpp to manage a sound buffer.

    Define a method load() to load a PCM file and unload() to release it.

    Also, define the appropriate getters. Hold the raw sound data in a buffer along with its size. The sound is loaded from a Resource:

    #ifndef _PACKT_SOUND_HPP_
    #define _PACKT_SOUND_HPP_
    
    class SoundManager;
    
    #include "Resource.hpp"
    #include "Types.hpp"
    
    class Sound {
    public:
        Sound(android_app* pApplication, Resource* pResource);
    
        const char* getPath();
        uint8_t* getBuffer() { return mBuffer; };
    ...