Audio and Video in PowerPoint

Create Audio and Video

The example below shows how to use the GemBox.Presentation API to add audio and video to your PowerPoint files in C# and VB.NET.

using GemBox.Presentation;
using GemBox.Presentation.Media;
using System.IO;

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 GemBox.Presentation
Imports GemBox.Presentation.Media
Imports System.IO

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
PowerPoint audio and video created with GemBox.Presentation
Screenshot of PowerPoint audio and video created with GemBox.Presentation

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.

Upload your file (Drag file here)
using GemBox.Presentation;
using GemBox.Presentation.Media;
using System.IO;
using System.IO.Compression;
using System.Linq;

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 GemBox.Presentation
Imports GemBox.Presentation.Media
Imports System.IO
Imports System.IO.Compression
Imports System.Linq

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
PowerPoint audio and video exported with GemBox.Presentation
Screenshot of PowerPoint audio and video exported with GemBox.Presentation

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