Freigeben über


Playing Invisible Sound Clips Revisited

So, it turns out there’s still more to report on how to programmatically control your media objects in PowerPoint. And it’s good news, so read on.

I originally wrote about how to have a sound clip play automatically when a slide is loaded, without having the sound file icon visible. I used the AnimationSettings.PlaySettings object, which works fine for PowerPoint 2000 and below. But it’s the exact wrong thing to do with PowerPoint 2002 and 2003, as it may lead to some of your other animation effects being deleted from the slide. Then I showed how you could do the same thing using the TimeLine object. Unfortunately, because of a flaw in the object model, when you access the PlaySettings object through the TimeLine object, you still might end up deleting effects unless you’re very careful.

Now, thanks to PowerPoint’s wonderful development team, I’ve learned that you can actually set a media object to play, pause, and stop, all without accessing the PlaySettings object. This presents a nice work-around that prevents you from accidentally deleting animations from your slide. Here’s how it works:

Turns out there are three effect types that control the playing of a sound or movie file: MsoAnimEffectMediaPause, MsoAnimEffectMediaPlay, and MsoAnimEffectMediaStop. By assigning Effect objects of these effect types to a media object, you can control when the audio or video file plays during your animation sequence, without accessing the PlaySettings object.

So, going back to our example from those previous entries, here’s how you would insert a sound file, move it off the viewable slide area, and create an Effect object that would play the file as soon as the slide loads:

 

Sub AddAudioOffSlide()

Dim shpSound As Shape

 

With ActivePresentation.Slides(1)

    'Add sound file

    With .Shapes

        Set shpSound = .AddMediaObject _

            (FileName:="C:\soundfiles\bobmould\black sheets of rain.mp3")

        'Position sound file off viewable slide area

        With shpSound

            .Left = 0 - .Width

            .Top = 0 - .Height

        End With

    End With

   

    With .TimeLine.MainSequence

        'Add effect to play sound file

        'As first effect in main sequence

        'set effect to perform when slide is loaded

        .AddEffect Shape:=shpSound, _

            effectId:=msoAnimEffectMediaPlay, _

            trigger:=msoAnimTriggerWithPrevious, _

            Index:=1

           

    End With

   

End With

Now obviously, with the ability to play, pause, and stop media files, you create much more complex animation sequences than the above example.

One interesting thing worth mentioning: the MsoAnimEffectMediaPause effect type actually functions to toggle whether the media object is playing or not. That is, if the media file is playing, assigning an MsoAnimEffectMediaPause effect to the file pauses it; but if the file is already paused, assigning another MsoAnimEffectMediaPause effect to the file causes the file to resume playing from the point at which it was pause. Conversely, an MsoAnimEffectMediaPlay effect always starts the file playing from the beginning of the file, whether or not the file is currently paused at a certain point in the file.

The following example adds effects for a video file to a slide’s main animation sequence. When the user clicks the page, the video file plays for 10 seconds, then pauses for 10 seconds, then starts playing for another 10 seconds before it stops. To accomplish this, the code adds four effects to the sequence. The first starts the file playing; the second pauses the file; the third starts the file playing again; and the final effect stops the file playing. The first effect is triggered when the user clicks the page; the other three are set to be triggered with the previous effect. The code sets the trigger delay time for each effect is set to different durations; otherwise, PowerPoint would perform all the effects simultaneously.

Note that the third effect, the one that resumes the file playing from the point where it was paused, is of type MsoAnimEffectMediaPause rather than type MsoAnimEffectMediaPlay.

 

Dim shpVideo As Shape

Dim effPause As Effect

Dim effPlayAgain As Effect

Dim effStop As Effect

 

With ActivePresentation.Slides(1)

    Set shpVideo = .Shapes("demovideo")

   

    With .TimeLine.MainSequence

        .AddEffect Shape:=shpVideo, _

            effectId:=msoAnimEffectMediaPlay, _

            trigger:=msoAnimTriggerOnPageClick

        Set effPause = .AddEffect(Shape:=shpVideo, _

            effectId:=msoAnimEffectMediaPause, _

            trigger:=msoAnimTriggerWithPrevious)

        effPause.Timing.TriggerDelayTime = 10

        Set effPlayAgain = .AddEffect(Shape:=shpVideo, _

            effectId:=msoAnimEffectMediaPause, _

                trigger:=msoAnimTriggerWithPrevious)

        effPlayAgain.Timing.TriggerDelayTime = 20

        Set effStop = .AddEffect(Shape:=shpVideo, _

            effectId:=msoAnimEffectMediaStop, _

                trigger:=msoAnimTriggerWithPrevious)

        effStop.Timing.TriggerDelayTime = 30

    End With

End With

For more information about inserting media objects to a presentation, you can always refer to my article Adding Multimedia to a PowerPoint 2003 Presentation.

I’m working on several articles dealing with how to create and customize animation sequences in PowerPoint 2002 and above. With any luck they’ll be published in a few weeks. Of course, I’ll post the links here once those articles go live.

Many thanks again to the PowerPoint development team for their willingness to bring me up to speed and (gently) correct my mistakes.

Comments