I want to add a prerecorded mp3 to a presentation slide and play it when the slide is shown. This is my code, but it does not play audio at all. How to use OpenXML and add audio files to a presentation?
public void AddVideoToPresentation(PresentationDocument presentationDocument, string audioFilePath)
{
int PicID = 915;
PresentationPart presentationPart = presentationDocument.PresentationPart;
var slideCount = presentationPart.Presentation.SlideIdList.Count();
var videoEmbedId = string.Format("audioId{0}{1}", slideCount, PicID++);
var mediaEmbedId = string.Format("medId{0}{1}", slideCount, PicID++);
SlidePart slidepart = presentationDocument.PresentationPart.SlideParts.LastOrDefault();
MediaDataPart mediaDataPart1 = presentationDocument.CreateMediaDataPart("audio/mp3", "mp3");
System.IO.Stream mediaDataPart1Stream = File.OpenRead(audioFilePath);
mediaDataPart1.FeedData(mediaDataPart1Stream);
mediaDataPart1Stream.Close();
slidepart.AddAudioReferenceRelationship(mediaDataPart1, videoEmbedId);
slidepart.AddMediaReferenceRelationship(mediaDataPart1, mediaEmbedId);
slidepart.Slide.Save();
}
This is rather complex as it deals with the animation timeline.
The way you usually figure this stuff out is to create a single slide deck with the single feature(s) you want (e.g. inserted audio and then in the Playback tab, set it to plays automatically). Then you put it in the OpenXML SDK Productivity Tool (get from https://github.com/dotnet/Open-XML-SDK). Then you add your simple deck and let the tool reflect your deck into C# code.
From there, you'll have to read through and decipher how to recreate what you need. For example:
One other point I wanted to make. You could create two decks, one with automatically play and one without. Then you can use both decks with the "compare files" button. This will help you better locate where you need to find the "play automatically" portion.