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 – playing background music


Let's open and play an MP3 music file with OpenSL ES:

  1. MP3 files are opened by OpenSL using a POSIX file descriptor pointing to the chosen file. Improve jni/ResourceManager.cpp created in the previous chapters by defining a new structure ResourceDescriptor and appending a new method descriptor():

    ...
    struct ResourceDescriptor {
        int32_t mDescriptor;
        off_t mStart;
        off_t mLength;
    };
    
    class Resource {
    public:
        ...
        status open();
        void close();
        status read(void* pBuffer, size_t pCount);
    
        ResourceDescriptor descriptor();
    
        bool operator==(const Resource& pOther);
    
    private:
        ...
    };
    #endif
  2. Implement jni/ResourceManager.cpp. Of course, makes use of the asset manager API to open the descriptor and fill a ResourceDescriptor structure:

    ...
    ResourceDescriptor Resource::descriptor() {
        ResourceDescriptor lDescriptor = { -1, 0, 0 };
        AAsset* lAsset = AAssetManager_open(mAssetManager, mPath,
                                ...