Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Mp3 Player with Jlayer  (Read 2537 times)
0 Members and 1 Guest are viewing this topic.
Offline pavul

Junior Devvie


Medals: 3
Exp: 4 years



« Posted 2017-08-07 23:43:53 »

if you want to play mp3 for bg music for your games this is what u need:

PLAY, STOP, RESUME, LOOP,
NOTE: you have to have installed jlayer1.0.1-1.jar


it works for music that is inside the jar ( for relative paths )


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
package com.ilusion2.audio;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

/**
 *this class is used to create a music player, with
 * play, stop, pause, loop, functionality, this class
 * must be used to put background music to a level
 * NOTE: the files to play must be in resources folder
 * @author pavulzavala
 */

public class MusicPlayer
{
   
    private InputStream is;
   
    private Player player;
    private boolean repeat;
    private boolean paused;
    private long pauseLocation;
    private long totalSongLength;
    private String musicFilePath;
   
    //@TODO consider add a counter to play a song
    // X number of times

    /**
     * this method is used to play a song, if u want to
     * repeat this song,  set Repeat to true before
     * call this method
     * NOTE: the files to play must be in resources folder
     * @param musicFilePath
     * @throws FileNotFoundException
     * @throws JavaLayerException
     * @throws IOException
     * @throws java.net.URISyntaxException
     */

