Results 1 to 4 of 4

Thread: Play files

  1. #1
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default Play files

    Hi, I am writing a Java application and i am playing some Windows Media Video (.wmv) and Wave Sounds (.wav). They play perfectly if I give the full path to the file,but I want to play them without doing this. The .wmv file is located in a subfolder of the current directory called 'Videos' and the .wav file is located in a subfolder of the current directory called 'Audio'. The code I am using is posted below.

    Help is much appreciated.


    //----------------------------------------------------------------------------


    public class wmvopen
    { //class

    public static void main(String args[])
    { //main



    try //try statement
    { //try
    Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "Q:\\Java123\\Videos\\Movie.wmv"); //open the file movie.wmv

    } //try

    catch (Exception e) //catch any exceptions here
    { //catch
    System.out.println("Error" + e ); //print the error
    } //catch

    } //main

    } //class

    //----------------------------------------------------------------------------

    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;
    import javax.sound.sampled.UnsupportedAudioFileException;

    public class IntroWave extends Thread
    {

    private String filename;
    private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb



    public IntroWave(String wavfile)
    {
    filename = wavfile;

    }



    public void run()
    { //run

    File soundFile = new File(filename);

    AudioInputStream audioInputStream = null;
    try
    {
    audioInputStream = AudioSystem.getAudioInputStream(soundFile);
    }

    catch (UnsupportedAudioFileException e1)
    {
    e1.printStackTrace();
    return;
    }

    catch (IOException e1)
    {
    e1.printStackTrace();
    return;
    }

    AudioFormat format = audioInputStream.getFormat();
    SourceDataLine auline = null;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

    try {
    auline = (SourceDataLine) AudioSystem.getLine(info);
    auline.open(format);
    } catch (LineUnavailableException e) {
    e.printStackTrace();
    return;
    } catch (Exception e) {
    e.printStackTrace();
    return;
    }



    auline.start();
    int nBytesRead = 0;
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

    try {
    while (nBytesRead != -1) {
    nBytesRead = audioInputStream.read(abData, 0, abData.length);
    if (nBytesRead >= 0)
    auline.write(abData, 0, nBytesRead);
    }
    } catch (IOException e) {
    e.printStackTrace();
    return;
    } finally {
    auline.drain();
    auline.close();
    }

    } //run




    public static void main(String[] args)
    {
    IntroWave theThread = new IntroWave("Q:\\Java123\\Audio\\JMF.wav");
    theThread.start();


    }
    }

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    12

    Default

    if you want to make open option,create menu entry,give it the ActionListener,and use JFileChooser

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default

    I want to play them without doing this.
    How are you going to get the path to the files?
    Have you tried relative paths in your code?
    What happens?

    new IntroWave("Audio\\JMF.wav");
    Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "Videos\\Movie.wmv");

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. How to play video files in Java
    By Hasan in forum Advanced Java
    Replies: 11
    Last Post: 04-26-2009, 11:57 PM
  2. Play music
    By carderne in forum New To Java
    Replies: 4
    Last Post: 08-05-2008, 01:09 PM
  3. How to play MP3 files?
    By Tsiliadoros in forum Advanced Java
    Replies: 0
    Last Post: 05-29-2008, 11:54 PM
  4. Delays by Play announcements
    By koni123 in forum Java Software
    Replies: 0
    Last Post: 01-08-2008, 09:51 AM
  5. play() and mp3
    By willemjav in forum Advanced Java
    Replies: 3
    Last Post: 12-23-2007, 02:50 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
  •