我正在开发适用于Android的Chromecast应用,并试图让WebVTT字幕/字幕正常工作。
我按如下方式实例化MediaTrack对象:
MediaTrack track = new MediaTrack.Builder(0, MediaTrack.TYPE_TEXT)
.setContentId("http://www.example.com/example.vtt")
.setSubtype(MediaTrack.SUBTYPE_SUBTITLES)
.setContentType("ISO-8859-1")
.setLanguage(Locale.ENGLISH)
.setName("English")
.build();
并将其添加到列表中。
我使用构建器实例化MediaInfo,并使用.setMediaTracks方法引用轨道列表:
MediaInfo.Builder builder = new MediaInfo.Builder("http://www.example.com/example.m3u8")
.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
.setContentType("application/vnd.apple.mpegurl")
.setMetadata(movieMetadata)
.setMediaTracks(mediaTracks).build();
在我的应用程序中,我然后启动随附库附带的CastControllerActivity:
mCastManager.startCastControllerActivity(this, mSelectedMedia, position, autoPlay);
完成此操作后,我设置了活动媒体轨道(在我的情况下只有一个)和文本轨道样式:
long [] activeTracks = new long[1];
activeTracks[0] = 0l;
mCastManager.getRemoteMediaPlayer()
.setActiveMediaTracks(mCastManager.getGoogleApiClient(), activeTracks)
.setResultCallback(new MediaResultCallback());
mCastManager.getRemoteMediaPlayer().setTextTrackStyle(
mCastManager.getGoogleApiClient(),
TextTrackStyle.fromSystemSettings(getBaseContext()));
播放包含字幕的剪辑时,控制台会显示以下内容:
[775.223s] [cast.receiver.MediaManager] Too many track IDs cast_receiver.js:18
[775.232s] [cast.receiver.MediaManager] Invalid track IDs cast_receiver.js:18
[775.238s] [cast.receiver.MediaManager] Sending error: INVALID_REQUEST INVALID_PARAMS
并且返回的ResultCallback中的状态代码为SERVICE_MISSING - 即使我在开发时使用的两台Android 4.4设备上都安装了Google Play Services 5.0.89。字幕由CORS标题托管。
任何想法或指示?
答案 0 :(得分:1)
这是因为CCL尚未更新以处理曲目(这是即将推出的功能)。 CCL从MediaInfo构建Bundle并在触发CastControllerActivity时使用该bundle来传递MediaInfo(MediaInfo不是可分区的,因此必须使用包装器和Bundle是一种解决方案)。在现有的CCL中,当它创建一个捆绑包时,它不会考虑曲目,因此捆绑包没有曲目信息。因此,您实际上正在加载一个媒体(因为它发生在CastControllerActivity中),它没有任何曲目信息,因此您看到的错误。
Till Tracks支持被添加到CCL,您可以执行以下操作。在Utils类中,使用CCL中的以下代码更新两个方法fromMediaInfo()
和toMediaInfo()
:
private static final String KEY_TRACK_ID = "track-id";
private static final String KEY_TRACK_CONTENT_ID = "track-custom-id";
private static final String KEY_TRACK_NAME = "track-name";
private static final String KEY_TRACK_TYPE = "track-type";
private static final String KEY_TRACK_SUBTYPE = "track-subtype";
private static final String KEY_TRACK_LANGUAGE = "track-language";
private static final String KEY_TRACK_CUSTOM_DATA = "track-custom-data";
private static final String KEY_TRACKS_DATA = "track-data";
public static Bundle fromMediaInfo(MediaInfo info) {
if (null == info) {
return null;
}
MediaMetadata md = info.getMetadata();
Bundle wrapper = new Bundle();
wrapper.putString(MediaMetadata.KEY_TITLE, md.getString(MediaMetadata.KEY_TITLE));
wrapper.putString(MediaMetadata.KEY_SUBTITLE, md.getString(MediaMetadata.KEY_SUBTITLE));
wrapper.putString(KEY_URL, info.getContentId());
wrapper.putString(MediaMetadata.KEY_STUDIO, md.getString(MediaMetadata.KEY_STUDIO));
wrapper.putString(KEY_CONTENT_TYPE, info.getContentType());
wrapper.putInt(KEY_STREAM_TYPE, info.getStreamType());
if (!md.getImages().isEmpty()) {
ArrayList<String> urls = new ArrayList<String>();
for (WebImage img : md.getImages()) {
urls.add(img.getUrl().toString());
}
wrapper.putStringArrayList(KEY_IMAGES, urls);
}
JSONObject customData = info.getCustomData();
if (null != customData) {
wrapper.putString(KEY_CUSTOM_DATA, customData.toString());
}
if (null != info.getMediaTracks() && !info.getMediaTracks().isEmpty()) {
try {
JSONArray jsonArray = new JSONArray();
for (MediaTrack mt : info.getMediaTracks()) {
JSONObject jsonObject = new JSONObject();
jsonObject.put(KEY_TRACK_NAME, mt.getName());
jsonObject.put(KEY_TRACK_CONTENT_ID, mt.getContentId());
jsonObject.put(KEY_TRACK_ID, mt.getId());
jsonObject.put(KEY_TRACK_LANGUAGE, mt.getLanguage());
jsonObject.put(KEY_TRACK_TYPE, mt.getType());
jsonObject.put(KEY_TRACK_SUBTYPE, mt.getSubtype());
if (null != mt.getCustomData()) {
jsonObject.put(KEY_TRACK_CUSTOM_DATA, mt.getCustomData().toString());
}
jsonArray.put(jsonObject);
}
wrapper.putString(KEY_TRACKS_DATA, jsonArray.toString());
} catch (JSONException e) {
LOGE(TAG, "fromMediaInfo(): Failed to convert Tracks data to json", e);
}
}
return wrapper;
}
public static MediaInfo toMediaInfo(Bundle wrapper) {
if (null == wrapper) {
return null;
}
MediaMetadata metaData = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE);
metaData.putString(MediaMetadata.KEY_SUBTITLE,
wrapper.getString(MediaMetadata.KEY_SUBTITLE));
metaData.putString(MediaMetadata.KEY_TITLE, wrapper.getString(MediaMetadata.KEY_TITLE));
metaData.putString(MediaMetadata.KEY_STUDIO, wrapper.getString(MediaMetadata.KEY_STUDIO));
ArrayList<String> images = wrapper.getStringArrayList(KEY_IMAGES);
if (null != images && !images.isEmpty()) {
for (String url : images) {
Uri uri = Uri.parse(url);
metaData.addImage(new WebImage(uri));
}
}
String customDataStr = wrapper.getString(KEY_CUSTOM_DATA);
JSONObject customData = null;
if (!TextUtils.isEmpty(customDataStr)) {
try {
customData = new JSONObject(customDataStr);
} catch (JSONException e) {
LOGE(TAG, "Failed to deserialize the custom data string: custom data= "
+ customDataStr, e);
}
}
List<MediaTrack> mediaTracks = null;
if (null != wrapper.getString(KEY_TRACKS_DATA)) {
try {
JSONArray jsonArray = new JSONArray(wrapper.getString(KEY_TRACKS_DATA));
mediaTracks = new ArrayList<MediaTrack>();
if (null != jsonArray && jsonArray.length() > 0) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = (JSONObject) jsonArray.get(i);
MediaTrack.Builder builder = new MediaTrack.Builder(
jsonObj.getLong(KEY_TRACK_ID), jsonObj.getInt(KEY_TRACK_TYPE));
if (jsonObj.has(KEY_TRACK_NAME)) {
builder.setName(jsonObj.getString(KEY_TRACK_NAME));
}
if (jsonObj.has(KEY_TRACK_SUBTYPE)) {
builder.setSubtype(jsonObj.getInt(KEY_TRACK_SUBTYPE));
}
if (jsonObj.has(KEY_TRACK_CONTENT_ID)) {
builder.setContentId(jsonObj.getString(KEY_TRACK_CONTENT_ID));
}
if (jsonObj.has(KEY_TRACK_LANGUAGE)) {
builder.setLanguage(jsonObj.getString(KEY_TRACK_LANGUAGE));
}
if (jsonObj.has(KEY_TRACKS_DATA)) {
builder.setCustomData(
new JSONObject(jsonObj.getString(KEY_TRACKS_DATA)));
}
mediaTracks.add(builder.build());
}
}
} catch (JSONException e) {
LOGE(TAG, "Failed to build media tracks from the wrapper bundle", e);
}
}
return new MediaInfo.Builder(wrapper.getString(KEY_URL))
.setStreamType(wrapper.getInt(KEY_STREAM_TYPE))
.setContentType(wrapper.getString(KEY_CONTENT_TYPE))
.setMetadata(metaData)
.setCustomData(customData)
.setMediaTracks(mediaTracks)
.build();
}
这有望解决您的问题。