Android: Use a file descriptor for all local media files.
authorChristian Strømme <christian.stromme@digia.com>
Wed, 4 Jun 2014 20:30:05 +0000 (22:30 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Thu, 5 Jun 2014 21:25:23 +0000 (23:25 +0200)
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>
src/plugins/android/jar/src/org/qtproject/qt5/android/multimedia/QtAndroidMediaPlayer.java
src/plugins/android/src/mediaplayer/qandroidmediaplayercontrol.cpp

index 32d3496..ff92af7 100644 (file)
@@ -379,6 +379,7 @@ public class QtAndroidMediaPlayer
             mMediaPlayer.setDisplay(mSurfaceHolder);
 
         AssetFileDescriptor afd = null;
+        FileInputStream fis = null;
         try {
             mUri = Uri.parse(path);
             final boolean inAssets = (mUri.getScheme().compareTo("assets") == 0);
@@ -390,8 +391,8 @@ public class QtAndroidMediaPlayer
                 final long length = afd.getLength();
                 FileDescriptor fd = afd.getFileDescriptor();
                 mMediaPlayer.setDataSource(fd, offset, length);
-            } else if (mUri.getScheme().compareTo("tempfile") == 0) {
-                FileInputStream fis = new FileInputStream(mUri.getPath());
+            } else if (mUri.getScheme().compareTo("file") == 0) {
+                fis = new FileInputStream(mUri.getPath());
                 FileDescriptor fd = fis.getFD();
                 mMediaPlayer.setDataSource(fd);
             } else {
@@ -409,9 +410,13 @@ public class QtAndroidMediaPlayer
         } catch (final NullPointerException e) {
             Log.d(TAG, "" + e.getMessage());
         } finally {
-            if (afd !=null) {
-                try { afd.close(); } catch (final IOException ioe) { /* Ignore... */ }
-            }
+            try {
+               if (afd != null)
+                   afd.close();
+               if (fis != null)
+                   fis.close();
+            } catch (final IOException ioe) { /* Ignore... */ }
+
             if ((mState & State.Initialized) == 0) {
                 setState(State.Error);
                 onErrorNative(MediaPlayer.MEDIA_ERROR_UNKNOWN,
index 6565b05..6817d65 100644 (file)
@@ -312,7 +312,7 @@ void QAndroidMediaPlayerControl::setMedia(const QMediaContent &mediaContent,
         const QString path = url.toString().mid(3);
         mTempFile.reset(QTemporaryFile::createNativeFile(path));
         if (!mTempFile.isNull())
-            mediaPath = QLatin1String("tempfile://") + mTempFile->fileName();
+            mediaPath = QStringLiteral("file://") + mTempFile->fileName();
     } else {
         mediaPath = url.toString();
     }