Manage Audio in Presentations Using PHP
Create Audio Frames
Aspose.Slides for PHP 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 Volumeexposed by the IAudioFrame object.
- Save the modified presentation.
This PHP code shows you how to add an embedded audio frame to a slide:
// Instantiates a Presentation class that represents a presentation file
$pres = new Presentation();
try {
    # Gets the first slide
    $sld = $pres->getSlides()->get_Item(0);
    # Loads the wav sound file to stream
    $fstr = new Java("java.io.FileInputStream", new Java("java.io.File", "audio.wav"));
    # Adds the Audio Frame
    $audioFrame = $sld->getShapes()->addAudioFrameEmbedded(50, 150, 100, 100, $fstr);
    $fstr->close();
    # Sets the Play Mode and Volume of the Audio
    $audioFrame->setPlayMode(AudioPlayModePreset->Auto);
    $audioFrame->setVolume(AudioVolumeMode->Loud);
    # Writes the PowerPoint file to disk
    $pres->save("AudioFrameEmbed_out.pptx", SaveFormat::Pptx);
} catch(JavaException e) {
} finally {
    if (!java_is_null($pres)) $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 PHP code shows you how to change an audio frame’s thumbnail or preview image:
$presentation = new Presentation();
try {
	$slide = $presentation->getSlides()->get_Item(0);
	# Adds an audio frame to the slide with a specified position and size.
	$audioStream = new Java("java.io.FileInputStream", "sample2.mp3");
	$audioFrame = $slide->getShapes()->addAudioFrameEmbedded(150, 100, 50, 50, $audioStream);
	$audioStream->close();
	# Adds an image to presentation resources.
	$picture;
	$image = Images->fromFile("eagle.jpeg");
	try {
		$picture = $presentation->getImages()->addImage($image);
	} finally {
		if (!java_is_null($image)) {
			$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", SaveFormat::Pptx);
} catch (JavaException $e) {
} finally {
	if (!java_is_null($presentation)) {
		$presentation->dispose();
	}
}
Change Audio Play Options
Aspose.Slides for PHP 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:
- Start drop-down list matches the AudioFrame.setPlayMode method
- Volume matches the AudioFrame.setVolume method
- Play Across Slides matches the AudioFrame.setPlayAcrossSlides method
- Loop until Stopped matches the AudioFrame.setPlayLoopMode method
- Hide During Show matches the AudioFrame.setHideAtShowing method
- Rewind after Playing matches the AudioFrame.setRewindAudio method
PowerPoint Editing options that correspond to Aspose.Slides AudioFrame properties:
- Fade In matches the AudioFrame.setFadeInDuration method
- Fade Out matches the AudioFrame.setFadeOutDuration method
- Trim Audio Start Time matches the AudioFrame.setTrimFromStart method
- Trim Audio End Time value equals the audio duration minus the value of AudioFrame.setTrimFromEnd method
The PowerPoint Volume controll on the audio control panel corresponds to the AudioFrame.setVolumeValue method. It lets you change the audio volume as a percentage.
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 PHP code demonstrates an operation in which an audio’s options are adjusted:
$pres = new Presentation("AudioFrameEmbed_out.pptx");
try {
    # Gets the AudioFrame shape
    $audioFrame = $pres->getSlides()->get_Item(0)->getShapes()->get_Item(0);
    # Sets the Play mode to play on click
    $audioFrame->setPlayMode(AudioPlayModePreset->OnClick);
    # Sets the volume to Low
    $audioFrame->setVolume(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", SaveFormat::Pptx);
} finally {
	if (!java_is_null($pres)) {
		$pres->dispose();
	}
}
This PHP example shows how to add a new audio frame with embedded audio, trim it, and set the fade durations:
$pres = new Presentation();
try {
    $slide = $pres->getSlides()->get_Item(0);
    $audioData = file_get_contents("sampleaudio.mp3");
    $audio = $pres->getAudios()->addAudio($audioData);
    $audioFrame = $slide->getShapes()->addAudioFrameEmbedded(50, 50, 100, 100, $audio);
    // Sets the trimming start offset to 1.5 seconds
    $audioFrame->setTrimFromStart(1500);
    // Sets the trimming end offset to 2 seconds
    $audioFrame->setTrimFromEnd(2000);
    // Sets the fade-in duration to 200 ms
    $audioFrame->setFadeInDuration(200);
    // Sets the fade-out duration to 500 ms
    $audioFrame->setFadeOutDuration(500);
    $pres->save("AudioFrameTrimFade_out.pptx", SaveFormat::Pptx);
} finally {
    $pres->dispose();
}
The following code sample shows how to retrieve an audio frame with embedded audio and set its volume to 85%:
$pres = new Presentation("AudioFrameEmbed_out.pptx");
try {
    $slide = $pres->getSlides()->get_Item(0);
    // Gets an audio frame shape
    $audioFrame = $slide->getShapes()->get_Item(0);
    // Sets the audio volume to 85%
    $audioFrame->setVolumeValue(85);
    $pres->save("AudioFrameValue_out.pptx", SaveFormat::Pptx);
}
finally {
    $pres->dispose();
}
Extract Audio
Aspose.Slides for PHP 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 shows you how to extract the audio used in a slide:
# Instantiates a Presentation class that represents a presentation file
$pres = new Presentation("AudioSlide.pptx");
$Array = new java_class("java.lang.reflect.Array");
try {
	# Accesses the desired slide
	$slide = $pres->getSlides()->get_Item(0);
	# Gets the slideshow transition effects for the slide
	$transition = $slide->getSlideShowTransition();
	# Extracts the sound in byte array
	$audio = $transition->getSound()->getBinaryData();
	echo("Length: " . $Array->getLength($audio));
} finally {
	if (!java_is_null($pres)) {
		$pres->dispose();
	}
}