Διαχείριση audio frames σε παρουσιάσεις σε Android

Επισκόπηση

Ένα audio frame σάς επιτρέπει να αναπαράγετε έναν ήχο σε μια διαφάνεια. Με το Aspose.Slides για Android μέσω Java, μπορείτε να ενσωματώσετε ένα audio clip, να επιλέξετε την εικόνα που το αντιπροσωπεύει, να ρυθμίσετε την αναπαραγωγή και να προσθέσετε υπότιτλους. Μπορείτε επίσης να ανακτήσετε έναν ήχο που έχει ανατεθεί σε μια μετάβαση διαφάνειας.

Create Audio Frames

Use addAudioFrameEmbedded to embed a local audio clip on the first slide. The first two coordinates position the frame on the slide, and the next two values set its width and height, all in points. This example calls setPlayMode to start the clip automatically and calls setVolume to select the loud preset.

import com.aspose.slides.*;
import java.io.FileInputStream;

Presentation presentation = new Presentation();
ISlide slide = presentation.getSlides().get_Item(0);

try (FileInputStream audioStream = new FileInputStream("audio.wav")) {
    IAudioFrame audioFrame = slide.getShapes().addAudioFrameEmbedded(50, 150, 100, 100, audioStream);
    audioFrame.setPlayMode(AudioPlayModePreset.Auto);
    audioFrame.setVolume(AudioVolumeMode.Loud);

    presentation.save("AudioFrameEmbed_out.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Change the Audio Frame Thumbnail

By default, an audio frame displays a speaker icon. To display a custom thumbnail, add an image to the presentation with addImage and set it on the frame’s picture with setImage. The example uses a JPEG as the thumbnail for an embedded audio clip.

import com.aspose.slides.*;
import java.io.FileInputStream;

Presentation presentation = new Presentation();
ISlide slide = presentation.getSlides().get_Item(0);

try (FileInputStream audioStream = new FileInputStream("audio.wav");
     FileInputStream imageStream = new FileInputStream("eagle.jpeg")) {
    IPPImage thumbnail = presentation.getImages().addImage(imageStream);

    IAudioFrame audioFrame = slide.getShapes().addAudioFrameEmbedded(150, 100, 50, 50, audioStream);
    audioFrame.getPictureFormat().getPicture().setImage(thumbnail);

    presentation.save("example_out.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Change Audio Play Options

PowerPoint’s Audio Options controls determine when a clip starts, how loud it sounds, and how it behaves during a slide show.

Έλεγχοι Audio Options του PowerPoint για ένα audio frame

Έλεγχος PowerPoint Μέθοδος Aspose.Slides για Android μέσω Java
Έναρξη setPlayMode
Προεπιλογή έντασης setVolume
Αναπαραγωγή σε όλες τις διαφάνειες setPlayAcrossSlides
Επανάληψη μέχρι τη διακοπή setPlayLoopMode
Απόκρυψη κατά τη διάρκεια της παρουσίασης setHideAtShowing
Επαναφορά μετά την αναπαραγωγή setRewindAudio
Ομαλή είσοδος setFadeInDuration
Ομαλή έξοδος setFadeOutDuration
Περικοπή χρόνου έναρξης ήχου setTrimFromStart
Περικοπή χρόνου λήξης ήχου setTrimFromEnd
Ρύθμιση έντασης setVolumeValue

The example finds the first audio frame on the first slide, calls setPlayMode to start playback on a click, and selects the low volume preset. The clip continues across slides with looping disabled. The icon stays hidden during the show, and the clip rewinds after playback.

import com.aspose.slides.*;

Presentation presentation = new Presentation("AudioFrameEmbed_out.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAudioFrame audioFrame = null;
    for (IShape shape : slide.getShapes()) {
        if (shape instanceof IAudioFrame) {
            audioFrame = (IAudioFrame) shape;
            break;
        }
    }

    if (audioFrame != null) {
        audioFrame.setPlayMode(AudioPlayModePreset.OnClick);
        audioFrame.setVolume(AudioVolumeMode.Low);
        audioFrame.setPlayAcrossSlides(true);
        audioFrame.setPlayLoopMode(false);
        audioFrame.setHideAtShowing(true);
        audioFrame.setRewindAudio(true);
        presentation.save("AudioFrameEmbed_changed.pptx", SaveFormat.Pptx);
    } else {
        System.out.println("The first slide has no audio frame.");
    }
} finally {
    presentation.dispose();
}

For a clip longer than 3.5 seconds, this example skips the first 1.5 seconds and the last 2 seconds. setTrimFromEnd takes the duration to omit from the end of the clip. To get the end time shown in PowerPoint, subtract that offset from the full clip duration. The trimmed clip fades in over 200 milliseconds and fades out over 500 milliseconds. All four methods take values in milliseconds.

import com.aspose.slides.*;
import java.io.FileInputStream;

Presentation presentation = new Presentation();
ISlide slide = presentation.getSlides().get_Item(0);

try (FileInputStream audioStream = new FileInputStream("audio.wav")) {
    IAudioFrame audioFrame = slide.getShapes().addAudioFrameEmbedded(50, 50, 100, 100, audioStream);
    audioFrame.setTrimFromStart(1500f);
    audioFrame.setTrimFromEnd(2000f);
    audioFrame.setFadeInDuration(200f);
    audioFrame.setFadeOutDuration(500f);

    presentation.save("AudioFrameTrimFade_out.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

To set the playback volume to a specific percentage, use setVolumeValue. This example sets the first audio frame on the first slide to 85%.

import com.aspose.slides.*;

Presentation presentation = new Presentation("AudioFrameEmbed_out.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAudioFrame audioFrame = null;
    for (IShape shape : slide.getShapes()) {
        if (shape instanceof IAudioFrame) {
            audioFrame = (IAudioFrame) shape;
            break;
        }
    }

    if (audioFrame != null) {
        audioFrame.setVolumeValue(85f);
        presentation.save("AudioFrameValue_out.pptx", SaveFormat.Pptx);
    } else {
        System.out.println("The first slide has no audio frame.");
    }
} finally {
    presentation.dispose();
}

Manage Audio Captions

Each audio frame provides access to its caption tracks through getCaptionTracks. Use the collection to attach WebVTT captions to the clip, export existing tracks, or remove them.

Add Audio Captions

The example embeds an audio clip on the first slide and uses add to attach a local WebVTT track. The first argument supplies the track label, and the second identifies the file containing the timed captions.

import com.aspose.slides.*;
import java.io.FileInputStream;

Presentation presentation = new Presentation();
ISlide slide = presentation.getSlides().get_Item(0);

try (FileInputStream audioStream = new FileInputStream("audio.wav")) {
    IAudioFrame audioFrame = slide.getShapes().addAudioFrameEmbedded(10, 10, 50, 50, audioStream);
    audioFrame.getCaptionTracks().add("English", "track.vtt");

    presentation.save("audio_with_captions.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Extract Audio Captions

This example exports each caption track attached to an audio frame on the first slide. It writes the bytes returned by getBinaryData to a WebVTT file and uses the identifier returned by getCaptionId as the file name so that each track has a separate output.

import com.aspose.slides.*;
import java.io.FileOutputStream;

Presentation presentation = new Presentation("audio_with_captions.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    for (IShape shape : slide.getShapes()) {
        if (!(shape instanceof IAudioFrame)) {
            continue;
        }
        IAudioFrame audioFrame = (IAudioFrame) shape;
        for (ICaptions track : audioFrame.getCaptionTracks()) {
            try (FileOutputStream output = new FileOutputStream(track.getCaptionId() + ".vtt")) {
                output.write(track.getBinaryData());
            }
        }
    }
} finally {
    presentation.dispose();
}

Remove Audio Captions

Use remove or removeAt to delete an individual track. The example uses clear to remove all captions from the first audio frame on the first slide while preserving the clip.

import com.aspose.slides.*;

Presentation presentation = new Presentation("audio_with_captions.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAudioFrame audioFrame = null;
    for (IShape shape : slide.getShapes()) {
        if (shape instanceof IAudioFrame) {
            audioFrame = (IAudioFrame) shape;
            break;
        }
    }

    if (audioFrame != null) {
        audioFrame.getCaptionTracks().clear();
        presentation.save("audio_without_captions.pptx", SaveFormat.Pptx);
    } else {
        System.out.println("The first slide has no audio frame.");
    }
} finally {
    presentation.dispose();
}

Find a Line in Audio Captions

To find a line in the audio captions on the first slide, search the text returned by getDataAsString in memory. In a plain-text WebVTT cue, the timing line contains -->, the caption lines follow it, and a blank line ends the cue. This example prints the track label and each caption line containing “café”.

import com.aspose.slides.*;
import java.util.Locale;

Presentation presentation = new Presentation("audio_with_captions.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    for (IShape shape : slide.getShapes()) {
        if (!(shape instanceof IAudioFrame)) {
            continue;
        }
        IAudioFrame audioFrame = (IAudioFrame) shape;
        for (ICaptions track : audioFrame.getCaptionTracks()) {
            boolean inCue = false;
            String captions = track.getDataAsString();
            for (String line : captions.split("\\r\\n|\\r|\\n")) {
                if (line.isEmpty()) {
                    inCue = false;
                } else if (line.contains("-->")) {
                    inCue = true;
                } else if (inCue && line.toLowerCase(Locale.ROOT).contains("café")) {
                    System.out.println(track.getLabel() + ": " + line);
                }
            }
        }
    }
} finally {
    presentation.dispose();
}

Extract Audio

A slide transition can have its own sound. To retrieve it, call getSound on the slide transition. The example accesses the bytes returned by getBinaryData and prints the number of bytes. If the first slide has no transition sound, it prints a message instead.

import com.aspose.slides.*;

Presentation presentation = new Presentation("AudioSlide.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    IAudio sound = slide.getSlideShowTransition().getSound();
    if (sound != null) {
        byte[] audioData = sound.getBinaryData();
        System.out.println("Transition sound: " + audioData.length + " bytes.");
    } else {
        System.out.println("The first slide has no transition sound.");
    }
} finally {
    presentation.dispose();
}

FAQ

Μπορώ να επαναχρησιμοποιήσω το ίδιο audio clip σε πολλές διαφάνειες χωρίς να το αντιγράψω;

Yes. Add the clip once to the presentation’s audio collection and pass the same audio object to addAudioFrameEmbedded for each slide.

Μπορώ να αντικαταστήσω τον ήχο σε ένα υπάρχον audio frame;

Yes. For a linked sound, use setLinkPathLong to point to the replacement file. For an embedded sound, select another clip from the presentation’s audio collection with setEmbeddedAudio. Both approaches preserve the existing frame.

Αλλάζει η περικοπή τα δεδομένα ήχου που αποθηκεύονται στην παρουσίαση;

No. setTrimFromStart and setTrimFromEnd set playback boundaries. You can still retrieve the full clip from getEmbeddedAudio.