问题
I am working with AMediaCodec
and AMediaExtractor
, all works fine on all devices(I hope:), but if I check the same code on Android Q (in my case Pixel 2XL) I got such error AMEDIA_ERROR_UNSUPPORTED
.
What I do
bool NativeCodec::createStreamingMediaPlayer(const std::string &filename)
{
AMediaExtractor *ex = AMediaExtractor_new();
media_status_t err =
AMediaExtractor_setDataSource(ex, filename.c_str());; <-- Here media status I got AMEDIA_ERROR_UNSUPPORTED
if (err != AMEDIA_OK)
{
__android_log_print(ANDROID_LOG_ERROR, "ERROR", "ERROR ::: %s", std::to_string(err).c_str());
return false;
}
.....
}
Maybe this is somehow connected with privicy that was introduced in Android Q, but I didn't find any info about it...
How to check this issue?
回答1:
This to me seems a bug on Android 10. It seems that android:requestLegacyExternalStorage="true"
does not change the situation. You may have to request <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE/>
on manifest and ask same permission at runtime. The AMediaExtractor_setDataSource
function must be called on a thread that is attached to Java. Doing all of that correctly will allow you to make it work on other versions of Android but not on Android 10. I've reported the issue on Android Bug Tracker here: https://issuetracker.google.com/144837266
As per google answer, it seems that all the app using native libraries that require file access through path can be affected and they know the issue https://www.youtube.com/watch?v=UnJ3amzJM94 .
A workaround in my case was to use AMediaExtractor_setDataSourceFd
, getting the file descriptor at Java level through contentResolver
and its method openFileDescriptor
.
回答2:
This is due to you not specifying a Java thread to be used. Create the thread and then attach it via AttachCurrentThread
来源:https://stackoverflow.com/questions/57653901/amediaextractor-setdatasource-amedia-error-unsupported-error-on-android-q