Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 919389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:32:11+00:00 2026-05-15T18:32:11+00:00

I’m developing a Java game and want to bundle and play a number of

  • 0

I’m developing a Java game and want to bundle and play a number of sampled sound effects. I’m planning to use the standard javax.sound.sampled functionality to play them.

What format is best for these kind of samples? I’m looking for a blend of good compression, decent quality and convenience for programmatic use within Java.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:32:11+00:00Added an answer on May 15, 2026 at 6:32 pm

    Quality vs. Size

    Here are some interesting articles on the differences of MP3 vs. WAV.

    • http://www.angelfire.com/vt2/tommymc3/MP3vsWav.html

    • http://www.scinet.cc/articles/wma-vs-mp3/wma-mp3.html

    • http://ask.yahoo.com/20000602.html

    Why MP3 and WAV? These are the ones I see most often when creating program, so compatibility is never an issue.

    I have this really useful java file that does everything for you. You construct the object with the path to the sound file, then use methods to play, stop, loop it, etc.

    In the meantime, you can look at these for reference, albeit they’re not as clean: How can I play sound in Java? .


    Simple Programming

    Unfortunately, I don’t have the file on this computer, but here’s a simplified version. This doesn’t use javax.swing like you’d hoped, but to be honest, I’ve always preferred alternative methods.

    Because this is not the same as the file I have on my other computer, I cannot guarantee MP3 compatibility.

    import  sun.audio.*;
    import  java.io.*;
    
    public class Sound {
    
        private InputStream input;
        private AudioStream audio;
    
        public Sound (File fileName)
        {
            input = new FileInputStream();
            audio = new AudioStream(input);
        }
    
        public void play()
        {
            AudioPlayer.player.start(audio);
        }
    
        public void stop()
        {
            AudioPlayer.player.stop(audio);
        }
    
    }
    

    Instantiation:

    String projectPath = <project directory>; // getting this is another question    
    Sound helloSound = new Sound(new File(projectPath + "/Sounds"));
    

    Now you can call helloSound.play(); whenever you want the clip to be played.

    I prefer this method so you don’t have to constantly set everything up a stream each time you want to play a clip. This should work effectively with a few sound bites and blurbs, but be sure not to overuse it and take up memory. Use this for common sound effects.


    Simple Programming, Cont’d

    Found a good file to play MP3s in the same way as I showed above. I completely forgot to put mine in a separate thread, so this is a much better bet.

    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();
    
        }
    
    }
    

    Easy as pie, right? ;).

    Get the jar here. See the documentation here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 451k
  • Answers 451k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is absolutely no compelling reason to choose SharpDevelop over… May 15, 2026 at 8:47 pm
  • Editorial Team
    Editorial Team added an answer well, you could try hacks like this.... ​$(':radio:disabled').removeAttr('disabled').click(function(){ this.checked=false; })​;… May 15, 2026 at 8:47 pm
  • Editorial Team
    Editorial Team added an answer I found out if you add an page-break div with… May 15, 2026 at 8:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.