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;
}
Related
I'm programming a game in Processing3 and I have a problem with the sound file. I'm playing song.play(); on draw() and every frame the program is playing again and again, anyone know a way to make it play the background music just once?
You haven't told us what library you're using- is it Minim?
If so, check out the Minim documentation for a list of classes and functions that you might find useful.
For example the AudioPlayer class has an isPlaying() function that you might use to check whether the song is playing before you play it.
You might also want to rearrange your program so that the song is only triggered once, either from the setup() function, or using the frameCount variable, or in response to user input.
I've found a way to make it work. I've created a variable: int x; and wrote
void draw()
{
x++;
If(x == 1)
song.play();
}
This way the song will play only when x is 1, and as x is growing every frame, it's not gonna be played again.
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.
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
My app plays a coin sound every time a button is pressed.
coin_sound.start();
You can easily press faster than the coin sound. When this happens I want the coin sound to start from the beginning ever time the button is pressed.
if(coin_sound.isPlaying()){
coin_sound.reset();
coin_sound = MediaPlayer.create(getContext(), R.raw.coin02);
}
coin_sound.start();
The problem with this is that loading a media file tiny as it may be is still a relatively slow process. When you start to click the button really fast the app lags hard.
Are there any solutions to my problem? The only idea I have is to do something with an array of coin_sounds, but this method seems like it will be messy and gross...
The other answer posted here is somewhat correct. You should not call create over and over.
The code in that answer has a problem, though. The reset method sends the MediaPlayer into the idle state, where it is illegal to call most other methods. If you were to go that route, you have to call methods in the following order:
coin_sound.reset();
coin_sound.setDataSource(...);
coin_sound.prepare();
coin_sound.start();
The difference between calling create and the previous sequence of method calls is simply the creation of a new instance. That, however, is not the quickest way to do what should be done.
You should simply call coin_sound.seekTo(0); when you want the current playing sound to restart. So do something like:
if (coin_sound.isPlaying()) coin_sound.seekTo(0);
else coin_sound.start();
That assumes you have left the MediaPlayer in the prepared state so start can be called. You can accomplish that by calling reset, setDataSource, and prepare in the onCompletion listener. Also, make sure to call release when the sound is no longer needed.
It is because you are initiating coin_sound in the button click event, try this
initiate this variable in your oncreate method
coin_sound = MediaPlayer.create(getContext(), R.raw.coin02);
then make this your code for your button
if(coin_sound.isPlaying()){
coin_sound.reset();
}
coin_sound.start();
the problem is you are recreating a new media player each time the button is clicked so the new media player doesnt think there is a sound
and do you need to start it again with coin_sound.start();? doesnt restart stop then start the sound for you?
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.