我正在尝试MediaStore
在 Android 10 上从中获取音频文件,从中提取音频数据,对其进行解码并播放它。
调用实例时出现以下错误setDataSource
:MediaExtractor
Error setting extractor data source, err -10002
重现:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val path = getSong().get(0).path
stringToJNI("file://"+ path)
val URI = Uri.parse(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.toString() + "/" + getSong().get(0).id)
stringToJNI(URI!!.toString())
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
external fun stringToJNI(URI: String)
companion object {
// Used to load the 'native-lib' library on application startup.
init {
System.loadLibrary("native-lib")
}
}
fun getSong() : MutableList<Songs> {
val SONGS_PROJECTION = arrayOf(
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA
)
val cursor = contentResolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
SONGS_PROJECTION,
null,
null,
MediaStore.Audio.Media._ID +
" ASC LIMIT 100"
)
val items: MutableList<Songs> = mutableListOf()
cursor?.let {
if (cursor.count > 0) {
cursor.moveToFirst()
while (!cursor.isAfterLast) {
val s0 = cursor.getString(cursor.getColumnIndex(SONGS_PROJECTION[0]))
val s1 = cursor.getString(cursor.getColumnIndex(SONGS_PROJECTION[1]))
val s2 = cursor.getString(cursor.getColumnIndex(SONGS_PROJECTION[2]))
items.add(Songs(s0, s1, s2))
cursor.moveToNext()
}
}
cursor.close()
}
return items
}
}
在MainActivity
中,我将路径传递给本机端,第二次传递 URI,每次都没有运气。
#include <jni.h>
#include <string>
#include <media/NdkMediaExtractor.h>
#include <android/log.h>
#include <bitset>
#define APP_NAME "MediaStoreToNativeAudio"
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, APP_NAME, __VA_ARGS__))
extern "C" JNIEXPORT void JNICALL
Java_com_example_mediastoretonativeaudio_MainActivity_stringToJNI(
JNIEnv *env,
jobject jobj,
jstring URI) {
const char *uri = env->GetStringUTFChars(URI, NULL);
std::string s(uri);
AMediaExtractor *extractor = AMediaExtractor_new();
media_status_t amresult = AMediaExtractor_setDataSource(extractor, uri);
if (amresult != AMEDIA_OK) {
LOGE("AMediaExtractor_setDataSource called with: [%s]", s.c_str());
LOGE("Error setting extractor data source, err %d", amresult);
}
return;
}
从日志:
2019-11-20 01:09:03.519 8270-8270/com.example.mediastoretonativeaudio E/NdkMediaExtractor: can't create http service
2019-11-20 01:09:03.519 8270-8270/com.example.mediastoretonativeaudio E/MediaStoreToNativeAudio: AMediaExtractor_setDataSource called with: [file:///storage/emulated/0/Music/Thank you for the drum machine/01 - Everything Moves.mp3]
2019-11-20 01:09:03.519 8270-8270/com.example.mediastoretonativeaudio E/MediaStoreToNativeAudio: Error setting extractor data source, err -10002
2019-11-20 01:09:03.543 8270-8270/com.example.mediastoretonativeaudio E/NdkMediaExtractor: can't create http service
2019-11-20 01:09:03.543 8270-8270/com.example.mediastoretonativeaudio E/MediaStoreToNativeAudio: AMediaExtractor_setDataSource called with: [content://media/external/audio/media/472]
2019-11-20 01:09:03.543 8270-8270/com.example.mediastoretonativeaudio E/MediaStoreToNativeAudio: Error setting extractor data source, err -10002
我的清单:
安装应用程序后,我还通过设置授予权限。
编辑:
带有测试代码的公共存储库:https ://github.com/AndrewBloom/MediaStoreToNativeAudioSample
相同的行为结果使用:
android:requestLegacyExternalStorage="true"