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 6704957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:20:54+00:00 2026-05-26T07:20:54+00:00

I have read several of the articles on here that suggest using the singleton

  • 0

I have read several of the articles on here that suggest using the singleton code, Which I have read and placed into the soundboard as I want to use the singleton code to only allow one mediaplayer instance no matter how many clicks the user defines.

Basically I only want one sound and if the user clicks another button during playback it stops the current sound and plays the one that is pressed. I want only the one MediaPlayer instance but do not understand how to implement it.

Here is the basic idea of my code:

    package com.example.context;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;




    import android.app.Activity;
    import android.content.ContentValues;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.view.ContextMenu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ContextMenu.ContextMenuInfo;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;

    public class main extends Activity implements OnClickListener {

    MediaPlayer player;

    int[] ressound ={R.raw.boomstick, R.raw.chainsaw, R.raw.shebitch, R.raw.byebye,
    R.raw.comegetsome, R.raw.groovy, R.raw.shoelace, R.raw.smart,   R.raw.yeahbaby};//added as needed
    int j=0;
    private static final String TAG = "MyTag";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


     //Coding for all buttons, registers, and Listeners
        Button btn1 = (Button) findViewById(R.id.btn1);
        registerForContextMenu(btn1); 
        btn1.setOnClickListener(this);
        Button btn2 = (Button) findViewById(R.id.btn2);
        registerForContextMenu(btn2);
        btn2.setOnClickListener(this);
        Button btn3 = (Button) findViewById(R.id.btn3);
        registerForContextMenu(btn3);
        btn3.setOnClickListener(this);

    }

     //On click Handlers for multiple buttons 
        public void onClick(View v) {
        switch(v.getId()){
        case R.id.btn1:
        // action to perform on button 1
        j = 0;
        playResource();
        break;
    case R.id.btn2:
        // action to perform on button 1
        j = 1;
        playResource();
        break;
    case R.id.btn3:
        // action to perform on button 1    
        j = 2;
        playResource();
        break;

    }
         public void playResource(int j, int resource) {
    this.j = j;
    if (player != null) {
        if (player.isPlaying())
            player.stop();
        player.reset();
        //from MediaPlayer implementation (link above)
        try {
            AssetFileDescriptor afd = getResources().openRawResourceFd(resource);
            if (afd == null) return;
            player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            afd.close();
            player.prepare();
        } catch (IOException ex) {
            Log.d(TAG, "create failed:", ex);
            // failed: return
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "create failed:", ex);
            // failed: return
        } catch (SecurityException ex) {
            Log.d(TAG, "create failed:", ex);
            // failed: return
        }
    }
    else {
        //player is null
        //it will create new MediaPlayer instance, setDataSource and call prepare
        player = MediaPlayer.create(this, resource);
    }
    //if everything ok play file
    //in case of any error return from method before (catch)
    player.start();
}

This is what I had to update and change my code too but it is giving me a problem with passing the playresource() function. Am I passing it wrong, should it be private that is passing.

  • 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-26T07:20:55+00:00Added an answer on May 26, 2026 at 7:20 am

    You don’t need a Singleton for that. What are you doing is calling create method every time you want to play a file. That’s wrong because you already have MediaPlayer instance.
    Check MediaPlayer.create implementation.
    I would do like that:

    • create new method playResource(int j, int resource)
    • in every case R.id.btnX i would call method playResource(X, R.raw.Y) – X and Y depends on btn

    playResource method example:

    private static final String TAG = "MyTag";
    playResource(int j, int resource) {
        this.j = j;
        if (player != null) {
            if (player.isPlaying())
                player.stop();
            player.reset();
            //from MediaPlayer implementation (link above)
            try {
                AssetFileDescriptor afd = getResources().openRawResourceFd(resource);
                if (afd == null) return null;
                player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                afd.close();
                player.prepare();
            } catch (IOException ex) {
                Log.d(TAG, "create failed:", ex);
                // failed: return
            } catch (IllegalArgumentException ex) {
                Log.d(TAG, "create failed:", ex);
                // failed: return
            } catch (SecurityException ex) {
                Log.d(TAG, "create failed:", ex);
                // failed: return
            }
        }
        else {
            //player is null
            //it will create new MediaPlayer instance, setDataSource and call prepare
            player = MediaPlayer.create(this, resource);
        }
        //if everything ok play file
        //in case of any error return from method before (catch)
        player.start();
    }
    

    When you don’t need MediaPlayer again, you should release it. For example in onPause() call:

    if (player != null) {
        player.release();
        player = null;
    } 
    

    I didn’t test that so there could be errors. Hope it helps.

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

Sidebar

Related Questions

I have read several articles here and else where that it is OK to
Several articles and blogs I have read suggest placing assemblies I wish to make
I want to enable NSZombies for my iPhone app. I have read several articles
I have read several articles on when to use nested classes, but none that
I have read in several places that it's possible to share the objects directory
I'm quite confused about several books in .NET that I have read. Would someone
I have read that using database keys in a URL is a bad thing
I have read several articles/questions/forums discussing the best auto-complete plugin for jQuery. After trying
I have an article system which is used by several users. Articles have their
I have read several articles and several stackoverflow.com posts about expression tree. It is

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.