简体   繁体   中英

Java - How to play an mp3 with looping points

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, eg: 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM