从Mediaplayer切换到一个简单的实现Exoplayer我注意到加载时间大大改善,但我想知道是否有任何内置功能,如流式音频时元数据更改侦听器?
我使用一个简单的例子实现了Exoplayer,如下所示:
Uri uri = Uri.parse(url); DefaultSampleSource sampleSource = new DefaultSampleSource(new FrameworkSampleExtractor(context, uri, null), 2); TrackRenderer au@R_404_6901@Renderer = new MediaCodecAu@R_404_6901@TrackRenderer(sampleSource); mExoPlayerInstance.prepare(au@R_404_6901@Renderer); mExoPlayerInstance.setPlayWhenReady(true);
解决方法:
我有一个从IceCast Stream启动ExoPlayer的AsyncTask:
OkhttpClIEnt okhttpClIEnt = new OkhttpClIEnt();UrIDataSource urIDataSource = new OkhttpDataSource(okhttpClIEnt, userAgent, null, null, CacheControl.FORCE_NETWORK);((OkhttpDataSource) urIDataSource).setRequestProperty("Icy-MetaData", "1");((OkhttpDataSource) urIDataSource).setPlayerCallback(mPlayerCallback);DataSource dataSource = new DefaultUrIDataSource(context, null, urIDataSource);ExtractorSampleSource sampleSource = new ExtractorSampleSource(uri, dataSource, allocator, BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE);MediaCodecAu@R_404_6901@TrackRenderer au@R_404_6901@Renderer = new MediaCodecAu@R_404_6901@TrackRenderer(sampleSource,MediaCodecSelector.DEFAulT, null, true, null, null,Au@R_404_6901@CapabilitIEs.getCapabilitIEs(context), Au@R_404_6901@Manager.STREAM_MUSIC);mPlayerCallback.playerStarted();exoPlayer.prepare(au@R_404_6901@Renderer);
OkhttpDataSource是使用OkhttpClIEnt实现httpDataSource的类.它创建inputStream作为请求的响应.我从AACDecoder库https://github.com/vbartacek/aacdecoder-android/blob/master/decoder/src/com/spoledge/aacdecoder/IcyInputStream.java中包含了这个类,并根据响应将inputStream替换为IcyinputStream:
(在OkhttpDataSource的open()中)
try { response = okhttpClIEnt.newCall(request).execute(); responseByteStream = response.body().byteStream(); String icyMetaIntString = response.header("icy-Metaint"); int icyMetaInt = -1; if (icyMetaIntString != null) { try { icyMetaInt = Integer.parseInt(icyMetaIntString); if (icyMetaInt > 0) responseByteStream = new IcyinputStream(responseByteStream, icyMetaInt, playerCallback); } catch (Exception e) { Log.e(TAG, "The icy-Metaint '" + icyMetaInt + "' cannot be parsed: '" + e); } }} catch (IOException e) { throw new httpDataSourceException("Unable to connect to " + dataSpec.uri.toString(), e, dataSpec);}
现在IcyinputStream可以捕获medatada并调用回调对象(此处为playerCallback). PlayerCallback也来自AACDecoder库:https://github.com/vbartacek/aacdecoder-android/blob/b58c519a341340a251f3291895c76ff63aef5b94/decoder/src/com/spoledge/aacdecoder/PlayerCallback.java
这样你就不会制作任何重复的流而且它是单数的.如果您不想在项目中使用AACDecoder库,那么您只需复制所需的文件并将它们直接包含在项目中.
总结以上是内存溢出为你收集整理的android – 使用Exoplayer从Icecast流中提取元数据全部内容,希望文章能够帮你解决android – 使用Exoplayer从Icecast流中提取元数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)