9

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.

  • For this feature to work, some adjustments of APlayer.swf are needed. I am working on it. – AlexG Jun 20 '14 at 7:11
2

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}
  • As far as I can see, \playSoundFromTo cannot be used in tabular enviroments. Is there a way to customize this so it will? – K.G. Feuerherm Jul 8 '14 at 19:07
  • @K.G.Feuerherm : As a workaround, put { and } around the \playSoundFromTo command. – AlexG Jul 9 '14 at 16:21
  • Thanks--found that to work around \mediabutton. Another thing I noticed was that use in floats is problematic. Putting a dummy commmand with an empty button label before it tends to work, if the float stays on the same page. Using the float package with [H] almost always works, unless the float is near the top of the page; in such a case, it seems that the package may read the wrong iteration of files during a make, because it sometimes dies with a complaint about not being an outer par anyway. No luck at all with RTL text, however :( – K.G. Feuerherm Jul 9 '14 at 21:04
  • I've been pondering this. Is this quite right? Seems to me that the counter is incremented on every call to \playSoundFromTo, even if it is the same sound file. Do we not want to restrict the \stepcounter somehow? @AlexG – K.G. Feuerherm Aug 6 '14 at 20:52
  • @K.G.Feuerherm: Each button needs its unique ID. This ID is used to determine the document page it is on. It is not related to the sound file. Then we want all buttons connected to a specific sound file to control the same sound player instance. Also, we don't want more than one \includemedia for a specific sound file on the same page. – AlexG Aug 8 '14 at 10:27

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.