Skip to content

Commit cc26c0e

Browse files
Merge pull request #491 from TakayukiHoshi1984/bugfix_android10_mediaplayer
不具合修正: Android 10 上の MediaPlayer プロファイルでメディアを再生できない場合がある
2 parents b9127b6 + 8d79243 commit cc26c0e

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

  • dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/mediaplayer

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/mediaplayer/HostMediaPlayerManager.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,14 @@ public void putMediaId(final Intent response, final String mediaId) {
230230
mMyCurrentMediaId = mediaId;
231231

232232
// Videoとしてパスを取得
233-
Uri mUri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, Long.valueOf(mediaId));
233+
Uri uri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, Long.valueOf(mediaId));
234234

235-
String filePath = getPathFromUri(mUri);
235+
String filePath = getPathFromUri(uri);
236236

237237
// nullなら、Audioとしてパスを取得
238238
if (filePath == null) {
239-
mUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Long.valueOf(mediaId));
240-
filePath = getPathFromUri(mUri);
239+
uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Long.valueOf(mediaId));
240+
filePath = getPathFromUri(uri);
241241
}
242242

243243
// ファイル存在チェック
@@ -248,6 +248,7 @@ public void putMediaId(final Intent response, final String mediaId) {
248248
}
249249

250250
String mMineType = getMIMEType(filePath);
251+
FileDescriptor fd = getDescriptorFromUri(uri);
251252

252253
// パス指定の場合
253254
if (AUDIO_TYPE_LIST.contains(mMineType)) {
@@ -260,11 +261,11 @@ public void putMediaId(final Intent response, final String mediaId) {
260261

261262
try {
262263
mSetMediaType = MEDIA_TYPE_MUSIC;
263-
mMyCurrentUri = mUri;
264+
mMyCurrentUri = uri;
264265
mMyCurrentFilePath = filePath;
265266
mMyCurrentFileMIMEType = mMineType;
266267
mMediaStatus = MEDIA_PLAYER_SET;
267-
mMediaPlayer.setDataSource(filePath);
268+
mMediaPlayer.setDataSource(fd);
268269
mMediaPlayer.setOnCompletionListener((mp) -> {
269270
mMediaStatus = MEDIA_PLAYER_COMPLETE;
270271
sendOnStatusChangeEvent("complete");
@@ -290,7 +291,7 @@ public void putMediaId(final Intent response, final String mediaId) {
290291
} else if (VIDEO_TYPE_LIST.contains(mMineType)) {
291292
try {
292293
mSetMediaType = MEDIA_TYPE_VIDEO;
293-
mMyCurrentUri = mUri;
294+
mMyCurrentUri = uri;
294295
mMyCurrentFilePath = filePath;
295296
mMyCurrentFileMIMEType = mMineType;
296297

@@ -304,7 +305,7 @@ public void putMediaId(final Intent response, final String mediaId) {
304305
FileInputStream fis;
305306

306307
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
307-
ParcelFileDescriptor descriptor = getContentResolver().openFileDescriptor(mUri, "r");
308+
ParcelFileDescriptor descriptor = getContentResolver().openFileDescriptor(uri, "r");
308309
fis = new ParcelFileDescriptor.AutoCloseInputStream(descriptor);
309310
} else {
310311
fis = new FileInputStream(mMyCurrentFilePath);
@@ -783,6 +784,19 @@ public void getPlayStatus(final Intent response) {
783784
}
784785
}
785786

787+
private FileDescriptor getDescriptorFromUri(final Uri uri) {
788+
ContentResolver resolver = getContentResolver();
789+
try {
790+
ParcelFileDescriptor descriptor = resolver.openFileDescriptor(uri, "r");
791+
if (descriptor == null) {
792+
return null;
793+
}
794+
return descriptor.getFileDescriptor();
795+
} catch (IOException e) {
796+
return null;
797+
}
798+
}
799+
786800
/**
787801
* URIからパスを取得.
788802
*

0 commit comments

Comments
 (0)