Playing MediaPlayer multiple times












0














I have the following class to play a Piano note (A, C, D, G), whenever a button is clicked:



public class AudioSoundPlayer {

private SparseArray<PlayThread> threadMap;
private Context context;
private static final SparseArray<String> SOUND_MAP = new SparseArray<>();
public static final int MAX_VOLUME = 100, CURRENT_VOLUME = 90;

static {
SOUND_MAP.put(1, "G");
SOUND_MAP.put(2, "D");
SOUND_MAP.put(3, "C");
SOUND_MAP.put(4, "A");
}

public AudioSoundPlayer(Context context) {
this.context = context;
threadMap = new SparseArray<>();
}

public void playNote(int note) {
if (!isNotePlaying(note)) {
PlayThread thread = new PlayThread(note);
thread.start();
threadMap.put(note, thread);
}
}

public void stopNote(int note) {
PlayThread thread = threadMap.get(note);

if (thread != null) {
threadMap.remove(note);
}
}

public boolean isNotePlaying(int note) {
return threadMap.get(note) != null;
}

private class PlayThread extends Thread {
int note;
MediaPlayer player;

public PlayThread(int note) {
this.note = note;
}

@Override
public void run() {
try {
String path = SOUND_MAP.get(note) + ".wav";
AssetManager assetManager = context.getAssets();
AssetFileDescriptor afd = assetManager.openFd(path);

player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
afd.getLength());
player.prepare();
player.start();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
//mp.reset();
// mp.release();
player.reset();
player.release();
player = null;
}
});
player.setLooping(false);

} catch (Exception e) {

} finally {

}
}
}

}


In my activity, I have 4 buttons where in each one of them I have a setOnTouchListener with soundPlayer.playNote(1); and soundPlayer.stopNote(1); when Action is Down or Up respectively. It works nicely if I play cool but as soon as I start pushing it and playing faster, it starts not working properly and returning me only the error "E/MediaPlayer: Error (1,-19)". I'm not sure if this way of playing sounds is the best option for what I'm doing any suggestions?










share|improve this question






















  • Maybe there is a possible answer here -> stackoverflow.com/a/18363031/9164071
    – Skemelio
    Nov 11 at 18:27
















0














I have the following class to play a Piano note (A, C, D, G), whenever a button is clicked:



public class AudioSoundPlayer {

private SparseArray<PlayThread> threadMap;
private Context context;
private static final SparseArray<String> SOUND_MAP = new SparseArray<>();
public static final int MAX_VOLUME = 100, CURRENT_VOLUME = 90;

static {
SOUND_MAP.put(1, "G");
SOUND_MAP.put(2, "D");
SOUND_MAP.put(3, "C");
SOUND_MAP.put(4, "A");
}

public AudioSoundPlayer(Context context) {
this.context = context;
threadMap = new SparseArray<>();
}

public void playNote(int note) {
if (!isNotePlaying(note)) {
PlayThread thread = new PlayThread(note);
thread.start();
threadMap.put(note, thread);
}
}

public void stopNote(int note) {
PlayThread thread = threadMap.get(note);

if (thread != null) {
threadMap.remove(note);
}
}

public boolean isNotePlaying(int note) {
return threadMap.get(note) != null;
}

private class PlayThread extends Thread {
int note;
MediaPlayer player;

public PlayThread(int note) {
this.note = note;
}

@Override
public void run() {
try {
String path = SOUND_MAP.get(note) + ".wav";
AssetManager assetManager = context.getAssets();
AssetFileDescriptor afd = assetManager.openFd(path);

player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
afd.getLength());
player.prepare();
player.start();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
//mp.reset();
// mp.release();
player.reset();
player.release();
player = null;
}
});
player.setLooping(false);

} catch (Exception e) {

} finally {

}
}
}

}


In my activity, I have 4 buttons where in each one of them I have a setOnTouchListener with soundPlayer.playNote(1); and soundPlayer.stopNote(1); when Action is Down or Up respectively. It works nicely if I play cool but as soon as I start pushing it and playing faster, it starts not working properly and returning me only the error "E/MediaPlayer: Error (1,-19)". I'm not sure if this way of playing sounds is the best option for what I'm doing any suggestions?










share|improve this question






















  • Maybe there is a possible answer here -> stackoverflow.com/a/18363031/9164071
    – Skemelio
    Nov 11 at 18:27














0












0








0







I have the following class to play a Piano note (A, C, D, G), whenever a button is clicked:



