FileDocCategorySizeDatePackage
AudioFileIO.javaAPI DocJaudiotagger 2.0.412854Wed Jun 08 12:05:40 BST 2011org.jaudiotagger.audio

AudioFileIO

public class AudioFileIO extends Object

The main entry point for the Tag Reading/Writing operations, this class will select the appropriate reader/writer for the given file.

It selects the appropriate reader/writer based on the file extension (case ignored).

Here is an simple example of use:

AudioFile audioFile = AudioFileIO.read(new File("audiofile.mp3")); //Reads the given file.
int bitrate = audioFile.getBitrate(); //Retreives the bitrate of the file.
String artist = audioFile.getTag().getFirst(TagFieldKey.ARTIST); //Retreive the artist name.
audioFile.getTag().setGenre("Progressive Rock"); //Sets the genre to Prog. Rock, note the file on disk is still unmodified.
AudioFileIO.write(audioFile); //Write the modifications in the file on disk.

You can also use the commit() method defined for AudioFiles to achieve the same goal as AudioFileIO.write(File), like this:

AudioFile audioFile = AudioFileIO.read(new File("audiofile.mp3"));
audioFile.getTag().setGenre("Progressive Rock");
audioFile.commit(); //Write the modifications in the file on disk.

author
Raphael Slinckx
version
$Id: AudioFileIO.java 976 2011-06-08 10:05:34Z paultaylor $
see
AudioFile
see
org.jaudiotagger.tag.Tag
since
v0.01

Fields Summary
public static Logger
logger
private static AudioFileIO
defaultInstance
This field contains the default instance for static use.
private final ModificationHandler
modificationHandler
This member is used to broadcast modification events to registered
private Map
readers
private Map
writers
Constructors Summary
public AudioFileIO()
Creates an instance.



            
     
    
        this.modificationHandler = new ModificationHandler();
        prepareReadersAndWriters();
    
Methods Summary
public voidaddAudioFileModificationListener(AudioFileModificationListener listener)
Adds an listener for all file formats.

param
listener listener

        this.modificationHandler.addAudioFileModificationListener(listener);
    
public voidcheckFileExists(java.io.File file)
Check does file exist

param
file
throws
java.io.FileNotFoundException

        logger.config("Reading file:" + "path" + file.getPath() + ":abs:" + file.getAbsolutePath());
        if (!file.exists())
        {
            logger.severe("Unable to find:" + file.getPath());
            throw new FileNotFoundException(ErrorMessage.UNABLE_TO_FIND_FILE.getMsg(file.getPath()));
        }
    
public static voiddelete(AudioFile f)

Delete the tag, if any, contained in the given file.

param
f The file where the tag will be deleted
throws
CannotWriteException If the file could not be written/accessed, the extension wasn't recognized, or other IO error occurred.
throws
org.jaudiotagger.audio.exceptions.CannotReadException


                                                                                
           
    
        getDefaultAudioFileIO().deleteTag(f);
    
public voiddeleteTag(AudioFile f)

Delete the tag, if any, contained in the given file.

param
f The file where the tag will be deleted
throws
CannotWriteException If the file could not be written/accessed, the extension wasn't recognized, or other IO error occurred.
throws
org.jaudiotagger.audio.exceptions.CannotReadException

        String ext = Utils.getExtension(f.getFile());

        Object afw = writers.get(ext);
        if (afw == null)
        {
            throw new CannotWriteException(ErrorMessage.NO_DELETER_FOR_THIS_FORMAT.getMsg(ext));
        }

        ((AudioFileWriter) afw).delete(f);
    
public static org.jaudiotagger.audio.AudioFileIOgetDefaultAudioFileIO()
This method returns the default instance for static use.

return
The default instance.

        if (defaultInstance == null)
        {
            defaultInstance = new AudioFileIO();
        }
        return defaultInstance;
    
