MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource("http://streaming.shoutcast.com/80sPlanet"); // this steam broadcasts audio/mpeg mediaPlayer.prepareAsync(); mediaPlayer.start();
可以通过使用不同的参数集调用重载的setDataSource()来设置不同类型的DataSource.
这个函数有一个有趣的原型:
void setDataSource(MediaDataSource dataSource)
看起来可以使用您自己的实现完全覆盖DataSource.它确实有效:
import android.media.MediaDataSource; import java.io.BufferedInputStream; import java.io.IOException; import java.net.URL; import java.net.HttpURLConnection; public class UrlMediaDataSource extends MediaDataSource { URL url; HttpURLConnection connection; BufferedInputStream stream; public UrlMediaDataSource(URL url) throws IOException { this.url = url; connection = (HttpURLConnection) url.openConnection(); } @Override public long getSize() { return 0; } @Override public int readAt(long position,byte[] buffer,int offset,int size) throws IOException { if (stream == null) stream = new BufferedInputStream(connection.getInputStream()); return stream.read(buffer,offset,size); } @Override public void close() throws IOException { stream.close(); stream = null; connection.disconnect(); connection = null; } }
并在主要代码中:
UrlMediaDataSource dataSource = new UrlMediaDataSource(new URL("http://streaming.shoutcast.com/80sPlanet")); mediaPlayer.setDataSource(dataSource);
是的,这很好用.但是,如果我尝试音频/ aacp广播流(例如:“http://111.223.51.8:8005” – 它是“COOLfahrenheit 93”收音机),则播放器不播放. Logcat跟踪:
06-07 23:26:01.680 1352-1147/? E/GenericSource: Failed to init from data source! 06-07 23:26:01.681 1352-1093/? D/NuPlayerDriver: notifyListener_l(0xf3e051e0),(100,1,-2147483648) 06-07 23:26:01.735 1352-2013/? D/NuPlayerDriver: reset(0xf3e051e0) 06-07 23:26:01.735 1352-2013/? D/NuPlayerDriver: notifyListener_l(0xf3e051e0),(8,0) 06-07 23:26:01.736 1352-1093/? D/NuPlayerDriver: notifyResetComplete(0xf3e051e0)
但是,当没有使用自定义MediaDataSource时,URL工作正常(音乐播放):
mediaPlayer.setDataSource("http://111.223.51.8:8005");
有人知道管理这个的正确方法吗?
只是不建议我直接使用URL – 我需要一个自定义的MediaDataSource来访问流的原始数据.
解决方法
The main point is that the
MediaPlayer@H_404_35@ does playback audio/mpeg (both ways – through URL and through custom
MediaDataSource@H_404_35@),but audio/aacp streams could be played back only via URL as
DataSource@H_404_35@.
所以,让我们了解一下发生了什么.
当您将URL作为数据源传递时,正在执行this检查:
if ("file".equals(scheme)) { path = uri.getPath(); } else if (scheme != null) { // handle non-file sources nativeSetDataSource( MediaHTTPService.createHttpServiceBinderIfNecessary(path),path,keys,values); return; }
MediaPlayer uses MediaHTTPService类,which is responsible用于提供来自http,https和widevine协议的数据. MediaHTTPService在内部使用MediaHTTPConnection
,它可以解决所有繁重的工作问题.不幸的是,这些API尚未公开(但),但您可以看到如何在MediaHTTPConnection
源(特别是seekTo
method)中建立连接.因此,您提供给MediaPlayer的自定义数据源应该描述MediaHTTPConnection
类实现的大致逻辑.