我正在使用PowerPoint 2007 VSTO加载项,我正在运行一个小问题.加载项使用以下代码将声音添加到当前幻灯片: var shape = slide.Shapes.AddMediaObject(soundFileLocation, 50, 50, 20, 20); 生成的形状确实有声
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以用于先前创建的形状…
如果这不可行,我会感到惊讶,但我似乎无法找到 – 任何帮助都会非常感激!
抱歉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
