单击停止播放声音媒体

Stop playing sound media on click

我想使用 PowerPoint VBA (Microsoft 365 MSO) 向 PowerPoint 幻灯片添加两个声音形状,均在单击时触发。

幻灯片时间轴将是:

  1. 第一次点击开始播放声音
  2. 第二次点击停止第一个声音并开始第二个。

我可以放置声音形状并添加动画对象来触发声音。

我找不到效果对象 属性,它复制了 GUI 选项以在单击时停止播放。

该代码将添加一个新幻灯片,创建两个声音形状并让它们在点击时触发,但声音 1 不会停止播放。

Sub TestSoundTrigger()

    Dim slTestSoundSlide As Slide
    Dim shSoundShape1 As Shape
    Dim shSoundShape2 As Shape
    Dim efSoundShape1 As Effect
    Dim efSoundShape2 As Effect

' Create the slide
    Set slTestSoundSlide = ActivePresentation.Slides.AddSlide(ActivePresentation.Slides.Count + 1, ActivePresentation.Designs(1).SlideMaster.CustomLayouts(1))

' Add 2 sound shapes
    Set shSoundShape1 = slTestSoundSlide.Shapes.AddMediaObject2(ActivePresentation.Path & "\testsound1.mp3", True, False, 10, 10)
    Set shSoundShape2 = slTestSoundSlide.Shapes.AddMediaObject2(ActivePresentation.Path & "\testsound2.mp3", True, False, 10, 10)
    
' Add the 2 triggers to play the sounds on click in turn
    Set efSoundShape1 = slTestSoundSlide.TimeLine.MainSequence.AddEffect(shSoundShape1, effectId:=msoAnimEffectMediaPlay)
    Set efSoundShape2 = slTestSoundSlide.TimeLine.MainSequence.AddEffect(shSoundShape2, effectId:=msoAnimEffectMediaPlay)

End Sub

我已经检查了 Effect 和 Timeline 对象属性,但找不到这个。

我找到这个 属性:ppSoundStopPrevious ppSoundEffectType

或者 您可以设置另一个 msoAnimEffectMediaStop - 但这会创建另一个动画步骤

    Sub TestSoundTrigger()

    Dim slTestSoundSlide As Slide
    Dim shSoundShape1 As Shape
    Dim shSoundShape2 As Shape
    Dim efSoundShape1 As Effect
    Dim efSoundShape2 As Effect

' Create the slide
    Set slTestSoundSlide = ActivePresentation.Slides.AddSlide(ActivePresentation.Slides.Count + 1, ActivePresentation.Designs(1).SlideMaster.CustomLayouts(1))

' Add 2 sound shapes
    Set shSoundShape1 = slTestSoundSlide.Shapes.AddMediaObject2(ActivePresentation.Path & "\testsound1.mp3", True, False, 10, 10)
    Set shSoundShape2 = slTestSoundSlide.Shapes.AddMediaObject2(ActivePresentation.Path & "\testsound2.mp3", True, False, 10, 10)

' Add the 2 triggers to play the sounds on click in turn
    Set efSoundShape1 = slTestSoundSlide.TimeLine.MainSequence.AddEffect(shSoundShape1, effectId:=msoAnimEffectMediaPlay)
    Set efSoundShape1 = slTestSoundSlide.TimeLine.MainSequence.AddEffect(shSoundShape1, effectId:=msoAnimEffectMediaStop)
    Set efSoundShape2 = slTestSoundSlide.TimeLine.MainSequence.AddEffect(shSoundShape2, effectId:=msoAnimEffectMediaPlay)

End Sub