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.
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:
playResource method example:
When you don’t need MediaPlayer again, you should release it. For example in onPause() call:
I didn’t test that so there could be errors. Hope it helps.