Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active September 21, 2021 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/c09e6c303ca65a21602154c569acc0a5 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/c09e6c303ca65a21602154c569acc0a5 to your computer and use it in GitHub Desktop.
Insert and Extract Audio in PowerPoint using Java
// Load presentation
Presentation pres = new Presentation("AudioSlide.pptx");
try {
// Access the desired slide
ISlide slide = pres.getSlides().get_Item(0);
// Get the slideshow transition effects for slide
ISlideShowTransition transition = slide.getSlideShowTransition();
// Extract sound in byte array
byte[] audio = transition.getSound().getBinaryData();
System.out.println("Length: " + audio.length);
} finally {
if (pres != null) pres.dispose();
}
// Load or create presentation
Presentation pres = new Presentation("AudioFrameEmbed_out.pptx");
try {
// Get the first slide
ISlide sld = pres.getSlides().get_Item(0);
// Load the wav sound file to stream
FileInputStream fstr = new FileInputStream(new File("audio.wav"));
// Add audio frame
IAudioFrame audioFrame = sld.getShapes().addAudioFrameEmbedded(50, 150, 100, 100, fstr);
fstr.close();
// Change play mode to play on click
audioFrame.setPlayMode(AudioPlayModePreset.OnClick);
// Set volume to Low
audioFrame.setVolume(AudioVolumeMode.Low);
// Set audio to play across slides
audioFrame.setPlayAcrossSlides(true);
// Set audio to not loop
audioFrame.setPlayLoopMode(false);
// Hide AudioFrame during the slide show
audioFrame.setHideAtShowing(true);
// Rewind audio to start after playing
audioFrame.setRewindAudio(true);
// Save the PPTX file to disk
pres.save("AudioFrameEmbed_changed.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment