Java Clip Not Rewinding Properly? - java

I'm not even sure if the problem is that it's not rewinding properly. I have a Sounds enumerator/class which lets me load all sound effects and then play them at will, and in my game, I initialize them and then each time I want to play one, I play it in its own thread so it doesn't freeze the rest of the game.
The problem is that the first time I hold down or click the mouse to shoot (and thus, play the sound), it plays the sound, even repeatedly, as it's supposed to. But after I release the mouse button and try again, the sound no longer plays.
Does anyone know what the cause of this could be? Here are some code snippets showing the Sounds class and the piece of my code where I am playing the sound.
Sounds class: http://pastebin.com/Hqt5dPQ6
Playing the sound: http://pastebin.com/Lex9ZmzJ
I appreciate any help, as I all but dropped this project a couple of months ago because I wasn't making any progress due to this one stupid problem.

Related

In Viewpager2, how to play only one video at a time?

I am creating a project like youtube shorts.
where i have implemented Viewpager2(modal contains only videoview) with RecyclerviewAdapter, everything is ok, but problem is when we swiping both video playing at same time, and some stuck in intial point of scrolling.
So, 1. how to play one video at a time only when screen is fully visible, and second video which coming by scrolling remain pauses or black screen until screen is fully visible?
for smoothness i tried setoffpagelimit method, it plays multiple audio without any scrolling, i think with the solution of first question 2nf question also will solved.

Java - Use music playback control buttons

Usually on keyboards there is buttons to stop the current track, go to the next track, or to the previous one. When I'm playing sound on YouTube for example, and press the F10 key, the video stops.
I'm creating a Java/Kotlin music player. What currently happens when I have music playing, and then I click F10, is that the last YouTube video I have in an open tab starts playing, and the music player does not get effected at all.
What I want is: When I click F10 (or the other control keys), I want my music player to get affected by them, not YouTube.
How can I achieve that?
If I understand correctly, you have an application for playing back sound, but when control keys (e.g., F10 in particular) are pressed, your browser is receiving the key, not your application.
If that is the case, the question seems is about how to use the Swing KeyListener. The KeyEvent API shows that the keycode for the key to be VK_F10. Are you having problems with implementing this? Kotlin might have it's own way of doing key listening, though. I did find this SO question on Kotlin KeyListeners.
As far as starting or stopping a sound, that is usually accomplished by wrapping the audio playback class (Clip or SourceDataLine) in a class that can receive and handle commands to start, stop or continue. Are you having problems with implementing this?
The code that links these two functional areas should be loosely coupled. With that, pursing the two matters as two separate questions will be beneficial.
If your issue is a specific OS implementation question, it would be best to clarify that in the question with additional information.

Stop other sounds/music

I'm creating an application/game in java, which contains background music/sound. Everything works as expected. I want to mute/stop if some system sound/media sound/other sound playing.
Any suggestions...??
I would suggest not care about this problem - let the user decide what programs and sounds he plays in the background

Android, where to put my UI updates and my game loop

So I'm new to Android, and have been having some difficulties with understanding threading/Android UI updating and such. I have some code for a simple game engine I made for an AP computer science final project and I have been trying to make it into an Android app. Coming from the java world I'm not used to threading or worrying about how much time or where a calculation is taking place so I've been having some difficulties getting my game playable. After a splash screen and a main menu, I have it set up so an activity named "Play" starts. In this activity I have found that I can initialize my game engine object(which gets passed from class to class), create an object I created to make an AsyncTask("GuiThreader" in the code linked below) but as soon as I throw in some code in "Play" to do anything more than that (like initialize a button, or start the threader helper class) I get an "Activity has Stopped Unexpectedly" error meaning I'm doing something wrong. I've been looking at a lot of the Android example code but its making little sense to me. So I guess with all that background my bigger question is how can I get this code working? More specifically where should I have my loop that checks when the game is over, and how can I update my button colors outside of the "main" threader to keep it from crashing.
Here is my code:
"Play" Activity (with lines of code commented that I was testing with):
http://pastebin.com/K5kFsMvG
"GuiThreader" used to make the game calculations an AsyncTask: http://pastebin.com/306eUYfq
"GUIdriver" used to call a class that updates the button colors: http://pastebin.com/RANZBH38
"ButtonColorUpdate" saves a value for buttons and updates their colors: http://pastebin.com/qN2fw1RC
If you need anything else just comment and I'll put it up. Thanks in advance for any help!
Start by reading this. Then I'd recommend looking at the code from Replica Island. There's a ton you can learn from studying the code of this project. If you want to interact with GUI "stuff" from a separate thread, a Handler (see the API) can be useful for sending messages between the two.

PulpCore music playback - loop sound and animate volume

I have been experimenting with PulpCore, trying to create my own tower defence game (not-playable yet), and I am enjoying it very much I ran into a problem that I can't quite figure out. I extended PulpCore with the JOrbis thing to allow OGG files to be played. Works fine. However, pulpCore seems to have a problem with looping the sound WHILE animating the volume level. I tried this with wav file too, to make sure it isn't jOrbis that breaks it. The code is like this:
Sound bgMusic = Sound.load("music/music.ogg");
Playback musicPlayback;
...
musicVolume = new Fixed(0.75);
musicPlayback = bgMusic.loop(musicVolume);
//TODO figure out why it's NOT looping when volume is animated
// musicVolume.animate(0, musicVolume.get(), FADE_IN_TIME);
This code, for as long as the last line is commented out, plays the music.ogg again and again in an endless loop (which I can stop by calling stop on the Playback object returned from loop(). However, I would like the music to fade in smoothly, so following the advice of the PulpCore API docs, I added the last line which will create the fade-in but the music will only play once and then stop. I wonder why is that? Here is a bit of the documentation:
Playback
pulpcore.sound.Sound.loop(Fixed level)
Loops this sound clip with the
specified volume level (0.0 to 1.0).
The level may have a property
animation attached.
Parameters: level
Returns: a Playback object for this
unique sound playback (one Sound can
have many simultaneous Playback
objects) or null if the sound could
not be played.
So what could be the problem? I repeat, with the last line, the sound fades in but doesn't loop, without it it loops but starts with the specified 0.75 volume level.
Why can't I animate the volume of the looped music playback? What am I doing wrong? Anyone has any experience with pulpCore and has come across this problem? Anyone could please download PulpCore and try to loop music which fades-in (out)?
note: I need to keep a reference to the Playback object returned so I can kill music later.
If the animate method only sets the option, it can work unpredictably - try switching this line with the loop itself, so the animation applies first.
Can you animate the volume on an unlooped playback, and then, at the end of that playback, start the loop at the fixed level?
Finally I managed to get an explanation and a simple work-around for this issue from the pulp core author. So here it is:
It is a PulpCore bug. When the output
volume is zero, the sound player stops
looping the sound.
To work around it, animate from a
value that is not zero, like this:
musicVolume.animate(0.0001, 1, FADE_IN_TIME);
Link to this on pulpcore Google groups

Categories

Resources