mirror of https://github.com/FongMi/TV.git
parent
edc88d2f7a
commit
ccf6d607ae
@ -0,0 +1,47 @@ |
||||
package com.fongmi.bear.player; |
||||
|
||||
import android.net.Uri; |
||||
|
||||
import com.fongmi.bear.App; |
||||
import com.google.android.exoplayer2.C; |
||||
import com.google.android.exoplayer2.MediaItem; |
||||
import com.google.android.exoplayer2.ext.rtmp.RtmpDataSource; |
||||
import com.google.android.exoplayer2.source.MediaSource; |
||||
import com.google.android.exoplayer2.source.ProgressiveMediaSource; |
||||
import com.google.android.exoplayer2.source.dash.DashMediaSource; |
||||
import com.google.android.exoplayer2.source.hls.HlsMediaSource; |
||||
import com.google.android.exoplayer2.source.rtsp.RtspMediaSource; |
||||
import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource; |
||||
import com.google.android.exoplayer2.upstream.DataSource; |
||||
import com.google.android.exoplayer2.upstream.DefaultDataSource; |
||||
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource; |
||||
import com.google.android.exoplayer2.upstream.HttpDataSource; |
||||
import com.google.android.exoplayer2.util.Util; |
||||
|
||||
import java.util.Map; |
||||
|
||||
public class ExoUtil { |
||||
|
||||
public static MediaSource getSource(Map<String, String> headers, String url) { |
||||
Uri videoUri = Uri.parse(url); |
||||
DataSource.Factory factory = getFactory(headers, url); |
||||
MediaItem mediaItem = new MediaItem.Builder().setUri(videoUri).build(); |
||||
int type = Util.inferContentType(videoUri); |
||||
if (type == C.CONTENT_TYPE_HLS || url.contains(".php") || url.contains(".m3u8")) { |
||||
return new HlsMediaSource.Factory(factory).createMediaSource(mediaItem); |
||||
} else if (type == C.CONTENT_TYPE_DASH) { |
||||
return new DashMediaSource.Factory(factory).createMediaSource(mediaItem); |
||||
} else if (type == C.CONTENT_TYPE_SS) { |
||||
return new SsMediaSource.Factory(factory).createMediaSource(mediaItem); |
||||
} else if (type == C.CONTENT_TYPE_RTSP) { |
||||
return new RtspMediaSource.Factory().createMediaSource(mediaItem); |
||||
} else { |
||||
return new ProgressiveMediaSource.Factory(factory).createMediaSource(mediaItem); |
||||
} |
||||
} |
||||
|
||||
private static DataSource.Factory getFactory(Map<String, String> headers, String url) { |
||||
HttpDataSource.Factory httpDataSourceFactory = new DefaultHttpDataSource.Factory().setDefaultRequestProperties(headers).setAllowCrossProtocolRedirects(true); |
||||
return url.startsWith("rtmp") ? new RtmpDataSource.Factory() : new DefaultDataSource.Factory(App.get(), httpDataSourceFactory); |
||||
} |
||||
} |
||||
@ -0,0 +1,37 @@ |
||||
package com.fongmi.bear.player; |
||||
|
||||
import com.fongmi.bear.App; |
||||
import com.google.android.exoplayer2.ExoPlayer; |
||||
import com.google.gson.JsonObject; |
||||
import com.google.gson.JsonParser; |
||||
|
||||
import java.util.HashMap; |
||||
|
||||
public class Player { |
||||
|
||||
private ExoPlayer mPlayer; |
||||
|
||||
private static class Loader { |
||||
static volatile Player INSTANCE = new Player(); |
||||
} |
||||
|
||||
public static Player get() { |
||||
return Loader.INSTANCE; |
||||
} |
||||
|
||||
public static ExoPlayer exo() { |
||||
return get().mPlayer = get().mPlayer == null ? new ExoPlayer.Builder(App.get()).build() : get().mPlayer; |
||||
} |
||||
|
||||
public void setMediaSource(JsonObject object) { |
||||
HashMap<String, String> headers = new HashMap<>(); |
||||
String url = object.get("url").getAsString(); |
||||
if (object.has("header")) { |
||||
JsonObject header = JsonParser.parseString(object.get("header").getAsString()).getAsJsonObject(); |
||||
for (String key : header.keySet()) headers.put(key, header.get(key).getAsString()); |
||||
} |
||||
mPlayer.setMediaSource(ExoUtil.getSource(headers, url)); |
||||
mPlayer.prepare(); |
||||
mPlayer.play(); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue