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

AudioFile

public class AudioFile extends Object

This is the main object manipulated by the user representing an audiofile, its properties and its tag.

The prefered way to obtain an AudioFile is to use the AudioFileIO.read(File) method.

The AudioFile contains every properties associated with the file itself (no meta-data), like the bitrate, the sampling rate, the encoding audioHeaders, etc.

To get the meta-data contained in this file you have to get the Tag of this AudioFile

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

Fields Summary
public static Logger
logger
protected File
file
The physical file that this instance represents.
protected AudioHeader
audioHeader
The Audio header info
protected org.jaudiotagger.tag.Tag
tag
The tag
Constructors Summary
public AudioFile()


     
    

    
public AudioFile(File f, AudioHeader audioHeader, org.jaudiotagger.tag.Tag tag)

These constructors are used by the different readers, users should not use them, but use the AudioFileIO.read(File) method instead !.

Create the AudioFile representing file f, the encoding audio headers and containing the tag

param
f The file of the audio file
param
audioHeader the encoding audioHeaders over this file
param
tag the tag contained in this file or null if no tag exists

        this.file = f;
        this.audioHeader = audioHeader;
        this.tag = tag;
    
public AudioFile(String s, AudioHeader audioHeader, org.jaudiotagger.tag.Tag tag)

These constructors are used by the different readers, users should not use them, but use the AudioFileIO.read(File) method instead !.

Create the AudioFile representing file denoted by pathnames, the encoding audio Headers and containing the tag

param
s The pathname of the audio file
param
audioHeader the encoding audioHeaders over this file
param
tag the tag contained in this file

        this.file = new File(s);
        this.audioHeader = audioHeader;
        this.tag = tag;
    
Methods Summary
public voidcheckFileExists(java.io.File file)
Check does file exist

param
file
throws
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()));
        }
    
protected java.io.RandomAccessFilecheckFilePermissions(java.io.File file, boolean readOnly)
Checks the file is accessible with the correct permissions, otherwise exception occurs

param
file
param
readOnly
throws
ReadOnlyFileException
throws
FileNotFoundException
return

        RandomAccessFile newFile;

        checkFileExists(file);

        // Unless opened as readonly the file must be writable
        if (readOnly)
        {
            newFile = new RandomAccessFile(file, "r");
        }
        else
        {
            if (!file.canWrite())
            {
                logger.severe("Unable to write:" + file.getPath());
                throw new ReadOnlyFileException(ErrorMessage.NO_PERMISSIONS_TO_WRITE_TO_FILE.getMsg(file.getPath()));
            }
            newFile = new RandomAccessFile(file, "rws");
        }
        return newFile;
    
public voidcommit()

Write the tag contained in this AudioFile in the actual file on the disk, this is the same as calling the AudioFileIO.write(this) method.

throws
CannotWriteException If the file could not be written/accessed, the extension wasn't recognized, or other IO error occured.
see
AudioFileIO

        AudioFileIO.write(this);
    
public org.jaudiotagger.tag.TagcreateDefaultTag()
Create Default Tag

return

        if(SupportedFileFormat.FLAC.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('."))))
        {
            return new FlacTag(VorbisCommentTag.createNewTag(), new ArrayList< MetadataBlockDataPicture >());
        }
        else if(SupportedFileFormat.OGG.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('."))))
        {
            return VorbisCommentTag.createNewTag();
        }
        else if(SupportedFileFormat.MP4.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('."))))
        {
            return new Mp4Tag();
        }
        else if(SupportedFileFormat.M4A.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('."))))
        {
            return new Mp4Tag();
        }
        else if(SupportedFileFormat.M4P.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('."))))
        {
            return new Mp4Tag();
        }
        else if(SupportedFileFormat.WMA.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('."))))
        {
            return new AsfTag();
        }
        else if(SupportedFileFormat.WAV.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('."))))
        {
            return new WavTag();
        }
        else if(SupportedFileFormat.RA.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('."))))
        {
            return new RealTag();
        }
        else if(SupportedFileFormat.RM.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('."))))
        {
            return new RealTag();
        }
        else
        {
            throw new RuntimeException("Unable to create default tag for this file format");
        }

    
public java.lang.StringdisplayStructureAsPlainText()
Optional debugging method

return

        return "";
    
public java.lang.StringdisplayStructureAsXML()
Optional debugging method

return

        return "";
    
public AudioHeadergetAudioHeader()
Return audio header

return

        return audioHeader;
    
public static java.lang.StringgetBaseFilename(java.io.File file)

param
file
return
filename with audioFormat separator stripped of.

        int index=file.getName().toLowerCase().lastIndexOf(".");
        if(index>0)
        {
            return file.getName().substring(0,index);
        }
        return file.getName();
    
public java.io.FilegetFile()
Retrieve the physical file

return

        return file;
    
public org.jaudiotagger.tag.TaggetTag()

Returns the tag contained in this AudioFile, the Tag contains any useful meta-data, like artist, album, title, etc. If the file does not contain any tag the null is returned. Some audio formats do not allow there to be no tag so in this case the reader would return an empty tag whereas for others such as mp3 it is purely optional.

return
Returns the tag contained in this AudioFile, or null if no tag exists.

        return tag;
    
public org.jaudiotagger.tag.TaggetTagOrCreateAndSetDefault()
Get the tag or if the file doesn't have one at all, create a default tag and set it

return

        Tag tag = getTag();
        if(tag==null)
        {
            tag = createDefaultTag();
            setTag(tag);
            return tag;
        }
        return tag;
    
public org.jaudiotagger.tag.TaggetTagOrCreateDefault()
Get the tag or if the file doesn't have one at all, create a default tag and return

return

        Tag tag = getTag();
        if(tag==null)
        {
            return createDefaultTag();
        }
        return tag;
    
public voidsetFile(java.io.File file)
Set the file to store the info in

param
file

        this.file = file;
    
public voidsetTag(org.jaudiotagger.tag.Tag tag)

        this.tag = tag;
    
public java.lang.StringtoString()

Returns a multi-line string with the file path, the encoding audioHeader, and the tag contents.

return
A multi-line string with the file path, the encoding audioHeader, and the tag contents. TODO Maybe this can be changed ?

        return "AudioFile " + getFile().getAbsolutePath()
                + "  --------\n" + audioHeader.toString() + "\n" + ((tag == null) ? "" : tag.toString()) + "\n-------------------";