Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
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  
  Looping MP3 File  (Read 7041 times)
0 Members and 1 Guest are viewing this topic.
Offline Vesayth

Senior Newbie


Projects: 1



« Posted 2008-10-09 05:41:49 »

Hello! First I want to admit that I have absolutely no idea what I'm doing when it comes to playing sounds and music. I have been using an ogg sound library to loop ogg files in my program, but the licenses for the music I am using require that they remain unmodified, meaning I must use MP3 format.

So that's my dilemma. I have implemented a barebones MP3 class (which uses JLayer) I found through Google, which can be found here: http://www.cs.princeton.edu/introcs/faq/mp3/mp3.html.

This works fine, however I cannot for the life of me seem to get it to loop. I don't think I know enough about the inner workings of sound files and sound interfaces to be able to figure it out on my own.

Any help would be appreciated!


Just as an aside. This is for a 2-d game that I am creating for a senior project at the college I go to, and when finished I will make it open-source. I have been working on it for a year now and I seem to be able to manage everything else in the game just fine, it's just the music that's stopping me. The game plays great without music, but who wants to play a game like that?
Offline Vesayth

Senior Newbie


Projects: 1



« Reply #1 - Posted 2008-10-10 00:00:06 »

It figures, after having been stuck on this problem for months I finally decide to post about it, then I come up with a solution.

Here is the code for anyone else who's curious, and if anyone knows a better way to do this then by all means please let me know!

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  
import java.io.BufferedInputStream;
import java.io.FileInputStream;

import javazoom.jl.player.Player;

public class MP3Player extends Thread
{
   private String filename;
   private Player player;
   private Thread mp3Thread;
   private boolean playing;
   private boolean locked;
   
   // constructor that takes the name of an MP3 file
  public MP3Player()
   {
      this.filename = "";
      mp3Thread = null;
      playing = true;
      locked = true;
   }
   
   public void run()
   {
      while (playing)
         play();
   }

   public void play()
   {
      if (!locked)
      {
         if (filename != null && filename != "")
         {
            try
            {
               FileInputStream fis     = new FileInputStream(filename);
               BufferedInputStream bis = new BufferedInputStream(fis);
               player = new Player(bis);
               
               mp3Thread = new Thread()
               {
                  public void run()
                  { try { player.play(); } catch (Exception e) { System.out.println(e); } }
               };
           
               mp3Thread.start();
               mp3Thread.join();
            } catch (Exception e) { System.out.println(e); }
         }
      }
   }

   public void changeMusic(String newGuy)
   {
      locked = true;
      filename = "";
      try
      {
         if (player != null)
            player.close();
         mp3Thread = null;
         
         filename = ".\\music/" + newGuy + ".mp3";
         
         locked = false;
      }
      catch (Exception e) {}
   }
   
   public void close()
   {
      locked = true;
      playing = false;
     
      try
      {
         if (player != null)
            player.close();
         
         if (mp3Thread != null)
            mp3Thread = null;
      } catch (Exception e) {}
   }
   
}
Offline nexeh

Junior Newbie





« Reply #2 - Posted 2009-11-28 20:13:43 »

Your source wasn't exactly complete. I found this online... It maybe be for school's site where they posted your project

http://www.cs.princeton.edu/introcs/faq/mp3/mp3.html

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  
/*************************************************************************
 *  Compilation:  javac -classpath .:jl1.0.jar MP3.java         (OS X)
 *                javac -classpath .;jl1.0.jar MP3.java         (Windows)
 *  Execution:    java -classpath .:jl1.0.jar MP3 filename.mp3  (OS X / Linux)
 *                java -classpath .;jl1.0.jar MP3 filename.mp3  (Windows)
 *  
 *  Plays an MP3 file using the JLayer MP3 library.
 *
 *  Reference:  http://www.javazoom.net/javalayer/sources.html
 *
 *
 *  To execute, get the file jl1.0.jar from the website above or from
 *
 *      http://www.cs.princeton.edu/introcs/24inout/jl1.0.jar
 *
 *  and put it in your working directory with this file MP3.java.
 *
 *************************************************************************/


import java.io.BufferedInputStream;
import java.io.FileInputStream;

import javazoom.jl.player.Player;


public class MP3 {
    private String filename;
    private Player player;

    // constructor that takes the name of an MP3 file
   public MP3(String filename) {
        this.filename = filename;
    }

    public void close() { if (player != null) player.close(); }

    // play the MP3 file to the sound card
   public void play() {
        try {
            FileInputStream fis     = new FileInputStream(filename);
            BufferedInputStream bis = new BufferedInputStream(fis);
            player = new Player(bis);
        }
        catch (Exception e) {
            System.out.println("Problem playing file " + filename);
            System.out.println(e);
        }

        // run in new thread to play in background
       new Thread() {
            public void run() {
                try { player.play(); }
                catch (Exception e) { System.out.println(e); }
            }
        }.start();




    }


    // test client
   public static void main(String[] args) {
        String filename = args[0];
        MP3 mp3 = new MP3(filename);
        mp3.play();

        // do whatever computation you like, while music plays
       int N = 4000;
        double sum = 0.0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                sum += Math.sin(i + j);
            }
        }
        System.out.println(sum);

        // when the computation is done, stop playing it
       mp3.close();

        // play from the beginning
       mp3 = new MP3(filename);
        mp3.play();

    }

}


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2009-11-28 21:24:35 »

Meself, I'd have just found some music without such a daft license restriction and used that instead in OGG format Smiley No need to make work for yourself!

Cas Smiley

Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #4 - Posted 2009-12-07 06:48:29 »

MP3 isn't suited for looping, since the decoded audio contains varying amounts of leading and trailing silence. You either have to specify the lead-in and lead-out points per file - or write some logic which does this for you.

Using Ogg is of course the smarter option. You get proper looping and a better size/quality ratio there.

弾幕 ☆ @mahonnaiseblog
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (50 views)
2014-04-15 12:08:23

BurntPizza (46 views)
2014-04-14 21:46:01

UprightPath (62 views)
2014-04-14 11:39:50

UprightPath (44 views)
2014-04-14 11:35:47

Porlus (60 views)
2014-04-14 09:48:38

tom_mai78101 (84 views)
2014-04-09 22:04:31

BurntPizza (142 views)
2014-04-08 17:06:04

tom_mai78101 (242 views)
2014-04-05 07:34:39

trollwarrior1 (201 views)
2014-04-04 06:06:45

CJLetsGame (208 views)
2014-03-31 20:16:10
List of Learning Resources
by SHC
2014-04-17 21:17:39

List of Learning Resources
by Longarmx
2014-04-07 21:14:44

Good Examples
by matheus23
2014-04-05 07:51:37

Good Examples
by Grunnt
2014-04-03 09:48:46

Good Examples
by Grunnt
2014-04-03 09:48:37

Good Examples
by matheus23
2014-04-01 12:40:51

Good Examples
by matheus23
2014-04-01 12:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 09:22:30
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!