    public void play( String musicFilePath ) throws FileNotFoundException, JavaLayerException, IOException, URISyntaxException
    {
       
   this.musicFilePath = musicFilePath;
   
   is = this.getClass().getResourceAsStream( musicFilePath );
       
   totalSongLength =  is.available();
   
    player = new Player( is );
   
        new Thread()
        {
            @Override
            public void run()
            {
                try
                {
                    player.play();
                   
                    if( player.isComplete() && repeat )
                    {
                    play( musicFilePath );
                    }
                   
                   
                }
                catch ( JavaLayerException | IOException ex)
                {
                    System.err.println("::: there was an error to play " + musicFilePath );
                } catch (URISyntaxException ex) {
                    Logger.getLogger(MusicPlayer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }////
        }.start();///
   
    }//
   
    /**
     * use this method to remuse current paused song
     * @throws FileNotFoundException
     * @throws JavaLayerException
     * @throws IOException
     * @throws java.net.URISyntaxException
     */

    public void resume( ) throws FileNotFoundException, JavaLayerException, IOException, URISyntaxException
    {
   
        paused = false;
       
    is = this.getClass().getResourceAsStream( musicFilePath );
   
    is.skip( totalSongLength - pauseLocation );
   
    player = new Player( is );
   
        new Thread()
        {
            @Override
            public void run()
            {
                try
                {
                    player.play();
                }
                catch (JavaLayerException ex)
                {
                    System.err.println("::: there was an error to play " + musicFilePath );
                }
            }////
        }.start();///
   
    }//
   
   
   
    /**
     * use this method to stop current song that is being
     * played
     */

    public void stop()
    {
    paused = false;
       
        if( null != player)
        {
        player.close();
       
        totalSongLength = 0;
        pauseLocation = 0;
        }///
       
    }//
   
    /**
     * use this method to pause current played song
     */

    public void pause()
    {
   
        paused = true;
        if( null != player)
        {
            try
            {
                pauseLocation = is.available();
                player.close();
            }
            catch (IOException ex)
            {
                System.out.println("::: error when song is paused");
            }
       
        }///
       
    }//

    /**
     *
     * @return true if the song i will start once is done,
     * false if not
     */

    public boolean isRepeat() {
        return repeat;
    }

    /**
     * set if the song will start once is done
     * @param repeat
     */

    public void setRepeat(boolean repeat) {
        this.repeat = repeat;
    }

    public boolean isPaused() {
        return paused;
    }

    public void setPaused(boolean paused) {
        this.paused = paused;
    }
   
}//class

its better not know why it works, that know why is not working
Offline SHC
« Reply #1 - Posted 2017-08-08 14:46:51 »

I heard there are licensing issues with using MP3s in Java applications, and hence I always use OGG. I don't know what they are though, could someone explain me about them?

Offline nsigma
« Reply #2 - Posted 2017-08-08 14:51:14 »

I heard there are licensing issues with using MP3s in Java applications, and hence I always use OGG. I don't know what they are though, could someone explain me about them?

Probably to do with the patents, but they've now expired anyway.  https://en.wikipedia.org/wiki/MP3#Licensing.2C_ownership_and_legislation

Praxis LIVE - hybrid visual IDE for (live) creative coding
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pavul

Junior Devvie


Medals: 3
Exp: 4 years



« Reply #3 - Posted 2017-08-08 15:30:51 »

i wasnt much aware of legal issues, but that is something to have in mind, I will check whether the patents are deprecated or not.
tnx!.

its better not know why it works, that know why is not working
Offline philfrei
« Reply #4 - Posted 2017-08-08 17:22:49 »

Is it okay to put a related wish-list item here?

I'd like to see OGG and MP3 decoders that give us the decompressed file back and let us handle the SourceDataLine for the final playback step. By doing just one less thing, it would allow us to get at the data for all sorts of effects, including varying the speed of playback or echo/chorus/distortion/filtering.

music and music apps: http://adonax.com
Offline SHC
« Reply #5 - Posted 2017-08-08 17:43:10 »

I gave a read on the licensing of MP3, and I got some quotes from the following links:

http://www.thehindu.com/sci-tech/technology/no-mp3-is-not-dead/article18474158.ece
http://www.audioblog.iis.fraunhofer.com/mp3-software-patents-licenses/

This announcement only means that the licensing of MP3 software ends, and they aren't required to pay to the Fraunhofer institute to use MP3 in their products. One important part is this:

Quote
This licensing program has now ended. Manufacturers which have already been mp3 licensees can still use the licensed software without any problems moving forward.

The licensing program has of course been ended, but still there is a small catch. From this quote:

Quote
Should other vendors be interested in using Fraunhofer’s mp3 software, they can now get in touch with Fraunhofer directly with their query.

They aren't saying that the MP3 software is now license free, you still have to get in touch with the company directly with your queries. It's only that from now on, they won't collect any fee, but there might be restrictions on the use of the software.

Quote
However, the end of the mp3 licensing program does not automatically mean that all mp3 technology is available license-free now. Apart from the core mp3 patents included in the licensing program, there might still be some implementation–specific patents (or patents for other functional enhancements) that have not expired.

MP3 has got some patents, and as I'm aware, they're in the European union and in the US. The patents in Europe were already expired in 2012, while since the US had patents for much longer time, they expired in the US in 2017. This means only that the software is patent free, and patents are much different from licenses and copyrights.

But having all this very sensitive stuff, and also that there are even better formats available, I think switching to either AAC or OGG Vorbis (there is also a term Theora I didn't know about) is a much better option.

Offline nsigma
« Reply #6 - Posted 2017-08-08 21:27:49 »

The licensing program has of course been ended, but still there is a small catch. From this quote:

Quote
Should other vendors be interested in using Fraunhofer’s mp3 software, they can now get in touch with Fraunhofer directly with their query.

They aren't saying that the MP3 software is now license free, you still have to get in touch with the company directly with your queries. It's only that from now on, they won't collect any fee, but there might be restrictions on the use of the software.

Except that this thread is about JLayer so Fraunhofer's software isn't relevant.

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline abcdef
« Reply #7 - Posted 2017-08-08 22:19:34 »

Correct, build your own MP3 decoder with out any license issues, use their software if you like but with their restrictions.

Personally I would go with ogg or opus just because they are far more superior formats
Offline gouessej
« Reply #8 - Posted 2017-08-09 09:15:48 »

Hi

Why not using Paul Lamb's 3D Sound System? Even Minecraft uses it. It supports both JavaSound and several Java bindings to the OpenAL API. I have used it with JOAL for years. It supports OGG very well, Midi, WAV, ...

Julien Gouesse | Personal blog | Website | Jogamp
Offline nsigma
« Reply #9 - Posted 2017-08-09 19:09:41 »

Why not using Paul Lamb's 3D Sound System? Even Minecraft uses it. It supports both JavaSound and several Java bindings to the OpenAL API. I have used it with JOAL for years. It supports OGG very well, Midi, WAV, ...

<yawn>  you're so predictable!  Tongue  Yes, if its features match what you need and you're using OpenAL (the JavaSound backend is crap) then it might be an option.

Praxis LIVE - hybrid visual IDE for (live) creative coding
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gouessej
« Reply #10 - Posted 2017-08-09 19:33:38 »

Why do you claim that the JavaSound backend is crap? Why don't you send your "constructive" remarks to the author of this library?

I had to install Mint and Mageia on several computers and the sound didn't work when I ran "Breaking the Tower". I assume that it's my fault again as usual, I can't blame JavaSound according to you. It's really ridiculous, it doesn't work on different hardware with two very different distributions and I'm sure that you'll tell me again that the problem doesn't come from JavaSound. As I'm honest, I'll try this fix and I'll make you know if it works:
https://keithp.com/blogs/Java-Sound-on-Linux/

Julien Gouesse | Personal blog | Website | Jogamp
Offline nsigma
« Reply #11 - Posted 2017-08-09 20:17:49 »

Why do you claim that the JavaSound backend is crap? Why don't you send your "constructive" remarks to the author of this library?

I have, multiple times, years ago.  Simply put, any sound library that doesn't do its own mixing to output to the soundcard is built wrongly!  (nb. the OpenAL software libraries do this for you on top of the soundcard driver, JavaSound just gives you direct access to the soundcard device)

I had to install Mint and Mageia on several computers and the sound didn't work when I ran "Breaking the Tower". I assume that it's my fault again as usual, I can't blame JavaSound according to you. It's really ridiculous, it doesn't work on different hardware with two very different distributions and I'm sure that you'll tell me again that the problem doesn't come from JavaSound. As I'm honest, I'll try this fix and I'll make you know if it works:
https://keithp.com/blogs/Java-Sound-on-Linux/

It's not your fault.  It's people writing JavaSound code that assumes because it works on one setup, it'll work on all.  JavaSound isn't actually written like that - it doesn't hide underlying platform differences, but gives you an API to query them.  This is actually a good thing (eg. consider File vs nio2!).

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline gouessej
« Reply #12 - Posted 2017-08-12 08:48:14 »

It means that I'll have to rewrite the source code using JavaSound in many cases.

Julien Gouesse | Personal blog | Website | Jogamp
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (150 views)
2017-09-30 02:57:34

theagentd (225 views)
2017-09-26 18:23:31

cybrmynd (303 views)
2017-08-02 12:28:51

cybrmynd (291 views)
2017-08-02 12:19:43

cybrmynd (299 views)
2017-08-02 12:18:09

Sralse (292 views)
2017-07-25 17:13:48

Archive (980 views)
2017-04-27 17:45:51

buddyBro (1105 views)
2017-04-05 03:38:00

CopyableCougar4 (1684 views)
2017-03-24 15:39:42

theagentd (1433 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!