Skip to content

Commit e00b6f3

Browse files
author
James Dong
committed
Fix failure from setDataSource(String path) when path is a local file
o the failure was because the mediaserver does not have read permission to sdcard o related-to-bug: 6325960,6322913 Change-Id: I4feec01b8165c78563eee8aab69cb24df3244d03
1 parent 54ae147 commit e00b6f3

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

media/java/android/media/MediaPlayer.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import android.graphics.SurfaceTexture;
3535
import android.media.AudioManager;
3636

37+
import java.io.File;
3738
import java.io.FileDescriptor;
39+
import java.io.FileInputStream;
3840
import java.io.IOException;
3941
import java.net.InetSocketAddress;
4042
import java.util.Map;
@@ -847,8 +849,10 @@ public void setDataSource(Context context, Uri uri, Map<String, String> headers)
847849
* As an alternative, the application could first open the file for reading,
848850
* and then use the file descriptor form {@link #setDataSource(FileDescriptor)}.
849851
*/
850-
public native void setDataSource(String path)
851-
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException;
852+
public void setDataSource(String path)
853+
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
854+
setDataSource(path, null, null);
855+
}
852856

853857
/**
854858
* Sets the data source (file-path or http/rtsp URL) to use.
@@ -875,7 +879,20 @@ public void setDataSource(String path, Map<String, String> headers)
875879
++i;
876880
}
877881
}
878-
_setDataSource(path, keys, values);
882+
setDataSource(path, keys, values);
883+
}
884+
885+
private void setDataSource(String path, String[] keys, String[] values)
886+
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
887+
File file = new File(path);
888+
if (file.exists()) {
889+
FileInputStream is = new FileInputStream(file);
890+
FileDescriptor fd = is.getFD();
891+
setDataSource(fd);
892+
is.close();
893+
} else {
894+
_setDataSource(path, keys, values);
895+
}
879896
}
880897

881898
private native void _setDataSource(

media/jni/android_media_MediaPlayer.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,6 @@ android_media_MediaPlayer_setDataSourceAndHeaders(
215215
"setDataSource failed." );
216216
}
217217

218-
static void
219-
android_media_MediaPlayer_setDataSource(JNIEnv *env, jobject thiz, jstring path)
220-
{
221-
android_media_MediaPlayer_setDataSourceAndHeaders(env, thiz, path, NULL, NULL);
222-
}
223-
224218
static void
225219
android_media_MediaPlayer_setDataSourceFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length)
226220
{
@@ -825,8 +819,6 @@ android_media_MediaPlayer_setNextMediaPlayer(JNIEnv *env, jobject thiz, jobject
825819
// ----------------------------------------------------------------------------
826820

827821
static JNINativeMethod gMethods[] = {
828-
{"setDataSource", "(Ljava/lang/String;)V", (void *)android_media_MediaPlayer_setDataSource},
829-
830822
{
831823
"_setDataSource",
832824
"(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V",

0 commit comments

Comments
 (0)