private voidprepareReadersAndWriters()
Creates the readers and writers.


        // Tag Readers
        readers.put(SupportedFileFormat.OGG.getFilesuffix(), new OggFileReader());
        readers.put(SupportedFileFormat.FLAC.getFilesuffix(),new FlacFileReader());
        readers.put(SupportedFileFormat.MP3.getFilesuffix(), new MP3FileReader());
        readers.put(SupportedFileFormat.MP4.getFilesuffix(), new Mp4FileReader());
        readers.put(SupportedFileFormat.M4A.getFilesuffix(), new Mp4FileReader());
        readers.put(SupportedFileFormat.M4P.getFilesuffix(), new Mp4FileReader());
        readers.put(SupportedFileFormat.M4B.getFilesuffix(), new Mp4FileReader());
        readers.put(SupportedFileFormat.WAV.getFilesuffix(), new WavFileReader());
        readers.put(SupportedFileFormat.WMA.getFilesuffix(), new AsfFileReader());
        final RealFileReader realReader = new RealFileReader();
        readers.put(SupportedFileFormat.RA.getFilesuffix(), realReader);
        readers.put(SupportedFileFormat.RM.getFilesuffix(), realReader);

        // Tag Writers
        writers.put(SupportedFileFormat.OGG.getFilesuffix(), new OggFileWriter());
        writers.put(SupportedFileFormat.FLAC.getFilesuffix(), new FlacFileWriter());
        writers.put(SupportedFileFormat.MP3.getFilesuffix(), new MP3FileWriter());
        writers.put(SupportedFileFormat.MP4.getFilesuffix(), new Mp4FileWriter());
        writers.put(SupportedFileFormat.M4A.getFilesuffix(), new Mp4FileWriter());
        writers.put(SupportedFileFormat.M4P.getFilesuffix(), new Mp4FileWriter());
        writers.put(SupportedFileFormat.M4B.getFilesuffix(), new Mp4FileWriter());                
        writers.put(SupportedFileFormat.WAV.getFilesuffix(), new WavFileWriter());
        writers.put(SupportedFileFormat.WMA.getFilesuffix(), new AsfFileWriter());

        // Register modificationHandler
        Iterator<AudioFileWriter> it = writers.values().iterator();
        for (AudioFileWriter curr : writers.values())
        {
            curr.setAudioFileModificationListener(this.modificationHandler);
        }
    
public static AudioFileread(java.io.File f)

Read the tag contained in the given file.

param
f The file to read.
return
The AudioFile with the file tag and the file encoding info.
throws
CannotReadException If the file could not be read, the extension wasn't recognized, or an IO error occurred during the read.
throws
org.jaudiotagger.tag.TagException
throws
org.jaudiotagger.audio.exceptions.ReadOnlyFileException
throws
java.io.IOException
throws
org.jaudiotagger.audio.exceptions.InvalidAudioFrameException

        return getDefaultAudioFileIO().readFile(f);
    
public AudioFilereadFile(java.io.File f)

Read the tag contained in the given file.

param
f The file to read.
return
The AudioFile with the file tag and the file encoding info.
throws
CannotReadException If the file could not be read, the extension wasn't recognized, or an IO error occurred during the read.
throws
org.jaudiotagger.tag.TagException
throws
org.jaudiotagger.audio.exceptions.ReadOnlyFileException
throws
java.io.IOException
throws
org.jaudiotagger.audio.exceptions.InvalidAudioFrameException

        checkFileExists(f);
        String ext = Utils.getExtension(f);

        AudioFileReader afr = readers.get(ext);
        if (afr == null)
        {
            throw new CannotReadException(ErrorMessage.NO_READER_FOR_THIS_FORMAT.getMsg(ext));
        }

        return afr.read(f);
    
public voidremoveAudioFileModificationListener(AudioFileModificationListener listener)
Removes an listener for all file formats.

param
listener listener

        this.modificationHandler.removeAudioFileModificationListener(listener);
    
public static voidwrite(AudioFile f)

Write the tag contained in the audioFile in the actual file on the disk.

param
f The AudioFile to be written
throws
CannotWriteException If the file could not be written/accessed, the extension wasn't recognized, or other IO error occurred.

        getDefaultAudioFileIO().writeFile(f);
    
public voidwriteFile(AudioFile f)

Write the tag contained in the audioFile in the actual file on the disk.

param
f The AudioFile to be written
throws
CannotWriteException If the file could not be written/accessed, the extension wasn't recognized, or other IO error occurred.

        String ext = Utils.getExtension(f.getFile());

        AudioFileWriter afw = writers.get(ext);
        if (afw == null)
        {
            throw new CannotWriteException(ErrorMessage.NO_WRITER_FOR_THIS_FORMAT.getMsg(ext));
        }

        afw.write(f);