[Tex/LaTex] Playing portions of MP3 in PDF

audiomedia9

I'm working on a book which should allow the playing of MP3s at selected points. I've found a good solution for the general case (i.e. the entire file): Audio examples in phonetics project, however, I also want to play portions of the files as well (e.g. words or phrases within sentences).

One solution is to use audio software to split the file into the necessary sections, but this of course produces a lot of unnecessary duplication.

The documentation for media9 mentions seek, which is half a solution (though I confess it isn't clear to me how to make this work). Hunting through JavaScript documentation has revealed this code, which suggests it is possible:

var player = app.media.openPlayer({
  rendition: this.media.getRendition( "myAudio" ),
  doc: this,
  settings: {
  startAt: 3,
  endAt: 8
  }
});

The MWE given in the link above is as follows:

\documentclass{article}
\usepackage{media9,graphicx}

\begin{document}
Sound sample: \includemedia[
  addresource=sound_sample.mp3,
  transparent,
  flashvars={
    source=sound_sample.mp3
   &autoPlay=true
  },
]{\includegraphics[height=1.44ex]{speaker.png}}{APlayer.swf}
\end{document}

I'd be grateful for any suggestions on how to proceed.

Best Answer

Version 2014/06/25 of media9 has updated players APlayer.swf and VPlayer.swf.

The play and pause methods of their programming interface now accept optional arguments which are time offsets, measured in seconds, into the media file. Thus, a properly configured \mediabutton lets the embedded player play a portion of the media file.

A generic sound example with two buttons which is similarly useable for video:

\includemedia[
  label=mysound,
  activate=pageopen,
  addresource=soundfile.mp3,
  flashvars={source=soundfile.mp3},
  transparent
]{\fbox{Play entire file}}{APlayer.swf}

\mediabutton[
  mediacommand=mysound:play [(10.0)],
  mediacommand=mysound:pause [(33.0)]
]{\fbox{Play from 10 s until 33 s}}

\mediabutton[
  mediacommand=mysound:play [(41.0)],
  mediacommand=mysound:pause [(63.0)]
]{\fbox{Play from 41 s until 1:03 min}}

In the particular case of a phonetics project, the same sound file may need to be playable on different pages of the document. Here we need to embed the audio player at least once per sound file and per page on which the player is referenced by media buttons. For this to do automatically, the code example below defines the command

\playSoundFromTo{<mp3 file path or URL>}{<tStart>}{<tEnd>}{<button text>}

Note, that a particular sound file and the player SWF are only once embedded physically into the document. Thus, repeated \includemedia for the same sound file on different pages do not add much to the PDF file size.

Complete example using a sound URL: enter image description here

\documentclass{article}
\usepackage{media9}[2014/06/25]

\makeatletter
\usepackage{zref}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 audio file/URL, #2 from (secs), #3 to (secs), #4 button text
\newcommand\playSoundFromTo[4]{%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % get abs. page No. this button is on, using zlabel/zref
  \zref@label{btn\theBtn}%
  \xdef\btnpage{\zref@extractdefault{btn\theBtn}{abspage}{0}}% 
  % insert player only once for *this* audio file on *this* page
  \ifcsname #1.\btnpage\endcsname\else%
    \makebox[0pt][r]{\includemedia[
      label=sound.\pdfmdfivesum{#1}.\btnpage,
      activate=pageopen,
      addresource=#1,
      flashvars={
          source=#1
%        &hideBar=true %enable this to make player invisible
      },
      transparent,noplaybutton
    ]{\strut\hspace{1in}}{APlayer.swf}}%
    \expandafter\xdef\csname #1.\btnpage\endcsname{}%
  \fi%
  \mediabutton[%play button
    mediacommand=sound.\pdfmdfivesum{#1}.\btnpage:play[(#2)],
    mediacommand=sound.\pdfmdfivesum{#1}.\btnpage:pause[(#3)]
  ]{#4}%
  \stepcounter{Btn}%
}
\newcounter{Btn}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatother

\begin{document}
\section{OpenBSD 4.9 Release Song}

\noindent\playSoundFromTo{http://www.openbsd.org/songs/song49.mp3}{5.5}{37.0}{\fbox{A couple of questions}}

\noindent\playSoundFromTo{http://www.openbsd.org/songs/song49.mp3}{39.0}{49.0}{\fbox{The answer}}

\noindent\playSoundFromTo{http://www.openbsd.org/songs/song49.mp3}{206.5}{221.0}{\fbox{Last harmonica solo}}

\end{document}