当前位置: 首页 > 工具软件 > Icecast > 使用案例 >

android exoplayer 直播流,android – 使用Exoplayer从Icecast流中提取元数...

窦成荫
2023-12-01

我有一个从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);

MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource,

MediaCodecSelector.DEFAULT, null, true, null, null,

AudioCapabilities.getCapabilities(context), AudioManager.STREAM_MUSIC);

mPlayerCallback.playerStarted();

exoPlayer.prepare(audioRenderer);

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);

}

这样你就不会制作任何重复的流而且它是单数的.如果您不想在项目中使用AACDecoder库,那么您只需复制所需的文件并将它们直接包含在项目中.

 类似资料: