Manage Audio in Presentations Using C#
Create Audio Frames
Aspose.Slides for .NET 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 IAudioFrame object. - Save the modified presentation.
This C# code shows you how to add an embedded audio frame to a slide:
// Instantiates a presentation class that represents a presentation file
using (Presentation pres = new Presentation())
{
// Gets the first slide
ISlide sld = pres.Slides[0];
// Loads the the wav sound file to stream
FileStream fstr = new FileStream("sampleaudio.wav", FileMode.Open, FileAccess.Read);
// Adds the Audio Frame
IAudioFrame audioFrame = sld.Shapes.AddAudioFrameEmbedded(50, 150, 100, 100, fstr);
// Sets the Play Mode and Volume of the Audio
audioFrame.PlayMode = AudioPlayModePreset.Auto;
audioFrame.Volume = AudioVolumeMode.Loud;
// Writes the PowerPoint file to disk
pres.Save("AudioFrameEmbed_out.pptx", SaveFormat.Pptx);
}
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 thumbnail (set your preferred image).
This C# code shows you how to change an audio frame’s thumbnail or preview image:
using (var presentation = new Presentation())
{
var slide = presentation.Slides[0];
// Adds an audio frame to the slide with a specified position and size.
var audioStream = new FileStream("sample2.mp3", FileMode.Open, FileAccess.Read);
var audioFrame = slide.Shapes.AddAudioFrameEmbedded(150, 100, 50, 50, audioStream);
audioStream.Dispose();
// Adds an image to presentation resources.
var imageStream = File.OpenRead("eagle.jpeg");
var audioImage = presentation.Images.AddImage(imageStream);
imageStream.Dispose();
// Sets the image for the audio frame.
audioFrame.PictureFormat.Picture.Image = audioImage; // <-----
//Saves the modified presentation to disk
presentation.Save("example_out.pptx", SaveFormat.Pptx);
}
Change Audio Play Options
Aspose.Slides for .NET 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 menu matches the AudioFrame.PlayMode property
- Volume matches the AudioFrame.Volume property
- Play Across Slides matches the AudioFrame.PlayAcrossSlides property
- Loop until Stopped matches the AudioFrame.PlayLoopMode property
- Hide During Show matches the AudioFrame.HideAtShowing property
- Rewind after Playing matches the AudioFrame.RewindAudio property
PowerPoint Editing options that correspond to Aspose.Slides AudioFrame properties:
- Fade In matches the AudioFrame.FadeInDuration property
- Fade Out matches the AudioFrame.FadeOutDuration property
- Trim Audio Start Time matches the AudioFrame.TrimFromStart property
- Trim Audio End Time value equals the audio duration minus the value of AudioFrame.TrimFromEnd property
The PowerPoint Volume controll on the audio control panel corresponds to the AudioFrame.VolumeValue property. 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 C# code demonstrates an operation in which an audio’s options are adjusted:
using (Presentation pres = new Presentation("AudioFrameEmbed_out.pptx"))
{
// Gets the AudioFrame shape
AudioFrame audioFrame = (AudioFrame)pres.Slides[0].Shapes[0];
// Sets the Play mode to play on click
audioFrame.PlayMode = AudioPlayModePreset.OnClick;
// Sets the volume to Low
audioFrame.Volume = AudioVolumeMode.Low;
// Sets the audio to play across slides
audioFrame.PlayAcrossSlides = true;
// Disables loop for the audio
audioFrame.PlayLoopMode = false;
// Hides the AudioFrame during the slide show
audioFrame.HideAtShowing = true;
// Rewinds the audio to start after playing
audioFrame.RewindAudio = true;
// Saves the PowerPoint file to disk
pres.Save("AudioFrameEmbed_changed.pptx", SaveFormat.Pptx);
}
This C# example shows how to add a new audio frame with embedded audio, trim it, and set the fade durations:
using (Presentation pres = new Presentation())
{
ISlide slide = pres.Slides[0];
byte[] audioData = File.ReadAllBytes("sampleaudio.mp3");
IAudio audio = pres.Audios.AddAudio(audioData);
IAudioFrame audioFrame = slide.Shapes.AddAudioFrameEmbedded(50, 50, 100, 100, audio);
// Sets the trimming start offset to 1.5 seconds
audioFrame.TrimFromStart = 1500f;
// Sets the trimming end offset to 2 seconds
audioFrame.TrimFromEnd = 2000f;
// Sets the fade-in duration to 200 ms
audioFrame.FadeInDuration = 200f;
// Sets the fade-out duration to 500 ms
audioFrame.FadeOutDuration = 500f;
pres.Save("AudioFrameTrimFade_out.pptx", SaveFormat.Pptx);
}
The following code sample shows how to retrieve an audio frame with embedded audio and set its volume to 85%:
using (Presentation pres = new Presentation("AudioFrameEmbed_out.pptx"))
{
// Gets an audio frame shape
IAudioFrame audioFrame = (IAudioFrame)pres.Slides[0].Shapes[0];
// Sets the audio volume to 85%
audioFrame.VolumeValue = 85f;
pres.Save("AudioFrameValue_out.pptx", SaveFormat.Pptx);
}
Extract Audio
Aspose.Slides for .NET 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 C# code shows you how to extract the audio used in a slide:
string presName = "AudioSlide.pptx";
// Instantiates a Presentation class that represents a presentation file
Presentation pres = new Presentation(presName);
// Accesses the slide
ISlide slide = pres.Slides[0];
// Gets the slideshow transition effects for the slide
ISlideShowTransition transition = slide.SlideShowTransition;
//Extracts the sound in byte array
byte[] audio = transition.Sound.BinaryData;
System.Console.WriteLine("Length: " + audio.Length);