Set audio position microseconds/Player

Hay,
I've made this nifty little class for playing audio:
import java.io.*;
import javax.sound.sampled.*;
public class Player implements LineListener
     // Consts
     public static final float maxVol = 6.0206f;
     public static final float minVol = -80.0f;
     // Vars
     // The AudioInputStream to play
     private AudioInputStream toPlay = null;
     // If the playthread should pause
     private volatile boolean paused = false;
     // If the thingy is playing
     private volatile boolean playing = false;
     // The line
     private volatile SourceDataLine line = null;
     // The thread that will play the sound
     private Thread playThread = null;
     // Construtor
     // Body
     public void play(String toPlay)
          try {
               // If playing
               if (playing && paused && (!line.isRunning())) {
                    setPaused(false);
                    // The return so don't play again
                    return;
               play(new FileInputStream(toPlay));
          } catch (Exception ex) {ex.printStackTrace();}
     public void play(File toPlay) {
          try {
               // If playing
               if (playing && paused && (!line.isRunning())) {
                    setPaused(false);
                    // The return so don't play again
                    return;
               play(new FileInputStream(toPlay));
          } catch (Exception ex) {ex.printStackTrace();}
     public void play(InputStream toPlay) {
          try {
               // If playing
               if (playing && paused && (!line.isRunning())) {
                    setPaused(false);
                    // The return so don't play again
                    return;
               // Get AudioInputStream from given file.
               this.toPlay = AudioSystem.getAudioInputStream(toPlay);
          } catch (Exception ex) {ex.printStackTrace();}
          // Make the play thread
          playThread = new Thread(new PlayThread());
          // Start it
          playThread.start();
     public void stop()
          // Not playing
          playing = false;
          // If there is a line
          if (line != null)
               // Close the line
               line.close();
               line = null;
          // Distroy the play thread
          playThread = null;
     public void update(LineEvent e)
          // Stop
          if (e.getType()==LineEvent.Type.STOP)
               stop();
     // Getters
     public SourceDataLine getLine()
          if (line != null)
               return line;
          return null;
     public float getVolume()
          FloatControl con = (FloatControl) line
                    .getControl(FloatControl.Type.MASTER_GAIN);
          // Get value
          return con.getValue();
     public boolean getMute()
          BooleanControl con = (BooleanControl) line
                    .getControl(BooleanControl.Type.MUTE);
          // Get
          return con.getValue();
     // Setters
     public void setPaused(boolean inPaused)
          paused = inPaused;
          // If not null
          if (line != null)
               // If paused
               if (paused)
                    // Stop the line
                    line.stop();
               } // Resume
               else
                    // Start
                    line.start();
     // Setters
     public void setVolume(float vol)
          // TODO: Maybe test to see if within min an max
          FloatControl con = (FloatControl) line
                    .getControl(FloatControl.Type.MASTER_GAIN);
          // Set value
          con.setValue(vol);
     public void setMute(boolean mute)
          BooleanControl con = (BooleanControl) line
                    .getControl(BooleanControl.Type.MUTE);
          // Set
          con.setValue(mute);
     private class PlayThread implements Runnable
          public void run()
               try {
                    System.out.println("toPlay is null " + (toPlay == null));
                    // If not playing
                    if (!playing)
                         // Now playing
                         playing = true;
                         play();
                    System.out.println("Finished");
               } catch (Exception ex) {ex.printStackTrace();}
          private void play()
               try {
                    AudioInputStream din = null;
                    if (toPlay != null)
                         AudioFormat baseFormat = toPlay.getFormat();
                         AudioFormat decodedFormat = new AudioFormat(
                                   AudioFormat.Encoding.PCM_SIGNED, baseFormat
                                             .getSampleRate(), 16, baseFormat
                                             .getChannels(),
                                   baseFormat.getChannels() * 2, baseFormat
                                             .getSampleRate(), false);
                         // Get AudioInputStream that will be decoded by underlying
                         // VorbisSPI
                         din = AudioSystem
                                   .getAudioInputStream(decodedFormat, toPlay);
                         // Play now !
                         rawplay(decodedFormat, din);
                         toPlay.close();
               } catch (Exception e) {e.printStackTrace();}
          private void rawplay(AudioFormat targetFormat, AudioInputStream din)
                    throws IOException, LineUnavailableException
               byte[] data = new byte[4096];
               line = getLine(targetFormat);
               if (line != null)
                    // Start
                    line.start();
                    int nBytesRead = 0, nBytesWritten = 0;
                    while (nBytesRead != -1)
                         // While still paused
                         while (paused) {}
                         nBytesRead = din.read(data, 0, data.length);
                         if (nBytesRead != -1)
                              nBytesWritten = line.write(data, 0, nBytesRead);
                    // Stop
                    line.drain();
                    line.stop();
                    line.close();
                    din.close();
          private SourceDataLine getLine(AudioFormat audioFormat)
                    throws LineUnavailableException
               SourceDataLine res = null;
               DataLine.Info info = new DataLine.Info(SourceDataLine.class,
                         audioFormat);
               res = (SourceDataLine) AudioSystem.getLine(info);
               res.open(audioFormat);
               return res;
}I've got an app that uses JMF to play MP3's, but using JMF just to play MP3's is overkill, but I've found a SPI from javaZoom now, so that's okay.
Anyway, I want to set the position of the line in microseconds now, as well as get the duration.
Can anyone help with this?
By the way, if anyone can open .rar files (lots of complainants about that) then the, in much need of an update, app is here: http://acquiesce.awardspace.co.uk/Projects/Download/Music_Exploder.rar
I you do want to try it, you do need the JMF.
Why not pop into the forum as well:
http://acquiesce.informe.com/mucic-exploder-dc4.html
You know I need the comments.
Luke :D

okay, so u can do this in the Clip interface, but the Clip takes ages to open, at lest for mp3's.
I need it to open it right away
I understand a bit more now, the SourceDataLine plays each byte as it is writen which is why it plays it straight away, but the Clip loads it into memory, so therefore it knows how long, how many frames etc about the audio, but it means that it's not as fast as streaming.
I need the best of both worlds really, I've tried opening a Clip on another Thread as well as playing from a SourceDataLine, the idea was to have the Clip take over the playing once it has loaded, but the line doesn't play or read/write.
Is there someway to take advantage of both streaming an non-streaming in this way?
Or simply make the Clip.open_AudioImputStream) faster?
Thanks
Luke

Similar Messages

  • How to set the position in SAP Script

    Hi Professionals,
    Can anyone tell me that How can I set the position of an bitmap Image (after inserting) in SAP Script ?
    by default position is in Left, I need to align it in center or somewhere else.
    Anybody knows, kindly reply!
    Thanks
    Devinder

    Hi,
    But Can we assign a window under another window. Because I want to insert a bmp picture on particular position.
    example:- there is a digital signature and wanted to insert upon a name.
    So, there is a text (which is customer name) and digital signature (in bmp picture format) upon it.
    Thanks
    Devinder

  • Is it possible to set a default music player for the music on my external hard drive while still keeping my iTunes the default player on my Mac ?

    Is it possible to set a default music player for the music on my external while still keeping my iTunes the default player on my Mac ?
    Message was edited by: Pellseidon

    There is no setting in the iPhone to set another default music player. The default is Music. If you are using another application, you are at the mercy of how that application operates as far as what occurs when it stops or errors out. The car is looking for music to play.

  • In need of a flash/html5/quicktime audio/video media player for a website

    Hello everybody,
    I'm looking to get some kind of audio/video media player for a website. I would like it to be customizable, and also most likely read a play list from some kind of text file or xml file.
    Normally, I would look for some kind of flash media player, but recently the site might be read from Apple mobile devices also, which of course not flash which might be a problem, so I'm also looking into other options such as some kind of QuickTime media player web application or even html5 player.
    I do not personally know how to code in flash or HTML 5, so I would be willing to hire someone to help out.
    Is HTML 5 for long enough that it would play on most current browsers? I would imagine that HTML 5 might not work on older browsers–is this correct?
    I don't know of any QuickTime media players - any links to get me started?
    Does anyone know some solutions to this flash / apple issue?

    Looks like you are getting a very good lesson in why Flash is the best choice for a Web media player.... plentilful, easily customizable, and is a "right now" solution. Good luck on getting your viewers to download and install the codex need to play an Ogg video!
    To add the extra accessabilty for the iPhone etc, use swfobject to provide an alternative video file type... the .mov
    Here is an example of a Flash video that plays on the iPhone (well sort of..):
    http://www.cidigitalmedia.com/tutorials/qt/play_qt.html
    Using swfobject for Flash detection, if the Web browser has the Flash plugin, the Flash .flv video is displayed, if not, the QT, .mov version is displayed.
    View the source code for more exact details.
    If the Web browser does not have the Flash plugin, alternate content is displayed, which in this case happens to be a QT player, which displays a .mov video. In this case it's just a single video. Now maybe you have a QT player with playlist... but let me tell you.. they are a lot rarer than Flash media players with playlists, xml playlist, etc.... Strong case for Flash here..
    But your alt content could just as easily hold a QT media player (with playlist) if such an animal exists.
    Info on swfobject:
    http://code.google.com/p/swfobject/
    Best wishes,
    Adninjastrator

  • I need to set the position of components on a page that contains a MenuBar,

    I need to set the position of components on a page that contains a MenuBar, but MenuBar does not provide a getPreferred size method.
    How can I get the MenuBar's size in AWT?
    Thanks.

    A menubar is not a general component that is added to a frame like any another. It is the specific responsibility of the setMenuBar/setJMenuBar methods to deal with this.

  • Set file position within a for loop, error 1

    Hi, I'm reading from a .sxm file consisting of a large chunk of text followed by binary data for 512x512 images with 4 bytes per pixel
    I’ve managed to successfully identify the start of the binary and read the first image from the file and view it using  "flatten pixmap.vi", so I’m doing something right, however when i try to read the next chunk of image data in the .sxm file using a for loop, set file position keeps churning out ERR (1).
    im at a loss as to whats causing this
    Solved!
    Go to Solution.
    Attachments:
    binaryread3d trouble.vi ‏22 KB
    back panel screenshot.png ‏27 KB

     I closed the file in the loop, doh! ... another morning well spent

  • Setting Cursor position

    Pls help me for the following query:
    how to set cursor position at the beginning of text field(JTextField) after setting text inside it.

    RTFM
    http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/text/JTextComponent.html#setCaretPosition(int)

  • How do you set the position of JScrollBars?

    Can anyone tell me how to set the positions of the JScrollBars in a JScrollPane so that it will show the area I want?
    please,
    Tommy

    Sorry i should of add the area I am talking about is a JPanel.

  • Set file position error

    Hi,
    I am using LabVIEW 2009 SP1 on Win XP OS system. I am reading and writing data to a text file whose size does not exceed 50 MB. whenever i try to set file position to a value of say 128 bytes from the start of file, the function does not show an error. But when i try to read back the file position using get file position function, the value shows a very high number. I am not able to do a random access read of the file data. Please help.
    Regards,
    Yashasvi

    Hi Yashasvi,
    can you post the vi (or vi section) causing the problem?
    Thanks,
    Marco

  • Is it possible to set a default media player for the iPhone?

    I have a music subscription with another company, therefore all my music is played through their app. When I plug my phone into my car to play music, it always opens iTunes and alerts me there is no music there so I have to open the app and select the music. Which is fine, but when the app stops (for whatever reason), the iTunes (which I'm not sure why they don't call iMusic or something equally silly) tries to play any music I have in iTunes. Is there any way to stop this madness? Thanks!

    There is no setting in the iPhone to set another default music player. The default is Music. If you are using another application, you are at the mercy of how that application operates as far as what occurs when it stops or errors out. The car is looking for music to play.

  • Set audio record path

    setting audio record path?

    Let me guess - your tempo's set to 120 and 4/4 time, right?
    The limit is on the number of quarter notes Logic can handle (8550).
    Set your song length to max (2158), reduce your tempo, and/or adjust your time signature. Example, temp 60, 4/8 time will give you 2hrs 22minutes of recording.
    good luck

  • I can't play wmv files on my mac.  I've downloaded Flip4Mac and set as the default player by clicking on a wmv file in Finder and clicking Command   I. Nothing is working, Help please.

    I've downloaded Flip4Mac and set as the default player by clicking on a wmv file in Finder and clicking Command   I. Nothing is working, Help please.

    Limnos,
    Thanks for your reply. I tried that too and it still doesnt work.  When I click on the wmv file it opens up in a new browser page. There is sound but no video.
    Thank you

  • I recorded audio with Quicktime player and the sound files won't disappear from the desktop no matter how I try to put them in the trash...

    I recorded audio with Quicktime player and the sound files won't disappear from the desktop no matter how I try to put them in the trash...tried xing out the red x, putting them in the trash...right clicking to make them disappear...nothing works...

    Just got rid of them... I closed Quicktime and was asked to delete. So I deleted them. Problem solved.

  • Setting tab position and alignment in one shot

    Is there anyway to add a tab and set the position and alignment at once?
    myParagraph.tabStops.add().position = myTabPosition;
    The problem I am having is I don't know how to reference the tab to set
    its alignment.

    Hi Fred,
    It'll work on most add() operations--it's our attempt at adding the AppleScript "with properties" statement to JavaScript.:-)
    Thanks,
    Ole

  • Custom Script - set object position

    Hello,
    I want to build a trasformation that set a position of an'object (entity, table, ..) in diagram. Any idea?
    Thank you.
    (sorry for my english)
    Davide

    It sounds like you want to use a default or derived attribute value.
    You set these in rules, which are applied via profiles.
    http://docs.oracle.com/cd/E23943_01/doc.1111/e10978/c04_metadata.htm#DAFIIEEI
    let me know if this helps!
    -ryan

Maybe you are looking for