5

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.

Village
  • 13,603
  • 23
  • 116
  • 219
  • (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 07:51
  • @Aditya is this no longer supported in ConTeXt? I can't find any solution that works. – Village Aug 31 '20 at 00:19
  • What is the format of the video file? Does using \externalfigure[video] play the video? Which viewer are you using? – Aditya Aug 31 '20 at 11:31
  • @Aditya yes, I have updated the question with details of each attempt. – Village Sep 01 '20 at 00:20
  • This means that context is unable to locate the file. Run the file with tracking enabled (see context wiki ), which will tell you which files are being searched and why context cannot find the file. – Aditya Sep 02 '20 at 02:18
  • @Aditya okay I added tracking. It is finding the file when I change to a MOV file, but just results in an empty clickable square. I see no errors listed about it not findnig that file. – Village Sep 02 '20 at 23:33
  • Which pdfviewer are you using. Does clicking the square play the video in Adobe Acrobat? – Aditya Sep 03 '20 at 03:28
  • @Aditya It also looks like a bug in current context. I posted a question on the mailing list: https://mailman.ntg.nl/pipermail/ntg-context/2020/099312.html – AlexG Sep 03 '20 at 05:22
  • @Aditya : Another example that doesn't work in TeXLive-2020, but works in https://live.contextgarden.net/: \starttext hello! \immediate\pdfextension obj stream file {\jobname.tex} \stoptext. – AlexG Sep 03 '20 at 05:28
  • @Aditya I am using Adobe Acrobat Reader DC to test this on the Windows machine, though compiling on another Linux machine. I also tried opening in Microsoft Edge and other browser readers. Clicking the square with an MOV there does not play, it just shows a white outline around the 20 cm x 20 cm area. – Village Sep 03 '20 at 17:28

1 Answers1

9

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
AlexG
  • 54,894
  • If I compile this in 2020, I get this error: /formats/luatex/lpdf-ini-macro.lua:892: pdf.obj(): no "string" or "file" given. – Village Aug 30 '20 at 20:50
  • It also prints: mtx-context | fatal error: return code: 256. – Village Aug 30 '20 at 20:50
  • @Village There has been done fundamental changes to context. It doesn't allow for low-level luatex commands anymore. Strange piece of software. – AlexG Aug 30 '20 at 20:52
  • So does it no longer support video? I can't find anything in the documentation, except \externalfigure mentioning it can support MOV filetype, which I think I can make an audio into a MOV, but I tried using \externalfigure[clip.mov] and it compiles without error, but make a blank page, and the file size is too small to suggest the video was added to the PDF. – Village Aug 30 '20 at 22:47
  • @Village These changes (preventing TeX/LuaTeX low-level commands) are independent from functionality, such as media inclusion. One just has to find a way to implement things differently, perhaps using Lua scripting. – AlexG Aug 31 '20 at 07:27
  • Maybe there is a workaround - using context filter module (actually, @Aditya is its author), which can be set to auxiliary latex compilations - that way you would get pdf "figure with video" that could be then included into context (well, if thats posible) ... Other option might be to download context-standalone, which uses last years LuaTeX. – Tomáš Kruliš Aug 31 '20 at 11:34
  • @Village Current compilation failure is a confirmed bug that is fixed in the upcoming context version: https://mailman.ntg.nl/pipermail/ntg-context/2020/099326.html – AlexG Sep 07 '20 at 08:25
  • @AlexG After the bug is fixed, is it this method you posted that I should use, or some other version, such as \externalfigure[sample.mp3]? How do I know when the new version is ready? I updated my ConTeXt standalone today, so should it be working, or it will be pushed out in a later date? – Village Sep 07 '20 at 14:22
  • Actually, I don't know when this fix will be available in the major distributions (TeXLive, MiKTeX). The fix just makes the method in my answer work again. – AlexG Sep 07 '20 at 14:31
  • @AlexG I'm not using TexLive or MikTeX, but downloaded ConTeXt standalone with rsync. – Village Sep 07 '20 at 14:38
  • Ok, you've said this before, sorry. So, did you test the code whether it compiles again? – AlexG Sep 07 '20 at 14:43