How to read an audio file from the disk and play it?

Do you know who to read an audio file from the hard disk and play it. It's for an application and not an applet. I tried with the Applet.newAudioClip(URL url) thing, but I keep getting a MalformedURLException.
And is there a way to get the path of the file you are using? Currently I'm doing this:
File file = new file("randomname");
string = file.getAbsolutePath();
file.delete();
I'm sure there's a better way. And this is not for an applet, just a normal app.

Below is a class that should be of use to you.
package com.sound;
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
public class SoundPlayer implements Runnable {
     String filename;
     public SoundPlayer(String filename){
          this.filename = filename;
          Thread t = new Thread(this);
          t.start();
     }//SoundPlayer constructor
     public void run(){
          playSound();
     }//run method
     public boolean playSound(){
          try {
               File file = new File(filename);
               AudioInputStream stream = AudioSystem.getAudioInputStream(file);
               AudioFormat     format = stream.getFormat();
               DataLine.Info info = new DataLine.Info(Clip.class, format);
               Clip clip = (Clip)AudioSystem.getLine(info);
               clip.open(stream);
               clip.start();
               while (clip.isRunning()) {
                   Thread.sleep(100);
               clip.close();
          catch (Exception e) {
              System.err.println("Error in run in SoundPlayer");
               return false;
          return true;
     }//playSound() method
}//Class SoundPlayer

Similar Messages

  • How to read a flat file from the batch

    Hi,
    We have a requirement to read a flat file in the custom java batch in ETM2.2. We have the file at a specific location.
    can some one let me know what are the various options, we have to read a flat file.
    Thanks,
    Sharath Kumar G.

    Is it referring to some "Process X" batch?
    And why MPL/XAI with or without Configuration Tools should not be used to do this and a custom Java batch is required?

  • How to read a XML file from BLOB column and insert in a table - PL/SQL Only

    Hi,
    To make data load more simple to end user instead placing file on the server and use SQL-LOADER, I came up with new idea that using oracle ebusiness suite attachment functionality. that loads a XML file from local PC to a database column(table is fnd_attachments, default data type is BLOB over here).
    I tried with DBMS_LOB and didnt get around.
    Please can anyone tell me how to read the BLOB column using PL/SQL and store the data in a oracle table. Here's the sample XML file and table structure FYI.
    <?xml version="1.0" encoding="UTF-8"?>
    <dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Corporate_alloc.xsd" generated="2009-07-07T14:17:49">
    <Corporate_alloc>
    <PKG_CODE>BKCORP</PKG_CODE>
    <PKG_NAME>Corporate Edition - Books</PKG_NAME>
    <DET_CODE>B9780080543758</DET_CODE>
    <DET_NAME>Waves, Tides and Shallow-Water Processes</DET_NAME>
    <ALLOCATION_RATIO>0.000041</ALLOCATION_RATIO>
    </Corporate_alloc>
    <Corporate_alloc>
    <PKG_CODE>BKCORP</PKG_CODE>
    <PKG_NAME>Corporate Edition - Books</PKG_NAME>
    <DET_CODE>B9780080534343</DET_CODE>
    <DET_NAME>Hydrostatically Loaded Structures</DET_NAME>
    <ALLOCATION_RATIO>0.000127</ALLOCATION_RATIO>
    </Corporate_alloc>
    </dataroot>
    CREATE TABLE TEST_XML
    ( PKG_CODE VARCHAR2(50),
    PKG_NAME VARCHAR2(100),
    DET_CODE VARCHAR2(20),
    DET_NAME VARCHAR2(500),
    ALLOCATION_RATIO NUMBER )
    Thanks
    EBV

    In regards to #3, use the COLUMNS functionality of XMLTable instead of using Extract. Two simple examples are
    Re: XML Data - Caliculate fields
    Re: Extractvalue function not recognised

  • Help reading a .txt file from the hard drive....

    Hi guys,
    Ok... I need to know how to read a .txt file from the hard drive... I have an example on how to read from a disk, but I don't have a disk drive. I could be confusing terminology, but I don't think I am. Here is the code I have for reading from a disk.. please let me know how I should change it to read from a hard drive.
    Also, when the error pops up, it says that it can't read another .txt file that is not specified in the code... any ideas how that is occuring? Please advise.
    import java.io.*;
    public class TEST {
    public static void main(String args[]) throws Exception{
    FileReader fr = new FileReader("customer.txt");
    BufferedReader bfr = new BufferedReader (fr);
    String s;
    while ((s=bfr.readLine())!= null){
    System.out.println (s);
    fr.close();
    }

    Ok... I need to know how to read a .txt file from the
    hard drive... I have an example on how to read from a
    disk, but I don't have a disk drive. I could be
    confusing terminology, but I don't think I am. Here
    is the code I have for reading from a disk.. please
    let me know how I should change it to read from a
    hard drive.The code will be the same, regardless of where the file is located. It's just the filename that will be different.
    Also, when the error pops up, it says that it can't
    read another .txt file that is not specified in the
    code... any ideas how that is occuring? Please
    advise.Probably becuase the class file (compiled version of the source code) is a different version to the source code, i.e. the source code hasn't been recompiled since the source code was changed.
    How does this relate to EJBs?

  • How to download a text file from the server

    hi everyone,
    can anyone tell me how to download and read a text file from the server and saved in into resource folder.
    with regards
    pallavi

    its really easy
    To read from server, use something like:
    HttpConnection connector = null;
    InputStream inp_stream = null;
    OutputStream out_stream = null;
    void CloseConnection()
         if(inp_stream!=null)inp_stream.close();
         inp_stream=null;
         if(out_stream!=null)out_stream.close();
         out_stream=null;
         connector.close();
         connector = null;
    public void getResponse(String URL,String params)
      try
         if(connector==null)connector = (HttpConnection)Connector.open(URL);//URL of your text file / php script
         connector.setRequestMethod(HttpConnection.POST);
         connector.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.1");
         connector.setRequestProperty("content-type", "application/x-www-form-urlencoded");
         //connector.setRequestProperty("charset","windows-1251");
         //*** If you need to send ("arg1=value&arg2=value") arguments to script use this:
         out_stream = connector.openOutputStream();
         byte postmsg[] = params.getBytes();
         out_stream.write(postmsg);
         int rc = connector.getResponseCode();//in any case here connection will be opened & closed
         if (rc != HttpConnection.HTTP_OK)
              CloseConnection();
              throw new IOException("HTTP response code: " + rc);
         inp_stream = connector.openInputStream();
         int pack_len = inp_stream.available();
         byte answ[]=new byte[pack_len];
         inp_stream.read(answ);
         CloseConnection();
         ProcAnswer(answ);//process received data
      catch(Exception ex)
         System.err.println("ERROR IN getResponse(): "+ex);
    } And you can read from resource file like
    public void loadFile(String filename)
        DataInputStream dis = new DataInputStream(getClass().getResourceAsStream("/"+filename));
        String str="";
        try
             while (true)
                ch = dis.read();//read character
                if(ch=='\r')continue;//if file made in windows
                if(ch=='\n' || ch==-1)//end of line or end of file
                    if(str.length()==0)continue;//if empty line
                    //do some thing with "str"
                    if(ch==-1)break;//it was last line
                    str="";//next line
                    continue;
                 str+=(char)ch;
           dis.close();
       catch (Exception e)
           System.err.println("ERROR in loadFile() " + e);
    }Welcome! =)
    Edited by: MorskoyZmey on Aug 14, 2008 3:40 AM

  • How do I delete a file from the Adobe Reader for iOS?

    How do I delete a file from the Adobe Reader for iOS?

    Read this:
    http://forums.adobe.com/thread/1012973?tstart=0

  • I just purchased a classic and some how deleated the audio book file from the library and the device section of iTunes, how to restore

    I accidently deleted the file folder audio books from the library and from teh device section in iTunes how to replace

    Edit > Preferences > General tab, tick Books. Audiobooks are synced from the Books tab of the device.
    tt2

  • How do a scan a file from my scanner and make it a PDF. . . . I do not see this choice in the Adobe program?

    How do a scan a file from my scanner and make it a PDF. . . . I do not see this choice in the Adobe program?
    In the past I would simply place a copy of the document on my scanner, then selected create PDF from scanner.  However, there is now no create PDF from scanner alternative, option or choice.  The only option available is selected file to convert to PDF.  what am I to do?

    That would have been a function of your scanning software. Adobe Reader is only used to view the pdf that your scanning software created.

  • How do I move music files from the startup disc to iTunes in the other internal hard disk os 10.5.8?

    How do I move music files from the startup disc to iTunes in the other internal hard disk os 10.5.8?
    The music I want to move was in iTunes but is now just files. I had to reinstall a new iTunes and lost all my old music.
    Thank you!

    That article is about using your iPod as an external hard drive, to move files from one computer to another computer.  It is not related to your question.
    You can't use iTunes to move song files from iPod to computer, except for songs you purchased from the iTunes Store (and you can also re-download purchases).  However, there are third-party methods and utilities that can move song files from iPod to computer.  If you do a Google search on something like "ipod transfer," you should get some links.
    Once the song files are on your current computer, add them to that computer's iTunes library by dragging the files (or folder containing the files) to the iTunes window.  After confirming everything is there (and fixing the data issue you mentioned) you can sync the iPod.  You may want to do a Restore on the iPod, if it might have a data corruption issue, which will erase the iPod's hard drive, re-install the latest software, and set it to defautl settings.

  • How do I move aiff files from the viewer to the timeline-it is not working when I try to drag and click or clicking on overwrite or insert in Final Cut Pro 7. Thanks

    How do I move aiff files from the viewer to the timeline-it is not working when I click and drag or when I try to move the files to the overwrite or insert buttons-Thanks.

    hi
    You cannot move audio from the viewer
    Mark your in and out points then in the browser grab hold of the audio tracks icon and drag that to the time line
    PJ

  • How do I transfer audio files from my laptop to my iPod classic

    How do I transfer audio files from my laptop to my iPod

    Thank you for using the Apple Support Communities
    You can synchronize audio files from iTunes to your iPod.
    https://www.apple.com/support/ipodclassic/how-to/
    -Zeph

  • How to Read a CAB File from JAVA?

    Hi,
    Anyone knows how to read a CAB File from java. I need to read a property file of txt file that is packaged in CAB file & then based on that, I have to do processing. Is there anyway to do it.
    I had tried using java.util.zip.ZipFile Class, but it does work for JAR, but not for CAB.
    siva.

    Perhaps there's something in the Cabinet SDK that will help:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncabsdk/html/cabdl.asp

  • How do I open RAW files from the Nikon D4s in photoshop?

    How do I open RAW files from the Nikon D4s in Photoshop? I have the updated newest version.

    The Nikon D4S is a brand new camera which has just been added by Adobe to Camera Raw 8.4.
    http://labs.adobe.com/technologies/cameraraw8-4/?tabID=details#tabTop
    8.4 has not yet been finally released but you can download and install a pre-release copy for CS6 and CC (it is not compatible with CS5 and earlier)
    CS6:http://labs.adobe.com/technologies/cameraraw8-4/
    CC: http://labs.adobe.com/technologies/cameraraw8-4-cc/

  • How do I seperate the audio track from the video and save it to a CD in Premiere Elements 13?

    How do I seperate the audio track from the video and save it to a CD in Premiere Elements 13?

    Binbashi
    You can separate the audio track from video and save it to a DVD disc within Premiere Elements but not a CD disc.
    To do save to the audio to DVD disc, you do not have to separate linked audio video, just use the export Publish+Share/Computer/Audio and set Presets to the wanted format (AAC, AIFF, MP3, or Windows Waveform (WAV).
    You will not see Audio in the choices since it is at the bottom of the list of choices under Publish+Share/Computer/. You will need to use the thin scroll bar to the right of the choices to scroll to it.
    If you need information on selective export of Timeline content when dealing with more than just this one file on the Timeline, please let us know. If you are trying to produce a CD for listening in car or CD player elsewhere, please let us know.
    Looking forward to learning of your results.
    Thank you.
    ATR

  • Hello Anybody, I have a question. Can any of you please suggest me how to make an xml file from the database table with all the rows? Note:- I am having the XSD Schema file and the resulted XML file should be in that XSD format only.

    Hello Anybody, I have a question. Can any of you please suggest me how to make an xml file from the database table with all the records?
    Note:- I am having the XSD Schema file and the resulted XML file should be in that XSD format only.

    The Oracle documentation has a good overview of the options available
    Generating XML Data from the Database
    Without knowing your version, I just picked 11.2, so you made need to look for that chapter in the documentation for your version to find applicable information.
    You can also find some information in XML DB FAQ

Maybe you are looking for

  • Bridge CS2 won't open

    Hi I rebuilt my win xp computer did a new install of the OS. I then re- installed Photoshop CS2. For a week Photoshop and bridge worked fine then on Friday I tried to open Bridge and nothing happened. Task Manager shows bridge in the Processes and us

  • Re: JDBC 10.2.0.3 available for download

    kmensah Posted this on Mar 14, 2007 3:25 PM: Some of you have requested when 10.2.0.3 will be available for download; here it is: http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/index.html But the link 10.2.0.3 link gives a 404 NOT FOUN

  • ME 808: System error: error during update Table EKAB

    Hi, Couple of years ago, during carve out, one of the contracts was deleted. A PO created against that contract carries some open quantities. Now our purchasing wants to close this PO, either by changing the quantity to delivered quantity (delivered

  • Live Cycle Interview...

    Hi All, I have an interview for Adobe Live cycle developer position.... though i have some hands on experience in developing forms, i was wondering if there is any material which has some common interview Question. I really appreciate if someone can

  • Switching social security number on att account

    hi , my girlfriend has had her att account under her dads ssn since she was young and hasnt changed it because she is afraid to loose her phone #. now that she is trying to upgrade to an iphone, she cant pick it up unless her dad goes and signs for h