OfficeTips Home || VBA Section || General Section || Download Section || Privacy Policy Bookmark and Share

Control the media player

 

The media player in PowerPoint can be displayed during the slide show or during the authoring experience when you click on a media object based on the settings you chose. You can also control them programmatically via the object model in either view.

The example below illustrates how this can be done. One thing to note is that the slide with the media shape must be the active slide for the code to work. If the slide is not active you are bound to run into a variety of errors. You can test out the code by inserting a media object on the slide and then running PlayMediaInDocument or PlayMediaInSlideShow.

The media player object has several option to control the player including support to jump to previous/next bookmarks.

Supported versions: PowerPoint 2013 or later

 

 

' --------------------------------------------------------------------------------
' Copyright ©1999-2018, Shyam Pillai, All Rights Reserved.
' --------------------------------------------------------------------------------
' You are free to use this code within your own applications, add-ins,
' documents etc but you are expressly forbidden from selling or 
' otherwise distributing this source code without prior consent.
' This includes both posting free demo projects made from this
' code as well as reproducing the code in text or html format.
' --------------------------------------------------------------------------------
Option Explicit
Private shapeId As Integer
Private mediaPlayer As Player

Function MediaPlayerRefInDocumentWindow(Index As Long) As Boolean
 
    shapeId = GetMediaShapeId(Index)
    
    If shapeId > 0 Then
        ActiveWindow.View.GotoSlide Index
        ActiveWindow.ViewType = ppViewNormal
        ActiveWindow.Panes(2).Activate
        
        Set mediaPlayer = ActiveWindow.View.Player(shapeId)
        MediaPlayerRefInDocumentWindow = True
    Else
        MsgBox "No media shape found on the slide."
    End If
End Function

Function MediaPlayerRefInSlideShowWindow(Index As Long) As Boolean
    shapeId = GetMediaShapeId(Index)
    If shapeId > 0 Then
        Set mediaPlayer = SlideShowWindows(1).View.Player(shapeId)
        MediaPlayerRefInSlideShowWindow = True
    Else
        MsgBox "No media shape found on the slide."
    End If
 
End Function

Function GetMediaShapeId(Index As Long) As Long
    Dim shp As Shape
    
    For Each shp In ActivePresentation.Slides(Index).Shapes
        If shp.Type = msoMedia Then
            GetMediaShapeId = shp.Id
            Exit Function
        End If
    Next shp
    
    GetMediaShapeId = -1
End Function

Sub PlayMediaInDocument()

If MediaPlayerRefInDocumentWindow(ActiveWindow.Selection.SlideRange(1).SlideIndex) Then
    If mediaPlayer.State = ppStopped Or mediaPlayer.State = ppPaused Then
        mediaPlayer.Play
    End If
End If

End Sub

Sub PlayMediaInSlideShow()

If MediaPlayerRefInSlideShowWindow(SlideShowWindows(1).View.CurrentShowPosition) Then
    If mediaPlayer.State = ppStopped Or mediaPlayer.State = ppPaused Then
        mediaPlayer.Play
    End If
End If

End Sub
 

		

 

 

Copyright 1999-2018 (c) Shyam Pillai. All rights reserved.