When two audio's are played at the same time...the sound is canceling out. How do I fix this weird phenomenon?
I have some code where there is audio on button click and audio in ten second intervals (in a background service). I have the following code to stop the button audio when the ten second interval plays, and it works fine:
public static void myPop(Context context){
AudioManager manager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
if(!manager.isMusicActive()) { //Only if there isn't any other audio playing
MediaPlayer pop = MediaPlayer.create(context, R.raw.pop);
pop.start();
}
else{
Log.v(TAG, "Audio is already playing");
}
}
This works fine, and it stops one audio from playing (pop) to let the other audio play(The one from the background service). Now, I am getting the issue when they both play at the same time. For example, when I tap the button at the exact same time as when the audio from the background service is about to start. when they are both played at the same time, the audio just gets cut off
Is there any way to give a preference to the background service audio? Somehow say that: If two audio pieces start at the exact same time, I want to let the background service audio to play.
To think of this visually:
(https://upload.wikimedia.org/wikipedia/en/4/49/Preference_example.jpg)
I want to say that if I have the choice between apples and oranges, pick apples. AKA If I have two audioplayers playing at the same time, pick one (the apple).
Thanks for your help,
Ruchir
You may have a race condition. In these three lines
1 if(!manager.isMusicActive()) {
2 MediaPlayer pop = MediaPlayer.create(context, R.raw.pop);
3 pop.start();
Between line 1 and line 3, there is opportunity for another player to get the media player and start playing, especially if line 2 is slow.
I would suggest making one media player that both players access (i.e. create the "MediaPlayer pop" elsewhere and share it), or find another way to synchronize the different players. Perhaps issuing a stop right before a play, rather than checking for play state.
You can use code for play or pause audio when other player start or pause.
AudioManager.OnAudioFocusChangeListener afChangeListener =
new AudioManager.OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT) {
// Pause playback
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// Resume playback
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
am.abandonAudioFocus(afChangeListener);
// Stop playback
}
}
};
For more referernce
http://developer.android.com/training/managing-audio/audio-focus.html
Related
I'm trying to make a short arcade-like beep sound whenever the user clicks a movement button for a game. for this, I tried to use the code I always use to make audio:
in the MainActivity class I declared
MediaPlayer bpress;
in the OnCreate function
bpress = MediaPlayer.create(getApplicationContext(), R.raw.blop);
and in the OnClick
if (view == left) {
bpress.start();
}
and it does work, but the volume of the sound isn't consistent - it often plays on half the volume or becomes barely audible.
I tried switching to a different, longer audio file to see if the problem comes from the short length of the beep sound, but the problem remains.
Therefore, I'm almost certain that the problem is related to the fact that said buttons are pressed quickly, and many times. but I don't know why it happens or how to fix it
Here is the solutions to the problem.
Put this chunk of code inside your onClick listener
Solution 1:
mpBlob.stop();
try {
mpBlob.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mpBlob.start();
Solution 2:
if(mpBlob.isPlaying()) {
mpBlob.stop();
mpBlob.release();
mpBlob = MediaPlayer.create(getApplicationContext(), R.raw.blob);
}
mpBlob.start();
I am developing a Video App, and I need to know whether is any BGM playing?
I tried to use AudioManager.isMusicActive(), but it didn't work, because when I play my video, this method always returns true(I guess it detects my app).
In a word, I need to know BGM playing in other App, not Mine.
Finally, I've solved this problem by detecting other app's music every time before my music is going to start.
Because befeore your music is playing, if AudioManager.isMusicActive() returns true, then it means BGM is playing.
Something like this,
public void start() {
AudioManager am = (AudioManager) weak_context.get().getSystemService(Context.AUDIO_SERVICE);
boolean check = am.isMusicActive();
playMyMusic();
}
I was wondering if releasing my media player before I play a random sound is bad practice:
So I don't usually deal with media output too much, but I am making a simple app that plays a random sound every time a button is clicked (sounds [] is an array filled with raw media files)
public void onClick(View v){
if(mediaplayer != null){
mediaplayer.release();
}
mediaplayer = MediaPlayer.create(this, sounds[randomNum])
mediaplayer.start();
}
So my question is, would releasing my media player every time before creation be considered good/bad practice? Would there be any better way to do this, as releasing and re-initializing the MediaPlayer object seems like it would consume resources...
Thanks,
Ruchir
You typically use release() when you no longer want to use a MediaPlayer any more. Once you call that, it can never be used again. It effectively destroys the native components that back its functionality.
If you do release, you will have to prepare the media all over again the next time you want to play it. This can be a time consuming process. If you want a sound to play responsively to a button press, you probably don't want to have to prepare it each time.
I want to create a music loop pad, something like this: https://www.youtube.com/watch?v=fwBPYwiYp-Y
Right now, i am using MediaPlayer to play the sounds. I have a global variable that stores the current time (position) of the first audio that is playing, and when i press another button the corresponding audio file starts playing at the position given by the global variable.
My proble is: the sounds are playing out of sync.
can you give me some help? How can i sync the sounds? like this app https://www.youtube.com/watch?v=fwBPYwiYp-Y
Can you point me some info or tutorials about how to create a loop pad?
Thanks
Richardd
Programming something like this is harder than you think. Here are two things you must do to have the sounds play in sync:
Make sure all you sounds are at the same tempo, and are cropped properly - that means that the starting and ending of the sound clip must be exactly on beat - another solution would be to store the starting and ending time for each clip (very easy with OOP)
Use a timer to make sure the sound starts playing on beat. For example, if you wanted to make your music run at 120 bpm, you would need to have your timer fire every 500 ms.
Here is some code (only concept, won't compile):
Timer beatTimer;
boolean isDrumLoopEnabled;
boolean isDrumLoopCurrentlyPlaying;
Sound drumloop;
public void startMusic(){
//prepare timer
beatTimer=new Timer();
beatTimer.interval=500;//for 120 bpm
beatTimer.onTick+=onTickHandler;
isDrumLoopPlaying=false;
isDrumLoopCurrentlyPlaying=false;
//load sounds
Sound drumloop=new Sound("/storage/emulated/drumloop.ogg");
//start music
beatTiemr.start();
}
//this method will run on every beat:
public void onTickHandler(){
if(isDrumLoopEnabled){
//make sure drum loop isn't already playing
if(!isDrumLoopCurrentlyPlaying){
//begin looping drumloop in background
drumloop.startloop();
isDrumLoopCurrentlyPlaying=true;
}
}else{
if(isDrumLoopCurrentlyPlaying){
//stop playing drumloop
drumloop.stopPlaying();
}
}
}
//when the button is pressed...
public void drumloop_buttonpressed(){
//this will toggle whether to play the drum loop:
isDrumLoopEnabled=!isDrumLoopEnabled;
}
I was trying to sharp my OOD thinking. Here is my question.
Suppose you want to design a music mp3 player. And I got a class with a collection of playlists.
class Player {
Map<String, List<Song>> playlists; // <Name, PlayList>
public void play (Song song) {
// decode song
// play song
}
public void play (String playlistName) {
// play a playlist
for (Song song : playlists.get(playlistName)) {
play (song);
}
}
public void stop () {
// stop playing
}
public void pause () {
// pause, resume playing the last song when hit play again
}
}
Let's assume "Song" already contains all the metadata of a song. All the functionalities of methods have been described. I got stucked when I was trying to realize the "pause" method. What would you do to realize this?
Thanks!
Look into state pattern. Which you will store state of the player. When you hit pause and play you will know the state of the player.
I would have a Song member variable that tracks the currently playing song. If there is nothing playing, the value would be set to null. When you pause a Song, you simply call the Pause method on the Song class if the currently playing Song is not null. The Song class in turn can track where in the song (in terms of minutes) it is and figure out how a resume would work.
It seems to me like th play method blocks until the song has finished. It is a completely single-threaded design. When you design your application like that, it can't react to user input while a song is playing (unless you place an input handler into the playing loop).
In a real-world application, the play() method would start playing the song in a separate thread (many audio APIs will do that for you, so that you don't have to mingle with multi-threading) and then return, so that the application can stay responsible while a song is playing.
That way the pause and resume methods would then interact with the thread which plays the song.