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 9081247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:16:12+00:00 2026-06-16T20:16:12+00:00

I’m pretty stumped here. I wrote a very basic program to test an MP3

  • 0

I’m pretty stumped here. I wrote a very basic program to test an MP3 abstraction we’ve been working on in a separate Eclipse project. The MP3 class has 3 dependencies which I moved over to the main project, and we copied the class as well. However, when trying to play a song on the new project, suddenly the code stopped working. The fix? Remove the SWT jar. I can’t figure out why it works, but removing SWT makes the MP3 play, and adding it back in breaks it again. The only thing contained in swt.jar is org.eclipse.swt code, as confirmed by decompiling with JD-GUI. The catch is that none of the audio code imports anything from org.eclipse.swt. Any idea how adding the SWT files in can completely break the MP3 code?

My test file:

MP3 mp3 = new MP3(new File("four-chord-song.mp3"));

mp3.play(60);

Scanner in = new Scanner(System.in);
long lastTime = System.currentTimeMillis();
while (true) {
    in.nextLine();
    System.out.println(System.currentTimeMillis() - lastTime);
    lastTime = System.currentTimeMillis();
}

And the MP3 file (kinda messy, the line that hangs with SWT is noted):

package com.partyrock.music;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.*;

public class MP3 extends Sound {

private File file;
private boolean isPaused;
private boolean isPlaying;
private AudioFileFormat fileFormat;
private Thread MP3Thread;
private SourceDataLine line;
private AudioInputStream din;
private MP3Player myMP3Player;
private double startTime = 0;

/**
 * Constructs an MP3 from a given file.
 * @param file The file
 */
public MP3(File file) {
    super();
    this.file = file;
    isPaused = false;

    if (!file.exists()) {
        System.err.println("MP3 constructed for non-existent file");
        return;
    }

    try {
        fileFormat = AudioSystem.getAudioFileFormat(file);
    } catch (UnsupportedAudioFileException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public class MP3Player implements Runnable {

    private double startTime;
    boolean paused;

    public MP3Player(double var) {
        this.startTime = var;
        paused = false;
    }

    public void run() {
        din = null;
        try {
            AudioInputStream in = AudioSystem.getAudioInputStream(file);
            AudioFormat baseFormat = in.getFormat();
            AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
            din = AudioSystem.getAudioInputStream(decodedFormat, in);

            DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat);
// ****** This is the line that breaks when SWT is included
            line = (SourceDataLine) javax.sound.sampled.AudioSystem.getLine(info);

            if (line != null) {
                line.open(decodedFormat);
                byte[] data = new byte[4096];

                // Start
                line.start();
                isPlaying = true;

                // Skip first startTime seconds
                int bytesToSkip = (int) (startTime
                        * (Integer) fileFormat.properties().get("mp3.bitrate.nominal.bps") / 8);
                din.skip(bytesToSkip);

                int nBytesRead;
                while ((data != null) && (din != null) && (nBytesRead = din.read(data, 0, data.length)) != -1
                        && isPlaying) {
                    // Skip first startTime seconds
                    if (isPaused == true) {
                        while (isPaused) {
                            Thread.sleep(100);
                        }
                    }
                    line.write(data, 0, nBytesRead);
                    // System.out.println(line.getMicrosecondPosition());
                }

                isPlaying = false;
                // Stop
                if (line != null && din != null) {
                    line.drain();
                    line.stop();
                    line.close();
                    din.close();
                }
            }

        } catch (Exception e) {
            System.out.println("Error!");
            e.printStackTrace();
        } finally {
            if (din != null) {
                try {
                    din.close();
                } catch (IOException e) {
                }
            }
        }
    }
}

@Override
public void play(double startTime) {
    this.startTime = startTime;
    myMP3Player = new MP3Player(startTime);
    MP3Thread = new Thread(myMP3Player);
    MP3Thread.start();

}

public void playthread(double startTime) {

}

@Override
public double getDuration() {
    // TODO Auto-generated method stub
    Long microseconds = (Long) fileFormat.properties().get("duration");
    double milliseconds = (int) (microseconds / 1000);
    double seconds = (milliseconds / 1000);
    return seconds;
}

@Override
public void pause() {
    // TODO Auto-generated method stub
    isPaused = true;
    // MP3Thread.suspend();
    myMP3Player.paused = true;
}

public void unpause() {
    // TODO Auto-generated method stub
    isPaused = false;
    myMP3Player.paused = false;
    // MP3Thread.resume();
}

@Override
public double getCurrentTime() {
    // TODO Auto-generated method stub
    System.out.println(line.getMicrosecondPosition());
    return startTime + (line.getMicrosecondPosition() / 1000);
}

/**
 * Returns the file associated with this MP3
 * @return The mp3 file
 */
public File getFile() {
    return file;
}

public void stop() {
    isPlaying = false;

    pause();
    MP3Thread = null;
    myMP3Player = null;

    isPaused = false;
    isPlaying = false;
    // fileFormat = null;
    MP3Thread = null;
    line = null;
    din = null;
    myMP3Player = null;
    startTime = 0;

    // Stop
    // line.drain();
    // line.stop();
    // line.close();
}

}
  • 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-06-16T20:16:13+00:00Added an answer on June 16, 2026 at 8:16 pm

    I’m quite confused as to how, but I re-downloaded the latest SWT, and it worked, although I had to add the -XstartOnFirstThread argument when launching or I got the ***WARNING: Display must be created on main thread due to Cocoa restrictions. error.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have been unable to fix a problem with Java Unicode and encoding. The
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Let's say I'm outputting a post title and in our database, it's Hello Y’all

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.