Easy Tutorial
❮ Cpp Static Usage Programmer Mobile Wallpaper ❯

9.1 Playing Sound Effects with SoundPool (Duang~)

Category Android Basic Tutorial

Introduction:

>

Chapter Nine introduces multimedia development in Android, or more accurately, the use of multimedia-related APIs. In practical development, we deal with multimedia in activities like taking photos, recording audio, playing music, and playing videos...

Well, that seems to be about it. For example, playing music involves using MediaPlayer, locating the music file, and calling the play method... Of course, true multimedia development is another field involving encoding and decoding audio and video, which we can only admire for now. We just need to know how to use these APIs! By the way, let's briefly introduce some common knowledge about the Android multimedia framework:

On Android, the default multimedia framework is OpenCore. OpenCore's advantage is its cross-platform portability and stability due to extensive validation; however, its disadvantage is its complexity, requiring considerable time to maintain. Starting from Android 2.0, Google introduced a slightly simpler framework, Stagefright, though it did not completely abandon OpenCore. It mainly added an OMX layer, referencing only the omx-component part of OpenCore. Initially, it seemed to be replacing OpenCORE, but a Stagefright vulnerability discovered in August allowed remote code execution through a specially crafted MMS message.

This vulnerability affected Android 2.2 and later versions, with weaker impact on 4.1 and later versions. Feels awesome but doesn't know why (what the heck are they talking about), well, that's enough科普... Just know this stuff!

This multimedia framework is located in the third layer of the Android architecture, Libraries, specifically in Media Framework! Additionally, if you want to know what types of audio and video data Android's multimedia framework supports, refer to the official documentation:

Supported Media Formats

You can directly click Media and Camera and then refer to the documentation below:

soundID.put(3, mSoundPool.load(this, R.raw.duang, 1));
soundID.put(4, mSoundPool.load(this, R.raw.duang, 1));
soundID.put(5, mSoundPool.load(this, R.raw.duang, 1));
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btn_play1:
            mSoundPool.play(soundID.get(1), 1, 1, 0, 0, 1);
            break;
        case R.id.btn_play2:
            mSoundPool.play(soundID.get(2), 1, 1, 0, 0, 1);
            break;
        case R.id.btn_play3:
            mSoundPool.play(soundID.get(3), 1, 1, 0, 0, 1);
            break;
        case R.id.btn_play4:
            mSoundPool.play(soundID.get(4), 1, 1, 0, 0, 1);
            break;
        case R.id.btn_play5:
            mSoundPool.play(soundID.get(5), 1, 1, 0, 0, 1);
            break;
        case R.id.btn_release:
            mSoundPool.release();   // Release SoundPool resources
            break;
    }
}
}

The code is straightforward. If you click the last button, the SoundPool will be released, and then the other buttons won't produce the "Duang" sound anymore.


4. OnLoadCompleteListener to monitor sound file loading completion

>

This is something I thought of later. After writing the initial code, I realized we could add the OnLoadCompleteListener to the existing code. We just need to override the onLoadComplete() method and set this listener to the SoundPool object.

mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
        Toast.makeText(MainActivity.this, "Special effects are ready~", Toast.LENGTH_SHORT).show();
    }
});

5. Sample code download:

SoundPoolDemo.zip


Summary:

>

This section introduced some basics of Android multimedia and taught you how to add sound effects to your app using SoundPool. Why wait? Add this feature to your app and make it "Duang" with excitement! -2.6.2 Menu (Menu)

-2.6.3 Simple Usage of ViewPager

-2.6.4 Simple Usage of DrawerLayout (Official Side-Sliding Menu)

-3.1.1 Event Handling Mechanism Based on Listeners

-3.2 Event Handling Mechanism Based on Callbacks

-3.3 Brief Analysis of Handler Message Passing Mechanism

-3.4 TouchListener vs OnTouchEvent + Multi-Touch

-3.5 Listening for Content Changes in EditText

-3.6 Responding to System Setting Events (Configuration Class)

-3.7 AsyncTask Asynchronous Task

-3.8 Gestures (Gestures)

-4.1.1 Introduction to Activity

-4.1.2 Getting Started with Activity

