编程播放PowerPoint中的形状的声音 [英] Programmatically play the sound of a shape in PowerPoint
问题描述
我工作的一个PowerPoint 2007 VSTO插件,和我在一个小问题上运行。该加载项使用以下code添加声音到当前幻灯片:
I am working on a PowerPoint 2007 VSTO add-in, and I am running in a small issue. The add-in adds sounds to the current slide using the following code:
var shape = slide.Shapes.AddMediaObject(soundFileLocation, 50, 50, 20, 20);
将所得的形状确实有一个声音,并且可以通过PowerPoint幻灯片播放。我的问题是,考虑到已创建这样一个形状的参考,我想以编程方式播放声音,但我找不到这样做的方式。我试过
The resulting shape does have a sound, and can be played through the PowerPoint slide. My problem is that, given a reference to a shape that has been created that way, I would like to programmatically play the sound, but I can't find the way to do it. I tried
var soundEffect = shape.AnimationSettings.SoundEffect;
soundEffect.Play();
但这种失败/崩溃,而当我检查SoundEffect中,它的类型是ppSoundNone。结果
修改有一些部分的成功
var shape = slide.Shapes.AddMediaObject(fileLocation, 50, 50, 20, 20);
shape.AnimationSettings.SoundEffect.ImportFromFile(fileLocation);
这样做可以让我玩了声:
Doing this allows me to play the sound with:
var animationSettings = shape.AnimationSettings;
var soundEffect = shape.AnimationSettings.SoundEffect;
soundEffect.Play();
但是有一个重大问题;这个作品只在最后加形状。出于某种原因,shape.AnimationSettings.SoundEffect.ImportFromFile(fileLocation)似乎在SoundEffect中属性重置为ppSoundNone为pviously创建的形状$ P $ ...
However there is one major issue; this works only for the last shape added. For some reason, shape.AnimationSettings.SoundEffect.ImportFromFile(fileLocation) seems to reset the SoundEffect property to ppSoundNone for the shapes previously created...
我会感到惊讶,如果这是不可行的,但我似乎无法找到如何 - !任何帮助将非常AP preciated
I would be surprised if this wasn't feasible, but I can't seem to find how - any help would be much appreciated!
推荐答案
对不起,VBA,但它可以很容易地移植到C#。以下是将工作:
Sorry for the VBA, but it can easily be ported to C#. Here's what will work:
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
这篇关于编程播放PowerPoint中的形状的声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!