Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6790779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:45:58+00:00 2026-05-26T17:45:58+00:00

I wrote this code in order to make a soundboard like app public class

  • 0

I wrote this code in order to make a soundboard like app

public class ButtonSoundActivity extends Activity implements OnClickListener {
    private Button buttons[];
    private MediaPlayer mediaPlayers[];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);     
    }

    @Override
    protected void onPause() {
        super.onPause();
        releaseSounds();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initContent();
    }

    @Override
    protected void onStop() {
        super.onStop();
        releaseSounds();
    }

    /**
     * Initialise all the content :
     * buttons, media players and set click listeners for the buttons
     */
    private void initContent() {
        buttons = new Button[5];
        mediaPlayers = new MediaPlayer[5];

        buttons[0] = (Button)findViewById(R.id.button0);
        buttons[1] = (Button)findViewById(R.id.button1);
        buttons[2] = (Button)findViewById(R.id.button2);
        buttons[3] = (Button)findViewById(R.id.button3);
        buttons[4] = (Button)findViewById(R.id.button4);

        mediaPlayers[0] = MediaPlayer.create(ButtonSoundActivity.this, R.raw.sound1);
        mediaPlayers[1] = MediaPlayer.create(ButtonSoundActivity.this, R.raw.sound2);
        mediaPlayers[2] = MediaPlayer.create(ButtonSoundActivity.this, R.raw.sound3);
        mediaPlayers[3] = MediaPlayer.create(ButtonSoundActivity.this, R.raw.sound4);
        mediaPlayers[4] = MediaPlayer.create(ButtonSoundActivity.this, R.raw.sound5);


        for(int i=0; i<5; i++) {
            buttons[i].setOnClickListener(this);
        }      
    }

    public void onClick(View v) {
        stopSounds();
        for(int i=0; i<5; i++)
            if(v.getId() == buttons[i].getId()) {
                if(mediaPlayers[i] != null)
                mediaPlayers[i].start();
            }
    }

    /**
     * Stop the previous sound when another button is clicked
     * so that the sounds don't overlap
     */
    private void stopSounds() {
        for(int i=0; i<5 ;i++)
            if(mediaPlayers[i] != null)
            if(mediaPlayers[i].isPlaying()) {
                mediaPlayers[i].pause();
                mediaPlayers[i].seekTo(0);
            }
    }

    /**
     * Release all sounds and empty the mediaPlayers array
     */
    private void releaseSounds() {
        for(int i=0; i<5;i++) {
            if(mediaPlayers[i] != null) {
                mediaPlayers[i].stop();
                mediaPlayers[i].release();
                mediaPlayers[i] = null;                 
            }
        }
    }       
}

the xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="button0" android:id="@+id/button0"/>

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="button1" android:id="@+id/button1"/>

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="button2" android:id="@+id/button2"/>      

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="button3" android:id="@+id/button3"/>

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="button4" android:id="@+id/button4"/>      
</LinearLayout>

The problem is that if I click a button multiple times in a row, the sound doesn’t play anymore, or sometime, when I first open the app, it crashes when I click a button.
The error says it’s related to the stopSounds method

error code

So, does anyone have an answer?

Edit: I added the if() before each mediaPlayer element to check if they are null, also I followed this method:

            AssetFileDescriptor fileDescriptor = getResources().openRawResourceFd(R.raw.sound1);
        mediaPlayers[0] = new MediaPlayer();

        try {
            mediaPlayers[0].setDataSource(fileDescriptor.getFileDescriptor());
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            mediaPlayers[0].prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

but without any luck 🙁

Edit 2: I got it working 🙂 , I just had to make sure that the mediaPlayer array is empty when I restart the app. Also I added the onPause() and onResume() functions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:45:59+00:00Added an answer on May 26, 2026 at 5:45 pm

    You are getting a NullPointerException at line 57, inside your stopSounds method. Could you post this line all by itself?

    Most likely one of the objects in mediaPlayers is null. Try stepping through the debugger and see what the array looks like when you first enter the stopSounds method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My C# code has a lot of statement like this.Name=.... In order to make
i have a class like this: class myclass { public function save($params){ // some
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
I wrote this code in C# to encrypt a string with a key: private
I wrote this code. The constructor works normally, but in the destructor I get
If I wrote this code: typeof(myType).TypeHandle Would it use reflection? How much different from:
Hi guys I wrote this code and i have two errors. Invalid rank specifier:
I was trying to understand something with pointers, so I wrote this code: #include
I want to convert function object to function. I wrote this code, but it
I wrote this tiny code: #include <stdio.h> int main() { size_t temp; temp =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.