2

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 Answers1

0

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.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • This is the path passing as argument native code File(Uri.parse("android.resource://"+packageName+"/" + R.raw.samplevideo).path).path – manikanta veeravalli May 13 '20 at 07:06
  • You actually can access resources bundled into your APK, but this is [not easy](https://stackoverflow.com/a/25560443/192373). Usually, we extract the resource from the APK in Java and put it in a (temporary) file, and then pass the full path for this file to native method. – Alex Cohn May 13 '20 at 07:45
  • Any example to pass the full path of a Raw file – manikanta veeravalli May 13 '20 at 11:33
  • Your samplevideo is not a raw file; it's a resource (blob) packaged into the APK bundle. It doesn't have a path. – Alex Cohn May 13 '20 at 12:19