Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the problem when video file in res/raw file can be played. #7866

Closed
JeffMony opened this issue Sep 8, 2020 · 9 comments
Closed

Fix the problem when video file in res/raw file can be played. #7866

JeffMony opened this issue Sep 8, 2020 · 9 comments
Assignees

Comments

@JeffMony
Copy link

JeffMony commented Sep 8, 2020

Before filing a feature request:

When filing a feature request:

Fill out the sections below, leaving the headers but replacing the content. If
you're unable to provide certain information, please explain why in the relevant
section. We may close issues if they do not include sufficient information.

[REQUIRED] Use case description

When I create a raw folder in res folder, I save test.mp4 in res/raw/test.mp4,
I want to play the raw video file with : android.resource schema
But exoplayer cannot play the android.resource schema file

Proposed solution

Author: JeffMony <jeffmony@163.com>
Date:   Tue Sep 8 14:56:25 2020 +0800

    Fix the problem when video file in res/raw file can be played.
    
    Signed-off-by: JeffMony <jeffmony@163.com>

diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/DefaultDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/DefaultDataSource.java
index afef3e676..582a53b02 100644
--- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/DefaultDataSource.java
+++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/DefaultDataSource.java
@@ -58,6 +58,7 @@ public final class DefaultDataSource implements DataSource {
   private static final String SCHEME_RTMP = "rtmp";
   private static final String SCHEME_UDP = "udp";
   private static final String SCHEME_RAW = RawResourceDataSource.RAW_RESOURCE_SCHEME;
+  private static final String SCHEME_ANDROID_RESOURCE = RawResourceDataSource.ANDROID_RESOURCE_SCHEME;
 
   private final Context context;
   private final List<TransferListener> transferListeners;
@@ -169,7 +170,7 @@ public final class DefaultDataSource implements DataSource {
       dataSource = getUdpDataSource();
     } else if (DataSchemeDataSource.SCHEME_DATA.equals(scheme)) {
       dataSource = getDataSchemeDataSource();
-    } else if (SCHEME_RAW.equals(scheme)) {
+    } else if (SCHEME_RAW.equals(scheme) || SCHEME_ANDROID_RESOURCE.equals(scheme)) {
       dataSource = getRawResourceDataSource();
     } else {
       dataSource = baseDataSource;
diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/RawResourceDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/RawResourceDataSource.java
index 0595cb84b..4c7c55d85 100644
--- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/RawResourceDataSource.java
+++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/RawResourceDataSource.java
@@ -65,6 +65,7 @@ public final class RawResourceDataSource extends BaseDataSource {
 
   /** The scheme part of a raw resource URI. */
   public static final String RAW_RESOURCE_SCHEME = "rawresource";
+  public static final String ANDROID_RESOURCE_SCHEME = "android.resource";
 
   private final Resources resources;
 
@@ -87,7 +88,8 @@ public final class RawResourceDataSource extends BaseDataSource {
     try {
       Uri uri = dataSpec.uri;
       this.uri = uri;
-      if (!TextUtils.equals(RAW_RESOURCE_SCHEME, uri.getScheme())) {
+      if (!TextUtils.equals(RAW_RESOURCE_SCHEME, uri.getScheme()) &&
+          !TextUtils.equals(ANDROID_RESOURCE_SCHEME, uri.getScheme())) {
         throw new RawResourceDataSourceException("URI must use scheme " + RAW_RESOURCE_SCHEME);
       }
 

Alternatives considered

A clear and concise description of any alternative solutions you considered,
if applicable.

@JeffMony
Copy link
Author

JeffMony commented Sep 8, 2020

@ojw28 Could you check the feature?

@JeffMony
Copy link
Author

JeffMony commented Sep 8, 2020

@ojw28 Could you check the feature?

Yes, I have checked it with many phones, such as Huawei/Vivo/Oppo/Mi Phone.

@JeffMony
Copy link
Author

JeffMony commented Sep 8, 2020

@kim-vde kim-vde self-assigned this Sep 8, 2020
@kim-vde
Copy link
Contributor

kim-vde commented Sep 8, 2020

Can you explain why you want the scheme to be "android.resource"? Can you use RawResourceDataSource.buildRawResourceUri(R.raw.filename) to build the URI?

@JeffMony
Copy link
Author

JeffMony commented Sep 8, 2020

@ojw28
I use the android.resource schema for the following reason:
Android platform support android.resource schema, for example, http://androidxref.com/9.0.0_r3/xref/frameworks/base/core/java/android/content/ContentResolver.java#190
`
public static final String SCHEME_ANDROID_RESOURCE = "android.resource";

96 public final @nullable InputStream openInputStream(@nonnull Uri uri)
997 throws FileNotFoundException {
998 Preconditions.checkNotNull(uri, "uri");
999 String scheme = uri.getScheme();
1000 if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
1001 // Note: left here to avoid breaking compatibility. May be removed
1002 // with sufficient testing.
1003 OpenResourceIdResult r = getResourceId(uri);
1004 try {
1005 InputStream stream = r.r.openRawResource(r.id);
1006 return stream;
1007 } catch (Resources.NotFoundException ex) {
1008 throw new FileNotFoundException("Resource does not exist: " + uri);
1009 }
1010 } else if (SCHEME_FILE.equals(scheme)) {
1011 // Note: left here to avoid breaking compatibility. May be removed
1012 // with sufficient testing.
1013 return new FileInputStream(uri.getPath());
1014 } else {
1015 AssetFileDescriptor fd = openAssetFileDescriptor(uri, "r", null);
1016 try {
1017 return fd != null ? fd.createInputStream() : null;
1018 } catch (IOException e) {
1019 throw new FileNotFoundException("Unable to create stream");
1020 }
1021 }
1022 }
`

ExoPlayer is a universal player, it should support the android platform standard schema such as android.resource

@JeffMony
Copy link
Author

JeffMony commented Sep 8, 2020

@kim-vde The reason

@ojw28
Copy link
Contributor

ojw28 commented Sep 8, 2020

This is equivalent to [Internal ref: b/162842184]. We should probably just make the changes documented in #6 of the internal issue.

@JeffMony
Copy link
Author

JeffMony commented Sep 8, 2020

This is equivalent to [Internal ref: b/162842184]. We should probably just make the changes documented in #6 of the internal issue.

So , will you submit this change to ExoPlayer project ?

@kim-vde
Copy link
Contributor

kim-vde commented Sep 8, 2020

Yes, it is work in progress.

kim-vde added a commit that referenced this issue Sep 9, 2020
Issue: #7866
PiperOrigin-RevId: 330736774
@kim-vde kim-vde closed this as completed Sep 9, 2020
ojw28 pushed a commit that referenced this issue Oct 21, 2020
Issue: #7866
PiperOrigin-RevId: 330736774
@google google locked and limited conversation to collaborators Nov 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants