该操作示例介绍了如何使用 Java 在 Presentation 中添加视频,方法是公开配置环境的详细信息并使用示例代码。您可以在 Linux、Windows 或 macOS 等操作系统中配置的 Java 环境中使用此应用程序,不依赖于任何第三方软件或 Microsoft PowerPoint,以使用 Java 在 PPTX 中嵌入视频。
使用 Java 在演示文稿中插入视频的步骤
- 配置应用程序以使用来自存储库管理器的 Aspose.Slides for Java JAR 文件
- 使用 Presentation 的实例生成空演示文稿以在演示文稿中插入视频
- 加载演示幻灯片集合中的第一张幻灯片以添加视频帧
- 从磁盘访问视频文件并将其添加到演示幻灯片视频帧中
- 设置自动播放和音量控制选项的视频帧方法
- 将包含视频的演示文稿保存在磁盘上
通过遵循 Java 在 PowerPoint 中插入视频 演示文稿的上述步骤,可以使用非常简单的 API 接口轻松处理。首先,将创建一个使用 Presentation 类实例的默认演示文稿,然后访问演示文稿中的第一张幻灯片。在后续步骤中,源视频文件将被加载并插入所选幻灯片的视频帧中。最后,在将包含视频的演示文稿保存到磁盘之前,将设置视频音量控制和播放模式属性。
使用 Java 在演示文稿中插入视频的代码
import com.aspose.slides.AudioVolumeMode; | |
import com.aspose.slides.ISlide; | |
import com.aspose.slides.IVideo; | |
import com.aspose.slides.IVideoFrame; | |
import com.aspose.slides.License; | |
import com.aspose.slides.Presentation; | |
import com.aspose.slides.SaveFormat; | |
import com.aspose.slides.ShapeType; | |
import com.aspose.slides.VideoPlayModePreset; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
public class InsertVideo { | |
public static void main(String[] args) throws Exception{ | |
String filesPath = "/Users/Documents/KnowledgeBase/TestData/"; | |
//Set the license to insert a video inside the presentation | |
License slideVideolicense = new License(); | |
slideVideolicense.setLicense(filesPath + "Conholdate.Total.Product.Family.lic"); | |
//Create a new presentation to insert a video inside the slide | |
Presentation srcPresenWithVideo = new Presentation(); | |
//Load the first slide to include the video | |
ISlide VideoSlide = srcPresenWithVideo.getSlides().get_Item(0); | |
// Add the video inside the presentation video collection | |
IVideo vid = srcPresenWithVideo.getVideos().addVideo(new FileInputStream(new File(filesPath+ "SampleVideo.mp4"))); | |
// Insert a Video Frame inside the slide | |
IVideoFrame videoFrame = VideoSlide.getShapes().addVideoFrame(0,0,720,540,vid); | |
// Embed the video inside Video Frame | |
videoFrame.setEmbeddedVideo(vid); | |
// Set the playbaack and volume options of the video frame | |
videoFrame.setPlayMode(VideoPlayModePreset.Auto); | |
videoFrame.setVolume(AudioVolumeMode.Loud); | |
// Save the presentation with video frame on the disk | |
srcPresenWithVideo.save(filesPath + "VideoFrame_out.pptx", SaveFormat.Pptx); | |
} | |
} |
通过在 Java 中使用上面的示例将 MP4 视频保存在 PPT 演示文稿中,使用非常简单的 API 接口就可以轻松实现。我们使用了 IVideoFrame 类,它还允许您为播放模式、倒带模式、循环播放视频和隐藏视频等功能设置标志。您还可以通过提供绝对路径来链接视频,而无需将视频也实际嵌入到演示文稿中。一旦您将视频嵌入到演示文稿中,您可以将其保存在磁盘上或内存流中以用于基于 Web 的应用程序。
此示例包含如何使用 Java 在演示文稿中插入视频。如果您有兴趣了解如何在演示文稿中隐藏幻灯片,请参阅 如何使用 Java 隐藏演示文稿中的幻灯片 上的文章。