代码之家  ›  专栏  ›  技术社区  ›  Mathias

在PowerPoint中以编程方式播放形状的声音

  •  5
  • Mathias  · 技术社区  · 15 年前

    我正在开发PowerPoint2007VSTO加载项,我正在运行一个小问题。加载项使用以下代码向当前幻灯片添加声音:

    var shape = slide.Shapes.AddMediaObject(soundFileLocation, 50, 50, 20, 20);
    

    生成的形状确实有声音,可以通过PowerPoint幻灯片播放。我的问题是,如果引用了以这种方式创建的形状,我希望以编程方式播放声音,但找不到方法。我试过

    var soundEffect = shape.AnimationSettings.SoundEffect;
    soundEffect.Play();
    

    但这失败/崩溃了,当我检查SoundEffect时,它的类型是ppSoundNone。
    编辑: 取得了部分成功

    var shape = slide.Shapes.AddMediaObject(fileLocation, 50, 50, 20, 20);
    shape.AnimationSettings.SoundEffect.ImportFromFile(fileLocation);
    

    这样做可以让我播放声音:

    var animationSettings = shape.AnimationSettings;
    var soundEffect = shape.AnimationSettings.SoundEffect;
    soundEffect.Play();
    

    但是有一个主要问题;这只适用于添加的最后一个形状。出于某种原因,shape.animationsettings.soundeffect.importfromfile(filelocation)似乎将先前创建的形状的soundeffect属性重置为ppsoundnone…

    如果这样做不可行,我会很惊讶,但我似乎找不到方法——任何帮助都会非常感谢!

    1 回复  |  直到 15 年前
        1
  •  4
  •   Todd Main    15 年前

    对不起,这是vba,但它可以很容易地移植到c。以下是有效的方法:

    Sub addsound1()
        Dim ap As Presentation : Set ap = ActivePresentation
        Dim sl As Slide : Set sl = ap.Slides(1)
        Dim audioShape As Shape
        soundFileLocation = "C:\droid_scan.wav"
        Set audioShape = sl.Shapes.AddShape(msoShapeActionButtonSound, 100, 100, 50, 50)
        With audioShape.ActionSettings(ppMouseClick)
            .Action = ppActionNone
            .SoundEffect.ImportFromFile soundFileLocation
        End With
    End Sub
    Sub addsound2()
        Dim ap As Presentation : Set ap = ActivePresentation
        Dim sl As Slide : Set sl = ap.Slides(1)
        Dim audioShape As Shape
        soundFileLocation = "C:\droid_scan2.wav"
        Set audioShape = sl.Shapes.AddShape(msoShapeActionButtonSound, 50, 50, 50, 50)
        With audioShape.ActionSettings(ppMouseClick)
            .Action = ppActionNone
            .SoundEffect.ImportFromFile soundFileLocation
        End With
    End Sub
    
    Sub PlaySound1()
        Dim sh As Shape
        Set sh = ActivePresentation.Slides(1).Shapes(1)
        sh.ActionSettings(ppMouseClick).SoundEffect.Play
    End Sub
    
    Sub PlaySound2()
        Dim sh As Shape
        Set sh = ActivePresentation.Slides(1).Shapes(2)
        sh.ActionSettings(ppMouseClick).SoundEffect.Play
    End Sub