Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
androidmediametadataretriever.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtCore/QUrl>
7#include <qdebug.h>
8#include <QtCore/qcoreapplication.h>
9
11
13{
14 m_metadataRetriever = QJniObject("android/media/MediaMetadataRetriever");
15}
16
21
23{
25
26 QJniObject metadata = m_metadataRetriever.callObjectMethod("extractMetadata",
27 "(I)Ljava/lang/String;",
28 jint(key));
29 if (metadata.isValid())
30 value = metadata.toString();
31
32 return value;
33}
34
35void AndroidMediaMetadataRetriever::release()
36{
37 if (!m_metadataRetriever.isValid())
38 return;
39
40 m_metadataRetriever.callMethod<void>("release");
41}
42
44{
45 if (!m_metadataRetriever.isValid())
46 return false;
47
49 if (url.isLocalFile()) { // also includes qrc files (copied to a temp file by QMediaPlayer)
50 QJniObject string = QJniObject::fromString(url.path());
51 QJniObject fileInputStream("java/io/FileInputStream",
52 "(Ljava/lang/String;)V",
53 string.object());
54
55 if (!fileInputStream.isValid())
56 return false;
57
58 QJniObject fd = fileInputStream.callObjectMethod("getFD",
59 "()Ljava/io/FileDescriptor;");
60 if (!fd.isValid()) {
61 fileInputStream.callMethod<void>("close");
62 return false;
63 }
64
65 auto methodId = env->GetMethodID(m_metadataRetriever.objectClass(), "setDataSource",
66 "(Ljava/io/FileDescriptor;)V");
67 env->CallVoidMethod(m_metadataRetriever.object(), methodId, fd.object());
68 bool ok = !env.checkAndClearExceptions();
69 fileInputStream.callMethod<void>("close");
70 if (!ok)
71 return false;
72 } else if (url.scheme() == QLatin1String("assets")) {
73 QJniObject string = QJniObject::fromString(url.path().mid(1)); // remove first '/'
74 QJniObject activity(QNativeInterface::QAndroidApplication::context());
75 QJniObject assetManager = activity.callObjectMethod("getAssets",
76 "()Landroid/content/res/AssetManager;");
77 QJniObject assetFd = assetManager.callObjectMethod("openFd",
78 "(Ljava/lang/String;)Landroid/content/res/AssetFileDescriptor;",
79 string.object());
80 if (!assetFd.isValid())
81 return false;
82
83 QJniObject fd = assetFd.callObjectMethod("getFileDescriptor",
84 "()Ljava/io/FileDescriptor;");
85 if (!fd.isValid()) {
86 assetFd.callMethod<void>("close");
87 return false;
88 }
89
90 auto methodId = env->GetMethodID(m_metadataRetriever.objectClass(), "setDataSource",
91 "(Ljava/io/FileDescriptor;JJ)V");
92 env->CallVoidMethod(m_metadataRetriever.object(), methodId,
93 fd.object(),
94 assetFd.callMethod<jlong>("getStartOffset"),
95 assetFd.callMethod<jlong>("getLength"));
96 bool ok = !env.checkAndClearExceptions();
97 assetFd.callMethod<void>("close");
98
99 if (!ok)
100 return false;
101 } else if (url.scheme() != QLatin1String("content")) {
102 // On API levels >= 14, only setDataSource(String, Map<String, String>) accepts remote media
103 QJniObject string = QJniObject::fromString(url.toString(QUrl::FullyEncoded));
104 QJniObject hash("java/util/HashMap");
105
106 auto methodId = env->GetMethodID(m_metadataRetriever.objectClass(), "setDataSource",
107 "(Ljava/lang/String;Ljava/util/Map;)V");
108 env->CallVoidMethod(m_metadataRetriever.object(), methodId,
109 string.object(), hash.object());
110 if (env.checkAndClearExceptions())
111 return false;
112 } else {
113 // While on API levels < 14, only setDataSource(Context, Uri) is available and works for
114 // remote media...
115 QJniObject string = QJniObject::fromString(url.toString(QUrl::FullyEncoded));
116 QJniObject uri = m_metadataRetriever.callStaticObjectMethod(
117 "android/net/Uri",
118 "parse",
119 "(Ljava/lang/String;)Landroid/net/Uri;",
120 string.object());
121 if (!uri.isValid())
122 return false;
123
124 auto methodId = env->GetMethodID(m_metadataRetriever.objectClass(), "setDataSource",
125 "(Landroid/content/Context;Landroid/net/Uri;)V");
126 env->CallVoidMethod(m_metadataRetriever.object(), methodId,
127 QNativeInterface::QAndroidApplication::context().object(),
128 uri.object());
129 if (env.checkAndClearExceptions())
130 return false;
131 }
132
133 return true;
134}
135
\inmodule QtCore
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString mid(qsizetype position, qsizetype n=-1) const &
Definition qstring.cpp:5300
\inmodule QtCore
Definition qurl.h:94
bool isLocalFile() const
Definition qurl.cpp:3445
QString scheme() const
Returns the scheme of the URL.
Definition qurl.cpp:1991
@ FullyEncoded
Definition qurl.h:129
QString toString(FormattingOptions options=FormattingOptions(PrettyDecoded)) const
Returns a string representation of the URL.
Definition qurl.cpp:2831
QString path(ComponentFormattingOptions options=FullyDecoded) const
Returns the path of the URL.
Definition qurl.cpp:2468
QHash< int, QWidget * > hash
[35multi]
Combined button and popup list for selecting options.
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLuint64 key
GLuint64 GLenum GLint fd
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
QUrl url("example.com")
[constructor-url-reference]