Skip to content

Commit

Permalink
Fix the issue of video/audio playback with file://android_asset scheme.
Browse files Browse the repository at this point in the history
The MediaPlayerBridge in Chromium accepts uri as data source, but MediaPlayer
can't handle file:///android_asset. So for such case, handle it specially.
  • Loading branch information
sqliu authored and Jesus Sanchez-Palencia committed Oct 7, 2013
1 parent 04c4575 commit 2fe0579
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org.chromium.media;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.text.TextUtils;
Expand All @@ -14,11 +15,13 @@
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// A wrapper around android.media.MediaPlayer that allows the native code to use it.
Expand Down Expand Up @@ -114,7 +117,24 @@ protected boolean setDataSource(
if (!TextUtils.isEmpty(cookies))
headersMap.put("Cookie", cookies);
try {
getLocalPlayer().setDataSource(context, uri, headersMap);
String scheme = uri.getScheme();
List<String> segments = uri.getPathSegments();
if (scheme.equals("file") && segments.size() > 0 &&
segments.get(0).equals("android_asset")) {
// For media resources in file:///android_asset, handle it with
// AssetFileDescriptor instead of default uri.
StringBuilder pathBuilder = new StringBuilder();
for (int i = 1; i < segments.size(); i++) {
pathBuilder.append(segments.get(i));
if (i != (segments.size() - 1))
pathBuilder.append(File.separator);
}

AssetFileDescriptor afd = context.getAssets().openFd(pathBuilder.toString());
getLocalPlayer().setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
} else {
getLocalPlayer().setDataSource(context, uri, headersMap);
}
return true;
} catch (Exception e) {
return false;
Expand Down

0 comments on commit 2fe0579

Please sign in to comment.