We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I am working on a tangible music player with Arduino and for this I am using the minim library. However, the player.pause() function does not work in my code. It only stops the song for a few milliseconds and then continues to play the song.
The code for the pause function is as the following:
boolean state = false;
if (keyPressed) {
if (key == 'p' && !state) {
state = true;
player[currentSong].pause();
}
else if (key == 'p' && state) {
state = false;
player[currentSong].play();
}
Is there something else that I have to add in order to let it pause until I press the P key again?
Answers
https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
https://Processing.org/reference/keyPressed_.html
https://Processing.org/reference/keyReleased_.html
Pressing the key is working properly, because I have printed the boolean states. The state changes from true to false and vice versa. However, the song does not pauses, it just keeps playing.
Based on ddf.minim.AudioPlayer's pause() example:
http://code.Compartmental.net/minim/audioplayer_method_pause.html
I assume that your code is inside the draw() method and if so that is the problem. Try this code and you will see that when you press a key it registers over several frames and each time flipping the state between true and false.
Move your code into the keyPressed method like this,. After all you only need to to the test when a key is pressed not every frame.
Also I would change the name of the variable
state
toisStopped
it makes it much clearer what is happening.Thanks for helping. I have tried both of the codes but it still does not work. In the code of GoToLoop the song just keeps rewinding to the beginning, but does not pause. In the code of quark, the state does change according to the code, so it does change from false to true. Thus, in none of these situations the song does not pause...
Whatever I do the song never stops, only if I use close(), but then the song never plays again.
I know that my code does what you want. Load the example called 'PlayAFile' that comes with minim and replace the code with this code and try it . It worked for me with PS3.0.2
@pet0103, my sketch example was intended as a template, not a complete program.
I haven't tested it till now. The only thing I had to do was adding an empty draw():
Changed SONGS to 3 and dragged 3 ".mp3" files onto the PDE.
Hit CTRL+K, entered subfolder "data/" and renamed those as:
"song01.mp3", "song02.mp3", "song03.mp3".
And voilà, "song01.mp3" started playing. And I could hit 'P' key to pause/resume.
Also spacebar or ENTER to jump to next ".mp3".
Please test it correctly before stating it isn't working! [-(
P.S.: In older Minim versions, it seemed to have some problems when closing the sketch w/ paused AudioPlayer and displayed some horrible red error messages.
That's why I've
@Override
exit() in order to forcibly close() all AudioPlayer instances.However it seems it's not needed anymore. You can delete exit() method if you wish. :-\"
Replace loop() w/ play() then. It's gonna stop the auto rewind(): >-)
@quark, the code does work in another file indeed. However not in my code, so I guess there is something else happening in the 500 lines of code that I have. The parts that are related to pause are as the following:
When comparing this part of the code with yours it should work, but it does not pause it only stops the song for a few milliseconds..
@GoToLoop, my code is very long, so it could be that while testing your code I missed something. And the way you describe it, it should indeed work, but in my code something goes wrong.
I cannot see anything in your last post that would cause the problem. Are you using minim in any other part of your code?
New "AudioPlayer Pause isPlaying II" w/ more features like rewind(), skip(), etc.: :-bd
Just place your ".mp3" files inside subfolder "data/". B-)
Okay guys, thank you for your help! I have been busy with the code a lot and have been able to achieve my goal. Thanks for your time!