Android: Use a file descriptor for all local media files.

Using a fd is more consistent across different Android versions and
also works with files that are in the applications private storage.

Task-number: QTBUG-39346
Change-Id: I462822459d12d7842d15f1cb7caafc75c18fe32c
Reviewed-by: Yoann Lopes <yoann.lopes@digia.com>
This commit is contained in:
Christian Strømme
2014-06-04 22:30:05 +02:00
committed by The Qt Project
parent d71aaca893
commit 8a66559e0b
2 changed files with 11 additions and 6 deletions

View File

@@ -379,6 +379,7 @@ public class QtAndroidMediaPlayer
mMediaPlayer.setDisplay(mSurfaceHolder); mMediaPlayer.setDisplay(mSurfaceHolder);
AssetFileDescriptor afd = null; AssetFileDescriptor afd = null;
FileInputStream fis = null;
try { try {
mUri = Uri.parse(path); mUri = Uri.parse(path);
final boolean inAssets = (mUri.getScheme().compareTo("assets") == 0); final boolean inAssets = (mUri.getScheme().compareTo("assets") == 0);
@@ -390,8 +391,8 @@ public class QtAndroidMediaPlayer
final long length = afd.getLength(); final long length = afd.getLength();
FileDescriptor fd = afd.getFileDescriptor(); FileDescriptor fd = afd.getFileDescriptor();
mMediaPlayer.setDataSource(fd, offset, length); mMediaPlayer.setDataSource(fd, offset, length);
} else if (mUri.getScheme().compareTo("tempfile") == 0) { } else if (mUri.getScheme().compareTo("file") == 0) {
FileInputStream fis = new FileInputStream(mUri.getPath()); fis = new FileInputStream(mUri.getPath());
FileDescriptor fd = fis.getFD(); FileDescriptor fd = fis.getFD();
mMediaPlayer.setDataSource(fd); mMediaPlayer.setDataSource(fd);
} else { } else {
@@ -409,9 +410,13 @@ public class QtAndroidMediaPlayer
} catch (final NullPointerException e) { } catch (final NullPointerException e) {
Log.d(TAG, "" + e.getMessage()); Log.d(TAG, "" + e.getMessage());
} finally { } finally {
if (afd !=null) { try {
try { afd.close(); } catch (final IOException ioe) { /* Ignore... */ } if (afd != null)
} afd.close();
if (fis != null)
fis.close();
} catch (final IOException ioe) { /* Ignore... */ }
if ((mState & State.Initialized) == 0) { if ((mState & State.Initialized) == 0) {
setState(State.Error); setState(State.Error);
onErrorNative(MediaPlayer.MEDIA_ERROR_UNKNOWN, onErrorNative(MediaPlayer.MEDIA_ERROR_UNKNOWN,

View File

@@ -312,7 +312,7 @@ void QAndroidMediaPlayerControl::setMedia(const QMediaContent &mediaContent,
const QString path = url.toString().mid(3); const QString path = url.toString().mid(3);
mTempFile.reset(QTemporaryFile::createNativeFile(path)); mTempFile.reset(QTemporaryFile::createNativeFile(path));
if (!mTempFile.isNull()) if (!mTempFile.isNull())
mediaPath = QLatin1String("tempfile://") + mTempFile->fileName(); mediaPath = QStringLiteral("file://") + mTempFile->fileName();
} else { } else {
mediaPath = url.toString(); mediaPath = url.toString();
} }