Android media player is playing over itself
I have a game with a single main music theme
that loops. On android if the user presses the home button the music will pause as desired, but if the user then reopens the app immediately the app will reset but two instances of the music theme will now play over each other and out of sync (as if the original instance was still playing in the background but silently). How do I prevent the duplicate instances of my theme track?
EDIT: ok the issue was that onSurfaceChanged
was creating a new instance of my game context, which of course created a new instance of the theme music.
Here is my Audio class:
public class Audio extends Token {
private MediaPlayer theme;
private SoundPool sound;
private int note = new int[17];
private int i = -1;
boolean toggle = false;
private int death, gliss, themeLoop;
//private float t;
boolean mute = false;
public Audio(Relic relic) {
theme = MediaPlayer.create(relic.context(), R.raw.theme_start);
theme.setLooping(true);
sound = new SoundPool.Builder().setMaxStreams(6).build();
note[0] = sound.load(relic.context(), R.raw.note1, 1);
note[1] = sound.load(relic.context(), R.raw.note2, 1);
note[2] = sound.load(relic.context(), R.raw.note3, 1);
note[3] = sound.load(relic.context(), R.raw.note4, 1);
note[4] = sound.load(relic.context(), R.raw.note5, 1);
note[5] = sound.load(relic.context(), R.raw.note6, 1);
note[6] = sound.load(relic.context(), R.raw.note7, 1);
note[7] = sound.load(relic.context(), R.raw.note8, 1);
note[8] = sound.load(relic.context(), R.raw.note9, 1);
note[9] = sound.load(relic.context(), R.raw.note10, 1);
note[10] = sound.load(relic.context(), R.raw.note11, 1);
note[11] = sound.load(relic.context(), R.raw.note12, 1);
note[12] = sound.load(relic.context(), R.raw.note13, 1);
note[13] = sound.load(relic.context(), R.raw.note14, 1);
note[14] = sound.load(relic.context(), R.raw.note15, 1);
note[15] = sound.load(relic.context(), R.raw.note16, 1);
note[16] = sound.load(relic.context(), R.raw.note17, 1);
death = sound.load(relic.context(), R.raw.death, 1);
gliss = sound.load(relic.context(), R.raw.gliss, 1);
playTheme();
}
public void playTheme() {
play(theme);
}
public void playNote() {
i = (i+1) % 17;
play(note[i]);
}
public void addPoints(int value) {
if (value > 1) {
play(gliss);
} else {
playNote();
}
}
public void gameover() {
play(death);
}
private void play(int i) {
if (!mute) {
sound.play(i, 1, 1, 1, 0, 1);
}
}
private void play(MediaPlayer player) {
if(!mute) {
player.start();
}
}
private void pause(MediaPlayer player) {
player.pause();
}
public void mute() {
mute = true;
pause(theme);
}
public void unmute() {
mute = false;
play(theme);
}
public void pause() {
// this is called from Activity.onPause
theme.stop();
theme.release();
theme = null;
}
}
android audio android-mediaplayer
add a comment |
I have a game with a single main music theme
that loops. On android if the user presses the home button the music will pause as desired, but if the user then reopens the app immediately the app will reset but two instances of the music theme will now play over each other and out of sync (as if the original instance was still playing in the background but silently). How do I prevent the duplicate instances of my theme track?
EDIT: ok the issue was that onSurfaceChanged
was creating a new instance of my game context, which of course created a new instance of the theme music.
Here is my Audio class:
public class Audio extends Token {
private MediaPlayer theme;
private SoundPool sound;
private int note = new int[17];
private int i = -1;
boolean toggle = false;
private int death, gliss, themeLoop;
//private float t;
boolean mute = false;
public Audio(Relic relic) {
theme = MediaPlayer.create(relic.context(), R.raw.theme_start);
theme.setLooping(true);
sound = new SoundPool.Builder().setMaxStreams(6).build();
note[0] = sound.load(relic.context(), R.raw.note1, 1);
note[1] = sound.load(relic.context(), R.raw.note2, 1);
note[2] = sound.load(relic.context(), R.raw.note3, 1);
note[3] = sound.load(relic.context(), R.raw.note4, 1);
note[4] = sound.load(relic.context(), R.raw.note5, 1);
note[5] = sound.load(relic.context(), R.raw.note6, 1);
note[6] = sound.load(relic.context(), R.raw.note7, 1);
note[7] = sound.load(relic.context(), R.raw.note8, 1);
note[8] = sound.load(relic.context(), R.raw.note9, 1);
note[9] = sound.load(relic.context(), R.raw.note10, 1);
note[10] = sound.load(relic.context(), R.raw.note11, 1);
note[11] = sound.load(relic.context(), R.raw.note12, 1);
note[12] = sound.load(relic.context(), R.raw.note13, 1);
note[13] = sound.load(relic.context(), R.raw.note14, 1);
note[14] = sound.load(relic.context(), R.raw.note15, 1);
note[15] = sound.load(relic.context(), R.raw.note16, 1);
note[16] = sound.load(relic.context(), R.raw.note17, 1);
death = sound.load(relic.context(), R.raw.death, 1);
gliss = sound.load(relic.context(), R.raw.gliss, 1);
playTheme();
}
public void playTheme() {
play(theme);
}
public void playNote() {
i = (i+1) % 17;
play(note[i]);
}
public void addPoints(int value) {
if (value > 1) {
play(gliss);
} else {
playNote();
}
}
public void gameover() {
play(death);
}
private void play(int i) {
if (!mute) {
sound.play(i, 1, 1, 1, 0, 1);
}
}
private void play(MediaPlayer player) {
if(!mute) {
player.start();
}
}
private void pause(MediaPlayer player) {
player.pause();
}
public void mute() {
mute = true;
pause(theme);
}
public void unmute() {
mute = false;
play(theme);
}
public void pause() {
// this is called from Activity.onPause
theme.stop();
theme.release();
theme = null;
}
}
android audio android-mediaplayer
Did my answer give you some insight into a possible answer gloo?
– Ishaan Javali
Nov 23 '18 at 4:29
add a comment |
I have a game with a single main music theme
that loops. On android if the user presses the home button the music will pause as desired, but if the user then reopens the app immediately the app will reset but two instances of the music theme will now play over each other and out of sync (as if the original instance was still playing in the background but silently). How do I prevent the duplicate instances of my theme track?
EDIT: ok the issue was that onSurfaceChanged
was creating a new instance of my game context, which of course created a new instance of the theme music.
Here is my Audio class:
public class Audio extends Token {
private MediaPlayer theme;
private SoundPool sound;
private int note = new int[17];
private int i = -1;
boolean toggle = false;
private int death, gliss, themeLoop;
//private float t;
boolean mute = false;
public Audio(Relic relic) {
theme = MediaPlayer.create(relic.context(), R.raw.theme_start);
theme.setLooping(true);
sound = new SoundPool.Builder().setMaxStreams(6).build();
note[0] = sound.load(relic.context(), R.raw.note1, 1);
note[1] = sound.load(relic.context(), R.raw.note2, 1);
note[2] = sound.load(relic.context(), R.raw.note3, 1);
note[3] = sound.load(relic.context(), R.raw.note4, 1);
note[4] = sound.load(relic.context(), R.raw.note5, 1);
note[5] = sound.load(relic.context(), R.raw.note6, 1);
note[6] = sound.load(relic.context(), R.raw.note7, 1);
note[7] = sound.load(relic.context(), R.raw.note8, 1);
note[8] = sound.load(relic.context(), R.raw.note9, 1);
note[9] = sound.load(relic.context(), R.raw.note10, 1);
note[10] = sound.load(relic.context(), R.raw.note11, 1);
note[11] = sound.load(relic.context(), R.raw.note12, 1);
note[12] = sound.load(relic.context(), R.raw.note13, 1);
note[13] = sound.load(relic.context(), R.raw.note14, 1);
note[14] = sound.load(relic.context(), R.raw.note15, 1);
note[15] = sound.load(relic.context(), R.raw.note16, 1);
note[16] = sound.load(relic.context(), R.raw.note17, 1);
death = sound.load(relic.context(), R.raw.death, 1);
gliss = sound.load(relic.context(), R.raw.gliss, 1);
playTheme();
}
public void playTheme() {
play(theme);
}
public void playNote() {
i = (i+1) % 17;
play(note[i]);
}
public void addPoints(int value) {
if (value > 1) {
play(gliss);
} else {
playNote();
}
}
public void gameover() {
play(death);
}
private void play(int i) {
if (!mute) {
sound.play(i, 1, 1, 1, 0, 1);
}
}
private void play(MediaPlayer player) {
if(!mute) {
player.start();
}
}
private void pause(MediaPlayer player) {
player.pause();
}
public void mute() {
mute = true;
pause(theme);
}
public void unmute() {
mute = false;
play(theme);
}
public void pause() {
// this is called from Activity.onPause
theme.stop();
theme.release();
theme = null;
}
}
android audio android-mediaplayer
I have a game with a single main music theme
that loops. On android if the user presses the home button the music will pause as desired, but if the user then reopens the app immediately the app will reset but two instances of the music theme will now play over each other and out of sync (as if the original instance was still playing in the background but silently). How do I prevent the duplicate instances of my theme track?
EDIT: ok the issue was that onSurfaceChanged
was creating a new instance of my game context, which of course created a new instance of the theme music.
Here is my Audio class:
public class Audio extends Token {
private MediaPlayer theme;
private SoundPool sound;
private int note = new int[17];
private int i = -1;
boolean toggle = false;
private int death, gliss, themeLoop;
//private float t;
boolean mute = false;
public Audio(Relic relic) {
theme = MediaPlayer.create(relic.context(), R.raw.theme_start);
theme.setLooping(true);
sound = new SoundPool.Builder().setMaxStreams(6).build();
note[0] = sound.load(relic.context(), R.raw.note1, 1);
note[1] = sound.load(relic.context(), R.raw.note2, 1);
note[2] = sound.load(relic.context(), R.raw.note3, 1);
note[3] = sound.load(relic.context(), R.raw.note4, 1);
note[4] = sound.load(relic.context(), R.raw.note5, 1);
note[5] = sound.load(relic.context(), R.raw.note6, 1);
note[6] = sound.load(relic.context(), R.raw.note7, 1);
note[7] = sound.load(relic.context(), R.raw.note8, 1);
note[8] = sound.load(relic.context(), R.raw.note9, 1);
note[9] = sound.load(relic.context(), R.raw.note10, 1);
note[10] = sound.load(relic.context(), R.raw.note11, 1);
note[11] = sound.load(relic.context(), R.raw.note12, 1);
note[12] = sound.load(relic.context(), R.raw.note13, 1);
note[13] = sound.load(relic.context(), R.raw.note14, 1);
note[14] = sound.load(relic.context(), R.raw.note15, 1);
note[15] = sound.load(relic.context(), R.raw.note16, 1);
note[16] = sound.load(relic.context(), R.raw.note17, 1);
death = sound.load(relic.context(), R.raw.death, 1);
gliss = sound.load(relic.context(), R.raw.gliss, 1);
playTheme();
}
public void playTheme() {
play(theme);
}
public void playNote() {
i = (i+1) % 17;
play(note[i]);
}
public void addPoints(int value) {
if (value > 1) {
play(gliss);
} else {
playNote();
}
}
public void gameover() {
play(death);
}
private void play(int i) {
if (!mute) {
sound.play(i, 1, 1, 1, 0, 1);
}
}
private void play(MediaPlayer player) {
if(!mute) {
player.start();
}
}
private void pause(MediaPlayer player) {
player.pause();
}
public void mute() {
mute = true;
pause(theme);
}
public void unmute() {
mute = false;
play(theme);
}
public void pause() {
// this is called from Activity.onPause
theme.stop();
theme.release();
theme = null;
}
}
android audio android-mediaplayer
android audio android-mediaplayer
edited Nov 23 '18 at 8:21
asked Nov 23 '18 at 4:06
gloo
814822
814822
Did my answer give you some insight into a possible answer gloo?
– Ishaan Javali
Nov 23 '18 at 4:29
add a comment |
Did my answer give you some insight into a possible answer gloo?
– Ishaan Javali
Nov 23 '18 at 4:29
Did my answer give you some insight into a possible answer gloo?
– Ishaan Javali
Nov 23 '18 at 4:29
Did my answer give you some insight into a possible answer gloo?
– Ishaan Javali
Nov 23 '18 at 4:29
add a comment |
1 Answer
1
active
oldest
votes
What you can do is try stopping the mp3 altogether rather than pausing it. So you can override the onPause
and onStart
methods like so:
@Override
public void onPause(){
super.onPause();
myMP3.stop();
}
@Override
public void onResume(){
super.onResume();
myMP3.start();
}
Now, when your app pauses, (or is temporarily closed), it will stop the mp3 completely. When your app resumes, it will start the mp3 again. I hope this helps!
I'm already callingtheme.stop()
fromonPause
. It certainly makes the track stop but the problem is when the app is reopened.
– gloo
Nov 23 '18 at 5:03
@gloo have you used onStart( ) also
– Kevin Kurien
Nov 23 '18 at 5:37
Originally I calledtheme.start()
fromonResume()
but the behavior I observed was identical, i.e a got two copies of the track playing out of sync.
– gloo
Nov 23 '18 at 7:24
Turns out I was just doing something dumb. Is it possible to close a question?
– gloo
Nov 23 '18 at 8:23
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440536%2fandroid-media-player-is-playing-over-itself%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
What you can do is try stopping the mp3 altogether rather than pausing it. So you can override the onPause
and onStart
methods like so:
@Override
public void onPause(){
super.onPause();
myMP3.stop();
}
@Override
public void onResume(){
super.onResume();
myMP3.start();
}
Now, when your app pauses, (or is temporarily closed), it will stop the mp3 completely. When your app resumes, it will start the mp3 again. I hope this helps!
I'm already callingtheme.stop()
fromonPause
. It certainly makes the track stop but the problem is when the app is reopened.
– gloo
Nov 23 '18 at 5:03
@gloo have you used onStart( ) also
– Kevin Kurien
Nov 23 '18 at 5:37
Originally I calledtheme.start()
fromonResume()
but the behavior I observed was identical, i.e a got two copies of the track playing out of sync.
– gloo
Nov 23 '18 at 7:24
Turns out I was just doing something dumb. Is it possible to close a question?
– gloo
Nov 23 '18 at 8:23
add a comment |
What you can do is try stopping the mp3 altogether rather than pausing it. So you can override the onPause
and onStart
methods like so:
@Override
public void onPause(){
super.onPause();
myMP3.stop();
}
@Override
public void onResume(){
super.onResume();
myMP3.start();
}
Now, when your app pauses, (or is temporarily closed), it will stop the mp3 completely. When your app resumes, it will start the mp3 again. I hope this helps!
I'm already callingtheme.stop()
fromonPause
. It certainly makes the track stop but the problem is when the app is reopened.
– gloo
Nov 23 '18 at 5:03
@gloo have you used onStart( ) also
– Kevin Kurien
Nov 23 '18 at 5:37
Originally I calledtheme.start()
fromonResume()
but the behavior I observed was identical, i.e a got two copies of the track playing out of sync.
– gloo
Nov 23 '18 at 7:24
Turns out I was just doing something dumb. Is it possible to close a question?
– gloo
Nov 23 '18 at 8:23
add a comment |
What you can do is try stopping the mp3 altogether rather than pausing it. So you can override the onPause
and onStart
methods like so:
@Override
public void onPause(){
super.onPause();
myMP3.stop();
}
@Override
public void onResume(){
super.onResume();
myMP3.start();
}
Now, when your app pauses, (or is temporarily closed), it will stop the mp3 completely. When your app resumes, it will start the mp3 again. I hope this helps!
What you can do is try stopping the mp3 altogether rather than pausing it. So you can override the onPause
and onStart
methods like so:
@Override
public void onPause(){
super.onPause();
myMP3.stop();
}
@Override
public void onResume(){
super.onResume();
myMP3.start();
}
Now, when your app pauses, (or is temporarily closed), it will stop the mp3 completely. When your app resumes, it will start the mp3 again. I hope this helps!
answered Nov 23 '18 at 4:15
Ishaan Javali
8871418
8871418
I'm already callingtheme.stop()
fromonPause
. It certainly makes the track stop but the problem is when the app is reopened.
– gloo
Nov 23 '18 at 5:03
@gloo have you used onStart( ) also
– Kevin Kurien
Nov 23 '18 at 5:37
Originally I calledtheme.start()
fromonResume()
but the behavior I observed was identical, i.e a got two copies of the track playing out of sync.
– gloo
Nov 23 '18 at 7:24
Turns out I was just doing something dumb. Is it possible to close a question?
– gloo
Nov 23 '18 at 8:23
add a comment |
I'm already callingtheme.stop()
fromonPause
. It certainly makes the track stop but the problem is when the app is reopened.
– gloo
Nov 23 '18 at 5:03
@gloo have you used onStart( ) also
– Kevin Kurien
Nov 23 '18 at 5:37
Originally I calledtheme.start()
fromonResume()
but the behavior I observed was identical, i.e a got two copies of the track playing out of sync.
– gloo
Nov 23 '18 at 7:24
Turns out I was just doing something dumb. Is it possible to close a question?
– gloo
Nov 23 '18 at 8:23
I'm already calling
theme.stop()
from onPause
. It certainly makes the track stop but the problem is when the app is reopened.– gloo
Nov 23 '18 at 5:03
I'm already calling
theme.stop()
from onPause
. It certainly makes the track stop but the problem is when the app is reopened.– gloo
Nov 23 '18 at 5:03
@gloo have you used onStart( ) also
– Kevin Kurien
Nov 23 '18 at 5:37
@gloo have you used onStart( ) also
– Kevin Kurien
Nov 23 '18 at 5:37
Originally I called
theme.start()
from onResume()
but the behavior I observed was identical, i.e a got two copies of the track playing out of sync.– gloo
Nov 23 '18 at 7:24
Originally I called
theme.start()
from onResume()
but the behavior I observed was identical, i.e a got two copies of the track playing out of sync.– gloo
Nov 23 '18 at 7:24
Turns out I was just doing something dumb. Is it possible to close a question?
– gloo
Nov 23 '18 at 8:23
Turns out I was just doing something dumb. Is it possible to close a question?
– gloo
Nov 23 '18 at 8:23
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440536%2fandroid-media-player-is-playing-over-itself%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Did my answer give you some insight into a possible answer gloo?
– Ishaan Javali
Nov 23 '18 at 4:29