4

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?

  • (Untested, since I don't have Windows, and Acrobat Reader on Linux hasn't been updated in a while). Doesn't \externalfigure[file.mp3] works? – Aditya Nov 10 '14 at 7:51
5

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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\protected\def\pdfobj{\pdfextension obj }
\def\pdflastobj{\numexpr\pdffeedback lastobj\relax}
\protected\def\pdfstartlink{\pdfextension startlink }
\protected\def\pdfendlink{\pdfextension endlink\relax}
\let\pdfxform\saveboxresource
\let\pdflastxform\lastsavedboxresourceindex
\define[3]\embedaudio{%
  \ifdefined\aplayer\else%
    \immediate\pdfobj stream file {APlayer.swf}%
    \immediate\pdfobj{<<
      /Type/Filespec/F (APlayer.swf)/UF (APlayer.swf)
      /EF <</F \the\pdflastobj\space 0 R>>
    >>}%
    \edef\aplayer{\the\pdflastobj}%
  \fi%
  %
  \immediate\pdfobj stream file {#2}%
  \immediate\pdfobj{<<
    /Type/Filespec/F (#2)/UF (#2)
    /EF <</F \the\pdflastobj\space 0 R>>
  >>}%
  \edef\audio{(#2) \the\pdflastobj\space 0 R}%
  %
  \immediate\pdfobj{<<
    /Type/RichMediaInstance/Subtype/Sound
    /Asset \aplayer\space 0 R
    /Params <</Binding/Foreground/FlashVars (autoPlay=true&source=#2&#1)>>
  >>}%
  %
  \immediate\pdfobj{<<
    /Type/RichMediaConfiguration/Subtype/Sound
    /Instances [\the\pdflastobj\space 0 R]
  >>}%
  %
  \immediate\pdfobj{<<
    /Type/RichMediaContent
    /Assets <<
      /Names [(APlayer.swf) \aplayer\space 0 R \audio]
    >>
    /Configurations [\the\pdflastobj\space 0 R]
  >>}%
  \edef\rmcontent{\the\pdflastobj}%
  %
  \setbox0=\hbox{ }\immediate\pdfxform 0%
  \pdfstartlink user{
    /Subtype/RichMedia
    /BS <</W 0/S/S>>
    /Contents (audio file:#2)
    /NM (rma:#2)
    /AP <</N \the\pdflastxform\space 0 R>>
    /RichMediaSettings <<
      /Activation <<
        /Condition/XA
        /Presentation <<
          /Transparent true
          /Style/Embedded
          /PassContextClick false
        >>
      >>
      /Deactivation <</Condition/PC>>
    >>
    /RichMediaContent \rmcontent\space 0 R
  }#3\pdfendlink%
}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.