转载自 http://blog.csdn.net/cwg19910909/article/details/50482807
android当中获取视频音频的时长,我列举了三种。
1:获取视频URI后获取cursor
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">cursor<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getLong</span>(cursor<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getColumnIndexOrThrow</span>(MediaStore<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Video</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Media</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.DURATION</span>))<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
2:根据MediaPlayer获取:
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">MediaPlayer mediaPlayer = new MediaPlayer()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
mediaPlayer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setDataSource</span>(url)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
mediaPlayer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.prepare</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
mediaPlayer<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getDuration</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
3:采用MediaMetadataRetriever的方式:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> static <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span> getRingDuring(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span> mUri){
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">duration</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">null</span>;
android<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>media<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>MediaMetadataRetriever mmr <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">new</span> android<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>media<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>MediaMetadataRetriever();
try {
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (mUri <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">!=</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">null</span>) {
HashMap<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span> headers<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">null</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (headers <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">==</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">null</span>) {
headers <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">new</span> HashMap<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">String</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>();
headers<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>put(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"User-Agent"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Mozilla/5.0 (Linux; U; Android 4.4.2; zh-CN; MW-KW-001 Build/JRO03C) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/1.0.0.001 U4/0.8.0 Mobile Safari/533.1"</span>);
}
mmr<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>setDataSource(mUri, headers);
}
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">duration</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> mmr<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>extractMetadata(android<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>media<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>MediaMetadataRetriever<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>METADATA_KEY_DURATION);
} catch (Exception ex) {
} finally {
mmr<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>release();
}
LogUtil<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>e(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ryan"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"duration "</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">+</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">duration</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">duration</span>;
}</code>