-4.1.3 Advanced Activity

-4.2.1 Introduction to Service

-4.2.2 Intermediate Service

-4.2.3 Advanced Service

-4.3.1 BroadcastReceiver Basics

-4.3.2 In-Depth BroadcastReceiver

-4.4.1 Introduction to ContentProvider

-4.4.2 Further Exploration of ContentProvider – Document Provider

-4.5.1 Basic Usage of Intent

-4.5.2 Passing Complex Data with Intent

-5.1 Basic Overview of Fragment

-5.2.1 Fragment Example Analysis – Bottom Navigation Bar Implementation (Method 1)

-5.2.2 Fragment Example Analysis – Bottom Navigation Bar Implementation (Method 2)

-5.2.3 Fragment Example Analysis – Bottom Navigation Bar Implementation (Method 3)

-5.2.4 Fragment Example Analysis – Bottom Navigation Bar + ViewPager Swipe to Switch Pages

-5.2.5 Fragment Example Analysis – Simple Implementation of News (Shopping) App List Fragment

-6.1 Data Storage and Access – File Storage and Reading

-6.2 Data Storage and Access – SharedPreferences for Saving User Preferences

-6.3.1 Data Storage and Access – Introduction to SQLite Database

-6.3.2 Data Storage and Access – Further Exploration of SQLite Database

-7.1.1 Android Network Programming Essentials and Learning HTTP Protocol

-7.1.2 Learning Android HTTP Request Headers and Response Headers

-7.1.3 Android HTTP Request Method: HttpURLConnection

-7.1.4 Android HTTP Request Method: HttpClient

-7.2.1 Android XML Data Parsing

-7.2.2 Android JSON Data Parsing

-7.3.1 Android File Upload

-7.3.2 Android File Download (1)

-7.3.3 Android File Download (2)

-7.4 Android Calling WebService

-7.5.1 Basic Usage of WebView (Web View)

-7.5.2 Basic Interaction Between WebView and JavaScript

-7.5.3 Considerations for WebView in Android 4.4 and Later

-7.5.4 WebView File Download

-7.5.5 WebView Cache Issues

-7.5.6 WebView Handling Error Code Information from Web Pages

-7.6.1 Socket Learning Network Basics Preparation

-7.6.2 TCP Protocol Based Socket Communication (1)

-7.6.3 TCP Protocol Based Socket Communication (2)

-7.6.4 UDP Protocol Based Socket Communication

-8.1.1 Summary of 13 Drawable Types in Android Part 1

-8.1.2 Summary of 13 Drawable Types in Android Part 2

-8.1.3 Summary of 13 Drawable Types in Android Part 3

-8.2.1 Comprehensive Analysis of Bitmap (Bitmap) Part 1

-8.2.2 OOM Issues Caused by Bitmap

-8.3.1 Detailed Explanation of Three Drawing Tool Classes

-8.3.2 Practical Examples of Drawing Classes

-8.3.3 Paint API – MaskFilter (Mask)

-8.3.4 Paint API – Xfermode and PorterDuff Detailed Explanation (Part 1)

-8.3.5 Paint API – Xfermode and PorterDuff Detailed Explanation (Part 2)

-8.3.6 Paint API – Xfermode and PorterDuff Detailed Explanation (Part 3)

-8.3.7 Paint API – Xfermode and PorterDuff Detailed Explanation (Part 4)

-8.3.8 Paint API – Xfermode and PorterDuff Detailed Explanation (Part 5)

-8.3.9 Paint API – ColorFilter (Color Filter) (1/3)

-8.3.10 Paint API – ColorFilter (Color Filter) (2/3)

-8.3.11 Paint API – ColorFilter (Color Filter) (3/3)

-8.3.12 Paint API – PathEffect (Path Effect)

-8.3.13 Paint API – Shader (Image Rendering)

-8.3.14 Paint Enum/Constants and ShadowLayer Shadow Effect

-8.3.15 Paint API – Typeface (Font Style)

-8.3.16 Canvas API Detailed Explanation (Part 1)

WeChat Subscription

❮ Cpp Static Usage Programmer Mobile Wallpaper ❯