Insert audio in PowerPoint C#

In various cases, the presenters include sounds or audio clips in their presentations. MS PowerPoint provides audio frames to insert the sounds into the slides. In this article, you will learn how to automate the manipulation of audio frames in presentations. Particularly, the article will cover how to add audio frames in PowerPoint PPT in C#. In addition, we will demonstrate how to extract audio frames from PPT slides.

C# API to Add Audio Frames in PowerPoint PPT

To add or extract audio frames in PowerPoint PPT/PPTX, we will use Aspose.Slides for .NET. It is a class library that lets you automate the creation and manipulation of PowerPoint and OpenOffice presentations. You can either install the API via NuGet or download its DLL.

PM> Install-Package Aspose.Slides.NET

Add an Audio Frame in PowerPoint PPT in C#

The following are the steps to add audio into a PowerPoint PPT using C#.

The following code sample shows how to insert audio in a PowerPoint PPTX in C#.

// Load presentation
using (Presentation pres = new Presentation("AudioFrameEmbed_out.pptx"))
{
// Get the first slide
ISlide sld = pres.Slides[0];
// Load the wav sound file to stream
FileStream fstr = new FileStream("sampleaudio.wav", FileMode.Open, FileAccess.Read);
// Add audio frame
IAudioFrame audioFrame = sld.Shapes.AddAudioFrameEmbedded(50, 150, 100, 100, fstr);
// Change play mode to play on click
audioFrame.PlayMode = AudioPlayModePreset.OnClick;
// Set volume to Low
audioFrame.Volume = AudioVolumeMode.Low;
// Set audio to play across slides
audioFrame.PlayAcrossSlides = true;
// Set audio to not loop
audioFrame.PlayLoopMode = false;
// Hide AudioFrame during the slide show
audioFrame.HideAtShowing = true;
// Rewind audio to start after playing
audioFrame.RewindAudio = true;
// Save the PPTX file to disk
pres.Save("AudioFrameEmbed_changed.pptx", SaveFormat.Pptx);
}
view raw insert-audio.cs hosted with ❤ by GitHub

Extract Audio Frames in PowerPoint PPT in C#

You can also extract the audio frames from an existing PowerPoint PPT. The following are the steps to perform this operation.

The following code sample shows how to extract audio from a PowerPoint PPTX in C#.

string presName = "AudioSlide.pptx";
// Load presentation file
Presentation pres = new Presentation(presName);
// Access the desired slide
ISlide slide = pres.Slides[0];
// Get the slideshow transition effects for slide
ISlideShowTransition transition = slide.SlideShowTransition;
// Extract sound in byte array
byte[] audio = transition.Sound.BinaryData;
System.Console.WriteLine("Length: " + audio.Length);

C# PowerPoint API - Get a Free License

Use Aspose.Slides for .NET without evaluation limitations by getting a free temporary license.

Conclusion

In this article, you have learned how to insert audio in PowerPoint PPT using C#. Moreover, you have seen how to extract the audio clips from presentations programmatically. Besides, you can explore the documentation to learn more about Aspose.Slides for .NET. In addition, you can post your question to our forum.

See Also

Tip: If you ever need to extract the audio from a video clip, you can use Aspose MP4 to MP3 converter to do the job.