Search in sources :

Example 1 with SoundFormat

use of de.gurkenlabs.litiengine.resources.SoundFormat in project litiengine by gurkenlabs.

the class Editor method importSounds.

private void importSounds(File... selectedFiles) {
    for (File file : selectedFiles) {
        try (InputStream stream = new FileInputStream(file.getAbsolutePath())) {
            SoundFormat format = SoundFormat.get(FileUtilities.getExtension(file));
            SoundResource resource = new SoundResource(new BufferedInputStream(stream), FileUtilities.getFileName(file.getName()), format);
            this.getGameFile().getSounds().removeIf(x -> x.getName().equals(resource.getName()));
            this.getGameFile().getSounds().add(resource);
            log.log(Level.INFO, "imported sound {0}", new Object[] { resource.getName() });
        } catch (IOException | UnsupportedAudioFileException e) {
            log.log(Level.SEVERE, e.getMessage(), e);
        }
    }
}
Also used : UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SoundResource(de.gurkenlabs.litiengine.resources.SoundResource) IOException(java.io.IOException) SoundFormat(de.gurkenlabs.litiengine.resources.SoundFormat) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with SoundFormat

use of de.gurkenlabs.litiengine.resources.SoundFormat in project litiengine by gurkenlabs.

the class AssetPanelItem method exportSound.

private void exportSound() {
    if (!(this.getOrigin() instanceof SoundResource)) {
        return;
    }
    SoundResource sound = (SoundResource) this.getOrigin();
    SoundFormat format = sound.getFormat();
    if (format == SoundFormat.UNSUPPORTED) {
        return;
    }
    FileFilter filter = new FileNameExtensionFilter(format.toString() + " - Sound", format.toString());
    try {
        JFileChooser chooser;
        String source = Editor.instance().getProjectPath();
        chooser = new JFileChooser(source != null ? source : new File(".").getCanonicalPath());
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        chooser.setDialogTitle("Export Sound");
        chooser.setFileFilter(filter);
        chooser.addChoosableFileFilter(filter);
        chooser.setSelectedFile(new File(sound.getName() + format.toFileExtension()));
        int result = chooser.showSaveDialog(Game.window().getRenderComponent());
        if (result == JFileChooser.APPROVE_OPTION) {
            try (FileOutputStream fos = new FileOutputStream(chooser.getSelectedFile().toString())) {
                fos.write(Codec.decode(sound.getData()));
                log.log(Level.INFO, "exported sound {0} to {1}", new Object[] { sound.getName(), chooser.getSelectedFile() });
            }
        }
    } catch (IOException ex) {
        log.log(Level.SEVERE, ex.getMessage(), ex);
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) FileOutputStream(java.io.FileOutputStream) SoundResource(de.gurkenlabs.litiengine.resources.SoundResource) IOException(java.io.IOException) SoundFormat(de.gurkenlabs.litiengine.resources.SoundFormat) FileFilter(javax.swing.filechooser.FileFilter) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) File(java.io.File) Blueprint(de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint)

Aggregations

SoundFormat (de.gurkenlabs.litiengine.resources.SoundFormat)2 SoundResource (de.gurkenlabs.litiengine.resources.SoundResource)2 File (java.io.File)2 IOException (java.io.IOException)2 Blueprint (de.gurkenlabs.litiengine.environment.tilemap.xml.Blueprint)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)1 JFileChooser (javax.swing.JFileChooser)1 FileFilter (javax.swing.filechooser.FileFilter)1 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)1