Results 1 to 3 of 3
  1. #1
    james12 is offline Member
    Join Date
    Jun 2014
    Posts
    37
    Rep Power
    0

    Default how to play audio in a stand alone application

    hi every one can any one let me know how to play audio in a stand alone application ..thanks in advance

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,776
    Rep Power
    5

    Default Re: how to play audio in a stand alone application

    Google: java sound
    Result: Trail: Sound (The Java™ Tutorials)
    Result: Java Sound Resources
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Join Date
    Jul 2014
    Location
    United States
    Posts
    7
    Rep Power
    0

    Default Re: how to play audio in a stand alone application

    import java.io.File; import java.io.IOException;
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.SourceDataLine;
    public class PlayAudio
    {
    private static final int EXTERNAL_BUFFER_SIZE = 128000;
    public static void main(String[] args)
    {
    String file1 = args[0];
    File file2 = new File(file1);
    AudioInputStream ais = null;
    try
    {
    ais = AudioSystem.getAudioInputStream(file2);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    System.exit(1);
    }
    AudioFormat af = ais.getFormat();
    SourceDataLine sdf = null;
    DataLine.Info dli = new DataLine.Info(SourceDataLine.class, af);
    try
    {
    sdf = (SourceDataLine) AudioSystem.getLine(dli);
    sdf.open(af);
    }
    catch (LineUnavailableException e)
    {
    e.printStackTrace();
    System.exit(1);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    System.exit(1);
    }
    sdf.start();

    int dat = 0;
    byte[] data1 = new byte[EXTERNAL_BUFFER_SIZE];
    while (dat != -1)
    {
    try
    {
    dat = ais.read(data1, 0, data1.length);
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    if (dat >= 0)
    {
    int nBytesWritten = sdf.write(data1, 0, dat);
    }
    }
    sdf.drain();
    sdf.close();
    System.exit(0);
    }
    }


    Regards

    [ ad links removed -- mod ]
    Last edited by JosAH; 07-02-2014 at 09:32 AM.

Similar Threads

  1. Can play audio, but can't stop it
    By LucienMontierre in forum New To Java
    Replies: 1
    Last Post: 06-16-2012, 07:07 PM
  2. Play Audio File
    By YapperNico in forum New To Java
    Replies: 0
    Last Post: 12-30-2010, 01:59 PM
  3. Play audio in the BlackBerry Browser
    By javierrebelde in forum Advanced Java
    Replies: 0
    Last Post: 04-30-2010, 07:51 PM
  4. Stand Alone Java Application
    By AndrewM16921 in forum New To Java
    Replies: 5
    Last Post: 01-25-2009, 03:57 PM
  5. How do i play an audio clip from a server?
    By JavaJenius in forum Networking
    Replies: 1
    Last Post: 03-08-2008, 06:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •