Reputation: 1119
I am creating a soundboard app. The app will feature different "pages" (fragments within viewpager) that the user can switch between. Each fragment has a number of ImageButtons that, when clicked on, will play a certain sound.
In each of my fragment classes I put the following code in its OnCreateView method like the following example below.
public static class FragmentPage1 extends Fragment {
int selectedSoundId;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_page1, container, false);
final MediaPlayer player = new MediaPlayer();
final Resources res = getResources();
final int[] buttonIds = { R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9 };
final int[] soundIds = { R.raw.sound1, R.raw.sound2, R.raw.sound3, R.raw.sound4, R.raw.sound5, R.raw.sound6, R.raw.sound7, R.raw.sound8, R.raw.sound9 };
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < buttonIds.length; i++) {
if (v.getId() == buttonIds[i]) {
selectedSoundId = soundIds[i];
AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]);
player.reset();
try {
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
player.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.start();
break;
}
}
}
};
for (int i = 0; i < buttonIds.length; i++) {
ImageButton soundButton = (ImageButton) rootView.findViewById(buttonIds[i]);
registerForContextMenu(soundButton);
soundButton.setOnClickListener(listener);
}
return rootView;
}
}
I have twelve different fragments that have a set of nine buttons on each. So as you can see I have twelve instances of this MediaPlayer code (one in each fragment). I've been trying to find a way to simplify this but I have so far come up with nothing.
How can I edit my code to make it so that I only have to use one instance of this MediaPlayer code?
Upvotes: 4
Views: 1079
Reputation: 1873
You can use singleton pattern, like this:
public class MediaPlayerSingleton extends MediaPlayer{
private static MediaPlayerSingleton mediaPlayerSingleton;
private MediaPlayerSingleton() {}
public static MediaPlayerSingleton getInstance() {
synchronized (mediaPlayerSingleton) { // if you'll be using it in moe then one thread
if(mediaPlayerSingleton == null)
mediaPlayerSingleton = new MediaPlayerSingleton();
}
return mediaPlayerSingleton;
}
}
And then use your player anywhere you want like this:
MediaPlayerSingleton mediaPlayer = MediaPlayerSingleton.getInstance();
mediaPlayer.reset(); // some call
Upvotes: 3