Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

[Android] Give host application a chance to take over MediaPlayer #3670

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public XWalkContent(Context context, AttributeSet attrs, XWalkViewInternal xwVie
mGeolocationPermissions = new XWalkGeolocationPermissions(sharedPreferences);

MediaPlayerBridge.setResourceLoadingFilter(
new XWalkMediaPlayerResourceLoadingFilter());
new XWalkMediaPlayerResourceLoadingFilter(mContentsClientBridge));

setNativeContent(nativeInit());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

package org.xwalk.core.internal;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.graphics.Bitmap;
import android.graphics.Picture;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.http.SslError;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
Expand All @@ -23,6 +26,7 @@
import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.chromium.content.browser.ContentViewClient;
import org.chromium.content_public.browser.WebContents;
Expand Down Expand Up @@ -171,6 +175,9 @@ public abstract XWalkWebResourceResponseInternal shouldInterceptRequest(

public abstract boolean shouldOverrideUrlLoading(String url);

public abstract boolean shouldOverrideMediaPlaying(MediaPlayer mediaPlayer,
Context context, Uri uri, Map<String, String> headers);

public abstract void onUnhandledKeyEvent(KeyEvent event);

public abstract boolean onConsoleMessage(ConsoleMessage consoleMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Picture;
import android.media.MediaPlayer;
import android.net.Uri;
import android.net.http.SslCertificate;
import android.net.http.SslError;
Expand Down Expand Up @@ -210,6 +212,16 @@ public boolean shouldOverrideUrlLoading(String url) {
return false;
}

@Override
public boolean shouldOverrideMediaPlaying(MediaPlayer mediaPlayer,
Context context, Uri uri, Map<String, String> headers) {
if (mXWalkResourceClient != null && mXWalkView != null) {
return mXWalkResourceClient.shouldOverrideMediaPlaying(mXWalkView,
mediaPlayer, context, uri, headers);
}
return false;
}

@Override
public boolean shouldOverrideKeyEvent(KeyEvent event) {
boolean overridden = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.chromium.media.MediaPlayerBridge;

import java.io.File;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

Expand All @@ -22,27 +23,34 @@

class XWalkMediaPlayerResourceLoadingFilter extends
MediaPlayerBridge.ResourceLoadingFilter {
private XWalkContentsClientBridge mContentsClientBridge;

XWalkMediaPlayerResourceLoadingFilter(XWalkContentsClientBridge clientBridge) {
mContentsClientBridge = clientBridge;
}

@Override
public boolean shouldOverrideResourceLoading(MediaPlayer mediaPlayer,
Context context, Uri uri) {
Context context, Uri uri, Map<String, String> headers) {
String scheme = uri.getScheme();
if (scheme == null) return false;
if (scheme.equals(AndroidProtocolHandler.APP_SCHEME)) {
uri = AndroidProtocolHandler.appUriToFileUri(uri);
}

scheme = uri.getScheme();
if (!scheme.equals(AndroidProtocolHandler.FILE_SCHEME)) return false;

try {
AssetFileDescriptor afd =
context.getAssets().openFd(AndroidProtocolHandler.getAssetPath(uri));
mediaPlayer.setDataSource(
afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());

return true;
} catch (Exception e) {
return false;
if (scheme.equals(AndroidProtocolHandler.FILE_SCHEME)) {
try {
AssetFileDescriptor afd =
context.getAssets().openFd(AndroidProtocolHandler.getAssetPath(uri));
mediaPlayer.setDataSource(
afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
return true;
} catch (Exception e) {
return false;
}
}

return mContentsClientBridge.shouldOverrideMediaPlaying(mediaPlayer, context, uri, headers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.net.http.SslError;
import android.net.Uri;
import android.os.Build;
import android.text.InputType;
import android.util.Log;
Expand Down Expand Up @@ -260,6 +262,26 @@ public boolean shouldOverrideUrlLoading(XWalkViewInternal view, String url) {
return false;
}

/**
* Give the host application a chance to take over the control when a new
* media is about to be played in the current XWalkViewInternal.
*
* @param view The XWalkViewInternal that is initiating the callback.
* @param mediaPalyer The android system services.
* @param context The context to use when resolving the Uri.
* @param uri Tthe Content URI of data you want to play
* @param header The headers to be sent together with the request for the data Note
* that the cross domain redirection is allowed by default.
* @return True if the host application wants to play itself, otherwise return false.
*
* @since 6.0
*/
@XWalkAPI
public boolean shouldOverrideMediaPlaying(XWalkViewInternal view,
MediaPlayer mediaPlayer, Context context, Uri uri, Map<String, String> headers) {
return false;
}

/**
* Notify the host application that an SSL error occurred while loading a
* resource. The host application must call either callback.onReceiveValue(true)
Expand Down