Audio Frame
Create Audio Frame
Aspose.Slides for Node.js via Java allows you to add audio files to slides. The audio files are embedded in slides as audio frames.
- Create an instance of the Presentation class.
- Get a slide’s reference through its index.
- Load the audio file stream you want to embed in the slide.
- Add the embedded audio frame (containing the audio file) to the slide.
- Set PlayMode and
Volume
exposed by the AudioFrame object. - Save the modified presentation.
This JavaScript code shows you how to add an embedded audio frame to a slide:
// Instantiates a Presentation class that represents a presentation file
var pres = new aspose.slides.Presentation();
try {
// Gets the first slide
var sld = pres.getSlides().get_Item(0);
// Loads the wav sound file to stream
var fstr = java.newInstanceSync("java.io.FileInputStream", java.newInstanceSync("java.io.File", "audio.wav"));
// Adds the Audio Frame
var audioFrame = sld.getShapes().addAudioFrameEmbedded(50, 150, 100, 100, fstr);
fstr.close();
// Sets the Play Mode and Volume of the Audio
audioFrame.setPlayMode(aspose.slides.AudioPlayModePreset.Auto);
audioFrame.setVolume(aspose.slides.AudioVolumeMode.Loud);
// Writes the PowerPoint file to disk
pres.save("AudioFrameEmbed_out.pptx", aspose.slides.SaveFormat.Pptx);
} catch (e) {console.log(e);
} finally {
if (pres != null) {
pres.dispose();
}
}
Change Audio Frame Thumbnail
When you add an audio file to a presentation, the audio appears as a frame with a standard default image (see the image in the section below). You change the audio frame’s preview image (set your preferred image).
This JavaScript code shows you how to change an audio frame’s thumbnail or preview image:
var presentation = new aspose.slides.Presentation();
try {
var slide = presentation.getSlides().get_Item(0);
// Adds an audio frame to the slide with a specified position and size.
var audioStream = java.newInstanceSync("java.io.FileInputStream", "sample2.mp3");
var audioFrame = slide.getShapes().addAudioFrameEmbedded(150, 100, 50, 50, audioStream);
audioStream.close();
// Adds an image to presentation resources.
var picture;
var image = aspose.slides.Images.fromFile("eagle.jpeg");
try {
picture = presentation.getImages().addImage(image);
} finally {
if (image != null) {
image.dispose();
}
}
// Sets the image for the audio frame.
audioFrame.getPictureFormat().getPicture().setImage(picture);// <-----
// Saves the modified presentation to disk
presentation.save("example_out.pptx", aspose.slides.SaveFormat.Pptx);
} catch (e) {console.log(e);
} finally {
if (presentation != null) {
presentation.dispose();
}
}
Change Audio Play Options
Aspose.Slides for Node.js via Java allows you to change options that control an audio’s playback or properties. For example, you can adjust an audio’s volume, set the audio to play looped, or even hide the audio icon.
The Audio Options pane in Microsoft PowerPoint:
PowerPoint Audio options that correspond to Aspose.Slides AudioFrame properties:
- Audio Options Start drop-down list matches the AudioFrame.PlayMode property
- Audio Options Volume matches the AudioFrame.Volume property
- Audio Options Play Across Slides matches the AudioFrame.PlayAcrossSlides property
- Audio Options Loop until Stopped matches the AudioFrame.PlayLoopMode property
- Audio Options Hide During Show matches the AudioFrame.HideAtShowing property
- Audio Options Rewind after Playing matches the AudioFrame.RewindAudio property
This is how you change the Audio Play options:
- Сreate or get the Audio Frame.
- Set new values for the Audio Frame properties you want to adjust.
- Save the modified PowerPoint file.
This JavaScript code demonstrates an operation in which an audio’s options are adjusted:
var pres = new aspose.slides.Presentation("AudioFrameEmbed_out.pptx");
try {
// Gets the AudioFrame shape
var audioFrame = pres.getSlides().get_Item(0).getShapes().get_Item(0);
// Sets the Play mode to play on click
audioFrame.setPlayMode(aspose.slides.AudioPlayModePreset.OnClick);
// Sets the volume to Low
audioFrame.setVolume(aspose.slides.AudioVolumeMode.Low);
// Sets the audio to play across slides
audioFrame.setPlayAcrossSlides(true);
// Disables loop for the audio
audioFrame.setPlayLoopMode(false);
// Hides the AudioFrame during the slide show
audioFrame.setHideAtShowing(true);
// Rewinds the audio to start after playing
audioFrame.setRewindAudio(true);
// Saves the PowerPoint file to disk
pres.save("AudioFrameEmbed_changed.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Extract Audio
Aspose.Slides for Node.js via Java allows you to extract the sound used in slide show transitions. For example, you can extract the sound used in a specific slide.
- Create an instance of the Presentation class and load the presentation containing the audio.
- Get the relevant slide’s reference through its index.
- Access the slideshow transitions for the slide.
- Extract the sound in byte data.
This code in JavaScript shows you how to extract the audio used in a slide:
// Instantiates a Presentation class that represents a presentation file
var pres = new aspose.slides.Presentation("AudioSlide.pptx");
try {
// Accesses the desired slide
var slide = pres.getSlides().get_Item(0);
// Gets the slideshow transition effects for the slide
var transition = slide.getSlideShowTransition();
// Extracts the sound in byte array
var audio = transition.getSound().getBinaryData();
console.log("Length: " + audio.length);
} finally {
if (pres != null) {
pres.dispose();
}
}