我正在写一本书,允许在选定点播放 MP3。我找到了一个适用于一般情况(即整个文件)的好解决方案:语音学项目中的音频示例不过我也想玩部分文件也是如此(例如句子内的单词或短语)。
一种解决方案是使用音频软件将文件分割成必要的部分,但这当然会产生很多不必要的重复。
media9 的文档中提到了 seek,这是解决方案的一半(尽管我承认我不清楚如何实现这一点)。浏览 JavaScript 文档后发现了以下代码,这表明这是可能的:
var player = app.media.openPlayer({
rendition: this.media.getRendition( "myAudio" ),
doc: this,
settings: {
startAt: 3,
endAt: 8
}
});
上面链接给出的MWE如下:
\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}
如果您能就如何进行提出任何建议,我将不胜感激。
答案1
2014/06/25
的版本media9
已更新播放器APlayer.swf
和VPlayer.swf
。
play
其编程接口的和方法pause
现在接受可选参数,这些参数是媒体文件中的时间偏移量(以秒为单位)。因此,正确配置后,\mediabutton
嵌入式播放器可以播放媒体文件的一部分。
带有两个按钮的通用声音示例,同样可用于视频:
\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}}
在语音学项目的特定情况下,可能需要在文档的不同页面上播放同一个声音文件。在这里,我们需要在每个声音文件和每个通过媒体按钮引用播放器的页面上至少嵌入一次音频播放器。为了自动执行此操作,下面的代码示例定义了命令
\playSoundFromTo{<mp3 file path or URL>}{<tStart>}{<tEnd>}{<button text>}
请注意,特定声音文件和播放器 SWF 仅一次物理嵌入文档。因此,\includemedia
在不同页面上重复相同的声音文件不会增加 PDF 文件大小。
使用声音 URL 的完整示例:
\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}