TransWikia.com

How to embed audio in a PDF created with ConTeXt?

TeX - LaTeX Asked on March 16, 2021

How can I place an MP3 file in the page of a PDF, so that it can play the audio on a Windows machine with Adobe Acrobat Reader?

Update: August 31, 2020

The answer below no longer works in 2020. I tried this code:

starttext
    setupinteraction[state=start]
    externalfigure[sample.mp3]
stoptext

I placed an mp3, sample.mp3 in the same folder as the tex file. I compiled. No errors appeared.

When I view it, I get a grey square that says:

name: sample.mp3
file: sample.mp3
state: unknown

I tried in both Adobe Acrobat Reader DC and Microsoft Edge and the results were the same grey square.

Then I tried changing the code to not include the file extension, just externalfigure[sample and that prints this message three times:

see samples.tex

I searched my entire ConTeXt install directory and cannot find any file with this name to refer to, nor is it mentioned in the ConTeXt Garden, nor could I find anything in the mailing lists.

Update: September 2, 2020

I found that .mp3 is not supported. Looking here, I found mov is supported, but it says "NOTE: Movie inclusion only works in Adobe Acrobat but does not work on Linux". Note that I’m compiling in Linux, but viewing on a Windows work computer to test in Adobe. Could that be cause, or should the file compile in Linux fine and still be viewable in Adobe? When compiled, the resulting file is just empty, and also is 5 kb, while the video is 700 kb, so clearly the mov isn’t being placed inside the PDF.

starttext
    setupinteraction[state=start]
    enabletrackers[graphics.locating]
    externalfigure[sample.mov][width=10cm, height=10cm]
stoptext

This does, at least, create an invisible 10 cm x 10 cm clickable box appear in Adobe, as the mouse changes from a cursor to a hand when placed within the left middle of the document, but nothing plays or is heard.

Update: September 2, 2020, Update 2

I tried by converting an MOV to pdf, then embedding the PDF, but the converted created a PDF with an image on each slide instead of placing a video in a PDF that could be embedded into the ConTeXt-created PDF.

One Answer

The following example defines embedaudio. It implements the same specification for embedded media as the media9 LaTeX package. Audio files in the MP3 format are played back within the player component APlayer.swf shipping with media9. MP3 files specified as URL are first downloaded before embedding. Adobe Reader on the Windows or OSX platforms is required for playback.

See the media9 manual, Table 4 and Table 5 for how to configure APlayer via FlashVars and for how to use the keyboard to control playback. FlashVars are passed as the first argument of embedaudio.

A similar command for embedding video with ConTeXt is given here.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% embedaudio{<flashvars>}{<audio file | URL (MP3)>}{link text}
% flashvars documented in `texdoc media9`,  page 20
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
protecteddefpdfobj{pdfextension obj }
defpdflastobj{numexprpdffeedback lastobjrelax}
protecteddefpdfstartlink{pdfextension startlink }
protecteddefpdfendlink{pdfextension endlinkrelax}
letpdfxformsaveboxresource
letpdflastxformlastsavedboxresourceindex
define[3]embedaudio{%
  ifdefinedaplayerelse%
    immediatepdfobj stream file {APlayer.swf}%
    immediatepdfobj{<<
      /Type/Filespec/F (APlayer.swf)/UF (APlayer.swf)
      /EF <</F thepdflastobjspace 0 R>>
    >>}%
    edefaplayer{thepdflastobj}%
  fi%
  %
  immediatepdfobj stream file {#2}%
  immediatepdfobj{<<
    /Type/Filespec/F (#2)/UF (#2)
    /EF <</F thepdflastobjspace 0 R>>
  >>}%
  edefaudio{(#2) thepdflastobjspace 0 R}%
  %
  immediatepdfobj{<<
    /Type/RichMediaInstance/Subtype/Sound
    /Asset aplayerspace 0 R
    /Params <</Binding/Foreground/FlashVars (autoPlay=true&source=#2&#1)>>
  >>}%
  %
  immediatepdfobj{<<
    /Type/RichMediaConfiguration/Subtype/Sound
    /Instances [thepdflastobjspace 0 R]
  >>}%
  %
  immediatepdfobj{<<
    /Type/RichMediaContent
    /Assets <<
      /Names [(APlayer.swf) aplayerspace 0 R audio]
    >>
    /Configurations [thepdflastobjspace 0 R]
  >>}%
  edefrmcontent{thepdflastobj}%
  %
  setbox0=hbox{ }immediatepdfxform 0%
  pdfstartlink user{
    /Subtype/RichMedia
    /BS <</W 0/S/S>>
    /Contents (audio file:#2)
    /NM (rma:#2)
    /AP <</N thepdflastxformspace 0 R>>
    /RichMediaSettings <<
      /Activation <<
        /Condition/XA
        /Presentation <<
          /Transparent true
          /Style/Embedded
          /PassContextClick false
        >>
      >>
      /Deactivation <</Condition/PC>>
    >>
    /RichMediaContent rmcontentspace 0 R
  }#3pdfendlink%
}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

starttext
ConTeXt{} embedded audio example: embedaudio{}{click.mp3}{strut $rightarrow$Click and listen!}

ConTeXt{} audio-URL example: embedaudio{}{http://www.openbsd.org/songs/song49.mp3}{strut $rightarrow$Click and listen!}
stoptext

Correct answer by AlexG on March 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP