How to Add Audio and Video Files to PowerPoint Slides in Java
E-iceblue Product Family
Posted on November 18, 2020
When slides are used for advertising campaign or product demonstration, people usually add media files (such as audio and video) to make the presentation more vivid and dynamic. In this article, I am going to show you how to insert audio and video files to your PowerPoint presentation by using Free Spire.Presentation for Java.
Add Spire.Presentation.jar as dependency
Method 1: Download Free Spire.Presentation for Java pack, unzip it and you’ll get Spire.Presentation.jar file from the “lib” folder. Import the jar file in your project as a dependency.
Method 2: If you are creating a Maven project, you can easily add the jar dependency by adding the following configurations to the pom.xml.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.presentation.free</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
Example 1. Add audio to slide
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class InsertAudio {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation presentation = new Presentation();
//Load a sample PowerPoint document
presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\example.pptx");
//Add a shape to the first slide
Rectangle2D.Double labelRect= new Rectangle2D.Double(50, 120, 120, 30);
IAutoShape labelShape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, labelRect);
labelShape.getLine().setFillType(FillFormatType.NONE);
labelShape.getFill().setFillType(FillFormatType.NONE);
labelShape.getTextFrame().setText("Double Click to Play Audio:");
labelShape.getTextFrame().getTextRange().setFontHeight(20);
labelShape.getTextFrame().getTextRange().setLatinFont(new TextFont("Times New Roman"));
labelShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
labelShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK);
//Add an audio file to the slide
Rectangle2D.Double audioRect = new Rectangle2D.Double(175, 120, 30, 30);
IAudio audio = presentation.getSlides().get(0).getShapes().appendAudioMedia((new java.io.File("C:\\Users\\Administrator\\Desktop\\music.wav")).getAbsolutePath(), audioRect);
audio.setPlayMode(AudioPlayMode.ON_CLICK);
//Save to file
presentation.saveToFile("AddAudio.pptx", FileFormat.PPTX_2013);
presentation.dispose();
}
}
Example 2. Add video to slide
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
public class InsertVideo {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation presentation = new Presentation();
//Load a sample PowerPoint file
presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\example.pptx");
//Add a shape to the first slide
Rectangle2D.Double labelRect = new Rectangle2D.Double(50, 120, 120, 50);
IAutoShape labelShape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, labelRect);
labelShape.getLine().setFillType(FillFormatType.NONE);
labelShape.getFill().setFillType(FillFormatType.NONE);
labelShape.getTextFrame().setText("Play Video:");
labelShape.getTextFrame().getTextRange().setFontHeight(20);
labelShape.getTextFrame().getTextRange().setLatinFont(new TextFont("Times New Roman"));
labelShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
labelShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK);
//Append a video file to the slide and set the cover image
Rectangle2D.Double videoRect = new Rectangle2D.Double(175, 120, 400, 225);
IVideo video = presentation.getSlides().get(0).getShapes().appendVideoMedia((new java.io.File("C:\\Users\\Administrator\\Desktop\\video.mp4")).getAbsolutePath(), videoRect);
BufferedImage coverImage = ImageIO.read( new File("C:\\Users\\Administrator\\Desktop\\coverImage.jpg"));
video.getPictureFill().getPicture().setEmbedImage(presentation.getImages().append(coverImage));
//Save to file
presentation.saveToFile("AddVideo.pptx", FileFormat.PPTX_2010);
presentation.dispose();
}
}
Posted on November 18, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.