Play video using FFmpeg library in androidNDK

Viewed 253

I have all required .so libraries and header files to play a video. I want to play video from the Raw folder. I am struck at CPP code. Unable to pass a video file path to CPP code. The following is my native-lib.cpp code

 const char *file = env->GetStringUTFChars(path, 0);
if (file == NULL) {
    printf("The file is a null object.");
}

av_register_all();
int ret = 0;
char errbuf[256];
AVFormatContext *fmt_ctx = avformat_alloc_context();
int ret12 = avformat_open_input(&fmt_ctx, file, NULL, NULL);
if (ret12 < 0) {
    av_strerror(ret, errbuf, sizeof(errbuf));
    __android_log_print(ANDROID_LOG_ERROR, "ffmpeg", "%s", errbuf);
    __android_log_print(ANDROID_LOG_ERROR, "ffmpeg", "%i", ret12);
    return;
}

The above ret12 returning -ve value and it is returning the control out

1 Answers

Not exactly the problem you are facing right now, but when you use GetStringUTFChars(), you must call ReleaseStringUTFChars() before you return from your native method to Java.

Now, when you pass a file path to JNI, is it a full path, or relative path? In Android app, the 'current directory' ate the beginning is /, the root. So, it could be that your code cannot find the file.

The next question is, whether your app has enough permissions to open this file. If the file is on the external storage, outside of your app's private folder, you need runtime READ_EXTERNAL_STORAGE permission to read it. Furthermore, on Android 10 there are further restrictions. You may need android:requestLegacyExternalStorage="true" attribute in your app AndroidManifest.xml.

Related