Java - How to play an mp3 with looping points - java

I'm a bit stumped here. I need background music for the game I'm making, and for most songs, there will be a beginning section that will head into a loop once it's done playing (aka an intro). Although this seems kinda simple, I haven't been able to find anything that would do this. Could anyone help? Thanks!
EDIT: Screwed up a tag

First of all, I recommend you to use OGG instead of MP3, has better performance.
You have to separate the music into two tracks, one with the intro, and one with the looping part. In the code you have to initialize both tracks as two different Music objects. Set the looping in the object with the intro track to false and start playing it. Now on each cycle of the game you have to check if the intro track is playing, if not, start playing the loop (You have to make additional checkings if you can stop the music by other means, e.g: pausing the game).
Depending of the performance of the computer, the change will be amost unnoticeable or be a small gap.

edit:
my bad, I saw Java and automatically assumed it was Android.
Try looking in this answer

Related

Setting a delay on a one-character string looped user input that uses a bufferedreader in java (cmd program)

I'm writing a program that is a basic 2D command window game, that uses a bufferedreader with the .readline() method to get a WASD input to determine what direction the "Character" (asterisk) will move. However, if the player does nothing, I need the program to keep going and just keep the character in place. This all happens in a loop that goes roughly like
determine player movement needed
determine other needed changes to the board
change the board
rinse and repeat.
with the loop getting stopped at step 1 any time the user decides to not move.
I have read several other posts, and most of them seem to relate to client-server connections, which does not apply here. Another post I found seemed to have an answer, but as the context of the code was different and I did not understand the code due to my sparse experience programming, I can not make enough sense of it to use it.
Set timeout on user input
I am unfamiliar with what a "Future" and "Executor" do, although I have seen the term executor thrown around a lot in other code, so if someone could even just explain those two things in relation to the problem, that would be great! I apologize that I don't have any source code to post with this question, but I really have no clue where to start on this one other than to create a BufferedReader, which is fairly irrelevant.
Edit: to add a bit more information, I plan to take the output of the br.readline every half second or so and then update then update the board even if nothing has been entered. I think the first step to get this done is creating some kind of input stream, possibly other than a bufferedreader, that does not require the user to press enter every time they want to move or not move, so maybe that would be a better place to start.

I want to know how to use a music file chosen by the user, then detect when a certain sound frequency. ANDROID

So basically I want this to get the range of 60 - 150 Hz which is the general area for bass that lies in a song. Whenever it is in this range I want it do a function, and only it the range, my problem is I have tried to look up the functions needed to do so but with no luck, if one could show me here or a good article or explanation on this it will be great! I appreciate all the help and I will continue looking on my own. If more explanation is needed I can provide whatever information that is needed!
Austin.
UPDATE: I simplified an algorithm here:
User selects the song they want
Song loads onto player
Function scans song and finds the lower frequencies throughout the song and the output is a pattern.
Step 1) Do a fast fourier transform: http://en.wikipedia.org/wiki/Fast_Fourier_transform
An FFT takes a piece of sound and transforms it into the frequency/time domain - as in, which frequencies are playing and how intensely and during what parts of the sound. This is a useful mathematical operation that relies upon the property that all sound, no matter how complex, can be fundamentally constructed out of one or more sine waves of different frequencies and amplitudes.
If you've ever looked at a spectrogram, for example in foobar2000, it is implemented using FFT:
I suggest instead of trying to implement FFT yourself you find a library that is well tested and fast, such as http://en.wikipedia.org/wiki/FFTW which is written in C
Step 2) Now that you've FFTed the part of the sound that the user is listening to, you can simply inspect the frequency bins and do whatever you want! Although detecting bass kicks is not as simple as 'is this frequency bin a high value?' because then you may mistake bass lines for bass kicks. You may need to do further testing and research to get it to work juuust right.
EDIT: Delyan suggests http://www.clear.rice.edu/elec301/Projects01/beat_sync/beatalgo.html and it looks pretty good.

Detect Song with Java

Is there a library for detecting the currently playing song with Java? Not only with WinAMP or WMP, but generally, a technique to listen the audio output for example?
Thank you.
Edit: No, I just want to listen the audio output and decide whether there's a song playing right now, or not.
Not identifying. Just there's a song or not (playing right now).
You can take the Fourier transform of the audio input and compare it to known frequency distributions of various songs in your database. If they are close enough, you can say that they are the same.
This obviously has some flaws, but it's an idea you can work off of.
CLAMP is a good choice for WinAMP. People misunderstood the question so hard.
Two things you could try: 1. do STFFT and look for harmonic relationships. These appear in ordinary speech as well, of course, so you'll have to experiment with what kinds of harmonic relationships exist in music vs speech. 2. analyse the envelope for repeating rhythmic patterns.

Android Game Development - Moving the phone

First of all, sorry for the vague title. I have no other ideas of what could possibly be put there, but you'll see this for yourself as you read on.
I am a very new Java developer for android and a martian to game development, therefore my question is not so related to particulars as it is related to theory and possibilities.
I'm interested in how I can develop my game so if holding the phone horizontally and it is tilted one way or another, an object will move in that direction. I'm sure this is very common and easily done but what about if I wanted the top speed to increase the further it was turned, and the acceleration to increase the quicker it was turned?
I honestly have no idea of the complexity of this question, whether you will laugh and give me one line of code or whether it's something you just can't teach.
Either way, thanks for reading, I look forward to your responses.
Check this out http://mobilestrategist.blogspot.com/2010/01/android-accelerometer-and-orientation.html
This gives you what you need to do what you're asking for, but as the other answer suggests, you may be running before you crawl here. I have been writing Android for 2 years and have yet to take advantage of the accelerometer. Though if you're just looking to noodle around with the device's capabilities, this is as good a place to start as any, I suppose.
You could read about sensors & sensor events.
TYPE_ACCELEROMETER is perhaps what you are looking for...

Simple way to play a single frequency in java?

I just want to play a very simple, straight forward note by giving my computer a certain frequency as an integer, and from there I can figure out how to make it play the note longer or shorter. It does not necessarily have to come out of the actual sound card - if it's generated and output by the internal speaker that's okay.
I looked at the midi libraries that java has included, and they are way more than what I want to do. This just needs to be very basic.
Look into JFugue -- it's really easy to do some basic stuff, and the capabilities are there if you want to expand later.
Player player = new Player();
player.play("A C# E");
This example constructs and plays an equal tempered scale.
As far as i know there is no way to do this without some boilerplate code in Java. The most simple API is probably provided by the Applet class (can be used by non-Applets as well) in the form of the static newAudioClip(URL url)-method. However this gives you just the ability to play predefined audio clips and you have very little control over the audio. If you just need to play audio from a small, predefined set of clips it might suffice (you could have a set of wav-files containing your notes and play them this way, AudioClip's can be looped if desired).
Other than that, both midi and sampled audio API's are much more powerful, but the flexibility comes at a price: You need considerably more code to set them up.

Categories

Resources