public class AudioSoundPlayer {

private SparseArray<PlayThread> threadMap;
private Context context;
private static final SparseArray<String> SOUND_MAP = new SparseArray<>();
public static final int MAX_VOLUME = 100, CURRENT_VOLUME = 90;

static {
SOUND_MAP.put(1, "G");
SOUND_MAP.put(2, "D");
SOUND_MAP.put(3, "C");
SOUND_MAP.put(4, "A");
}

public AudioSoundPlayer(Context context) {
this.context = context;
threadMap = new SparseArray<>();
}

public void playNote(int note) {
if (!isNotePlaying(note)) {
PlayThread thread = new PlayThread(note);
thread.start();
threadMap.put(note, thread);
}
}

public void stopNote(int note) {
PlayThread thread = threadMap.get(note);

if (thread != null) {
threadMap.remove(note);
}
}

public boolean isNotePlaying(int note) {
return threadMap.get(note) != null;
}

private class PlayThread extends Thread {
int note;
MediaPlayer player;

public PlayThread(int note) {
this.note = note;
}

@Override
public void run() {
try {
String path = SOUND_MAP.get(note) + ".wav";
AssetManager assetManager = context.getAssets();
AssetFileDescriptor afd = assetManager.openFd(path);

player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
afd.getLength());
player.prepare();
player.start();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
//mp.reset();
// mp.release();
player.reset();
player.release();
player = null;
}
});
player.setLooping(false);

} catch (Exception e) {

} finally {

}
}
}

}


In my activity, I have 4 buttons where in each one of them I have a setOnTouchListener with soundPlayer.playNote(1); and soundPlayer.stopNote(1); when Action is Down or Up respectively. It works nicely if I play cool but as soon as I start pushing it and playing faster, it starts not working properly and returning me only the error "E/MediaPlayer: Error (1,-19)". I'm not sure if this way of playing sounds is the best option for what I'm doing any suggestions?










share|improve this question













I have the following class to play a Piano note (A, C, D, G), whenever a button is clicked:



public class AudioSoundPlayer {

private SparseArray<PlayThread> threadMap;
private Context context;
private static final SparseArray<String> SOUND_MAP = new SparseArray<>();
public static final int MAX_VOLUME = 100, CURRENT_VOLUME = 90;

static {
SOUND_MAP.put(1, "G");
SOUND_MAP.put(2, "D");
SOUND_MAP.put(3, "C");
SOUND_MAP.put(4, "A");
}

public AudioSoundPlayer(Context context) {
this.context = context;
threadMap = new SparseArray<>();
}

public void playNote(int note) {
if (!isNotePlaying(note)) {
PlayThread thread = new PlayThread(note);
thread.start();
threadMap.put(note, thread);
}
}

public void stopNote(int note) {
PlayThread thread = threadMap.get(note);

if (thread != null) {
threadMap.remove(note);
}
}

public boolean isNotePlaying(int note) {
return threadMap.get(note) != null;
}

private class PlayThread extends Thread {
int note;
MediaPlayer player;

public PlayThread(int note) {
this.note = note;
}

@Override
public void run() {
try {
String path = SOUND_MAP.get(note) + ".wav";
AssetManager assetManager = context.getAssets();
AssetFileDescriptor afd = assetManager.openFd(path);

player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
afd.getLength());
player.prepare();
player.start();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
//mp.reset();
// mp.release();
player.reset();
player.release();
player = null;
}
});
player.setLooping(false);

} catch (Exception e) {

} finally {

}
}
}

}


In my activity, I have 4 buttons where in each one of them I have a setOnTouchListener with soundPlayer.playNote(1); and soundPlayer.stopNote(1); when Action is Down or Up respectively. It works nicely if I play cool but as soon as I start pushing it and playing faster, it starts not working properly and returning me only the error "E/MediaPlayer: Error (1,-19)". I'm not sure if this way of playing sounds is the best option for what I'm doing any suggestions?







android android-mediaplayer






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 17:33









Mateus Barbosa

3010




3010












  • Maybe there is a possible answer here -> stackoverflow.com/a/18363031/9164071
    – Skemelio
    Nov 11 at 18:27


















  • Maybe there is a possible answer here -> stackoverflow.com/a/18363031/9164071
    – Skemelio
    Nov 11 at 18:27
















Maybe there is a possible answer here -> stackoverflow.com/a/18363031/9164071
– Skemelio
Nov 11 at 18:27




Maybe there is a possible answer here -> stackoverflow.com/a/18363031/9164071
– Skemelio
Nov 11 at 18:27

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251374%2fplaying-mediaplayer-multiple-times%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251374%2fplaying-mediaplayer-multiple-times%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation