Audio and Video in PowerPoint using C# and VB.NET

Create Audio and Video

Very often, it's necessary to improve presentations and turn the slides more dynamic and interactive for the audience. One of the ways of making presentations more compelling is adding audio or video to play while the slideshow is running.

In the GemBox.Presentation API, you can add audio and video to your PowerPoint files in C# and VB.NET. Notice that the media-related types are grouped in the GemBox.Presentation.Media namespace.

The example below shows how you can create an audiovisual presentation using the GemBox.Presentation.

PowerPoint audio and video created with GemBox.Presentation
Screenshot of PowerPoint audio and video created with GemBox.Presentation
using System.IO;
using GemBox.Presentation;
using GemBox.Presentation.Media;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var presentation = new PresentationDocument();

        // Create new presentation slide.
        var slide = presentation.Slides.AddNew(SlideLayoutType.Custom);

        // Create and add audio content.
        AudioContent audio = null;
        using (var stream = File.OpenRead("%#Applause.wav%"))
            audio = slide.Content.AddAudio(AudioContentType.Wav, stream, 2, 2, LengthUnit.Centimeter);

        // Set the ending fade durations for the media.
        audio.Fade.End = TimeOffset.From(300, TimeOffsetUnit.Millisecond);

        // Get the picture associated with this media.
        var picture = audio.Picture;

        // Set drawing properties.
        picture.Action.Click.Set(ActionType.PlayMedia);
        picture.Layout.Width = Length.From(7, LengthUnit.Centimeter);
        picture.Layout.Height = Length.From(7, LengthUnit.Centimeter);
        picture.Name = "Applause.wav";

        // Create and add video content.
        VideoContent video = null;
        using (var stream = File.OpenRead("%#Wildlife.wmv%"))
            video = slide.Content.AddVideo("video/x-ms-wmv", stream, 10, 2, 10, 5.6, LengthUnit.Centimeter);

        // Set drawing properties.
        video.Picture.Action.Click.Set(ActionType.PlayMedia);
        video.Picture.Name = "Wildlife.wmv";

        // Set the amount of time to be trimmed from the start and end of the media.
        video.Trim.Start = TimeOffset.From(600, TimeOffsetUnit.Millisecond);
        video.Trim.End = TimeOffset.From(800, TimeOffsetUnit.Millisecond);

        // Set the starting and ending fade durations for the media.
        video.Fade.Start = TimeOffset.From(100, TimeOffsetUnit.Millisecond);
        video.Fade.End = TimeOffset.From(200, TimeOffsetUnit.Millisecond);

        // Add video bookmarks.
        video.Bookmarks.Add(TimeOffset.From(1500, TimeOffsetUnit.Millisecond));
        video.Bookmarks.Add(TimeOffset.From(3000, TimeOffsetUnit.Millisecond));

        presentation.Save("Audio and Video.pptx");
    }
}
Imports System.IO
Imports GemBox.Presentation
Imports GemBox.Presentation.Media

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim presentation = New PresentationDocument

        ' Create New presentation slide.
        Dim slide = presentation.Slides.AddNew(SlideLayoutType.Custom)

        ' Create and add audio content.
        Dim audio As AudioContent = Nothing
        Using stream As Stream = File.OpenRead("%#Applause.wav%")
            audio = slide.Content.AddAudio(AudioContentType.Wav, stream, 2, 2, LengthUnit.Centimeter)
        End Using

        ' Set the ending fade durations for the media.
        audio.Fade.End = TimeOffset.From(300, TimeOffsetUnit.Millisecond)

        ' Get the picture associated with this media.
        Dim picture = audio.Picture

        ' Set drawing properties.
        picture.Action.Click.Set(ActionType.PlayMedia)
        picture.Layout.Width = Length.From(7, LengthUnit.Centimeter)
        picture.Layout.Height = Length.From(7, LengthUnit.Centimeter)
        picture.Name = "Applause.wav"

        ' Create and add video content.
        Dim video As VideoContent = Nothing
        Using stream As Stream = File.OpenRead("%#Wildlife.wmv%")
            video = slide.Content.AddVideo("video/x-ms-wmv", stream, 10, 2, 10, 5.6, LengthUnit.Centimeter)
        End Using

        ' Set drawing properties.
        video.Picture.Action.Click.Set(ActionType.PlayMedia)
        video.Picture.Name = "Wildlife.wmv"

        ' Set the amount of time to be trimmed from the start And end of the media.
        video.Trim.Start = TimeOffset.From(600, TimeOffsetUnit.Millisecond)
        video.Trim.End = TimeOffset.From(800, TimeOffsetUnit.Millisecond)

        ' Set the starting And ending fade durations for the media.
        video.Fade.Start = TimeOffset.From(100, TimeOffsetUnit.Millisecond)
        video.Fade.End = TimeOffset.From(200, TimeOffsetUnit.Millisecond)

        ' Add video bookmarks.
        video.Bookmarks.Add(TimeOffset.From(1500, TimeOffsetUnit.Millisecond))
        video.Bookmarks.Add(TimeOffset.From(3000, TimeOffsetUnit.Millisecond))

        presentation.Save("Audio and Video.pptx")
    End Sub
End Module

Note that playing of media created with the GemBox.Presentation API currently works only on MS PowerPoint 2013. In subsequent updates, we will add media-playing support for older versions of MS PowerPoint.

Export Audio and Video

The following example shows how you can export audio and video from PowerPoint files.

PowerPoint audio and video exported with GemBox.Presentation
Screenshot of PowerPoint audio and video exported with GemBox.Presentation
Upload your file (Drag file here)
using System.Linq;
using System.IO;
using System.IO.Compression;
using GemBox.Presentation;
using GemBox.Presentation.Media;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var presentation = PresentationDocument.Load("%InputFileName%");
        var slide = presentation.Slides[0];

        // Get audios from first slide.
        var audios = slide.Content.Drawings.All()
            .OfType<Picture>()
            .Where(p => p.Media?.MediaType == MediaType.Audio)
            .Select(p => p.Media as AudioContent);

        // Get videos from first slide.
        var videos = slide.Content.Drawings.All()
            .OfType<Picture>()
            .Where(p => p.Media?.MediaType == MediaType.Video)
            .Select(p => p.Media as VideoContent);

        // Create a ZIP file for storing audio and video files.
        using (var archiveStream = File.OpenWrite("Output.zip"))
        using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create))
        {
            int counter = 0;
            foreach (var audio in audios)
            {
                string extension = audio.Content.ContentType.Replace("audio/", string.Empty);
                var entry = archive.CreateEntry($"Audio {++counter}.{extension}");

                // Export audio from PowerPoint file to the ZIP entry.
                using (var entryStream = entry.Open())
                using (var audioStream = audio.Content.Open())
                    audioStream.CopyTo(entryStream);
            }

            counter = 0;
            foreach (var video in videos)
            {
                string extension = video.Content.ContentType.Replace("video/", string.Empty);
                var entry = archive.CreateEntry($"Video {++counter}.{extension}");

                // Export video from PowerPoint file to the ZIP entry.
                using (var entryStream = entry.Open())
                using (var videoStream = video.Content.Open())
                    videoStream.CopyTo(entryStream);
            }
        }
    }
}
Imports System.Linq
Imports System.IO
Imports System.IO.Compression
Imports GemBox.Presentation
Imports GemBox.Presentation.Media

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim presentation = PresentationDocument.Load("%InputFileName%")
        Dim slide = presentation.Slides(0)

        ' Get audios from first slide.
        Dim audios = slide.Content.Drawings.All() _
            .OfType(Of Picture)() _
            .Where(Function(p) p.Media?.MediaType = MediaType.Audio) _
            .Select(Function(p) TryCast(p.Media, AudioContent))

        ' Get videos from first slide.
        Dim videos = slide.Content.Drawings.All() _
            .OfType(Of Picture)() _
            .Where(Function(p) p.Media?.MediaType = MediaType.Video) _
            .Select(Function(p) TryCast(p.Media, VideoContent))

        ' Create a ZIP file for storing audio and video files.
        Using archiveStream = File.OpenWrite("Output.zip")
            Using archive = New ZipArchive(archiveStream, ZipArchiveMode.Create)

                Dim counter As Integer = 0
                For Each audio In audios
                    counter += 1
                    Dim extension As String = audio.Content.ContentType.Replace("audio/", String.Empty)
                    Dim entry = archive.CreateEntry($"Audio {counter}.{extension}")

                    ' Export audio from PowerPoint file to the ZIP entry.
                    Using entryStream = entry.Open()
                        Using audioStream = audio.Content.Open()
                            audioStream.CopyTo(entryStream)
                        End Using
                    End Using
                Next

                counter = 0
                For Each video In videos
                    counter += 1
                    Dim extension As String = video.Content.ContentType.Replace("video/", String.Empty)
                    Dim entry = archive.CreateEntry($"Video {counter}.{extension}")

                    ' Export video from PowerPoint file to the ZIP entry.
                    Using entryStream = entry.Open()
                        Using videoStream = video.Content.Open()
                            videoStream.CopyTo(entryStream)
                        End Using
                    End Using
                Next
            End Using
        End Using
    End Sub
End Module

See also


Next steps

GemBox.Presentation is a .NET component that enables you to read, write, edit, convert, and print presentation files from your .NET applications using one simple API.

Download Buy