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

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 to make an audiovisual presentation with the GemBox.Presentation API.

Note: The 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.

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 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 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

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