Today, we have successfully transplanted our self-developed ffmpeg-based player to Android and successfully tested it on the tablet. According to the ideas provided in this article, we can port all ffmpeg-based players to Android.
Preparations:
The compilation methods of ffmpeg and SDL are described in the previous article.
During development, the two compiled library files must be libffmpeg. so and libSDL. so, put it under <ndk-path>/platforms/android-8/arch-arm/usr/lib/. During debugging and running, you need to use adb to push to the/system/lib/directory of Android.
In this example, the player is https://github.com/pkuembedded/tvplayer. This player was developed by our lab and is still being updated, but basic playback functions are available. Readers can transplant other powerful open-source players, including self-developed players, based on their own interests.
Step 1: Create an Android project in eclipse.
To distinguish it from the TVplayer of PC Linux, the project name is TVplayer_Android. The package name is com. player and the class name is TVplayer.
When creating a new project, you must select the Android and ndk and sdk versions consistent with ffmpeg and SDL.
Step 2: import the player to the project.
Create jni/src in the TVplayer_Android directory. Copy all c files and H files in src under TVplayer to TVplayer_Android/jni/src to prepare for NDK compilation.
Create TVplayer_Android/jni/Android. mk. Content:
LOCAL_PATH: = $ (call my-dir)/src
Include $ (CLEAR_VARS)
LOCAL_MODULE: = TVplayer
LOCAL_SRC_FILES: = \
Audio. c \
Display. c \
File. c \
Queue. c \
Sync. c \
TVplayer. c \
Video. c
LOCAL_C_INCLUDES + =/home/baby/workspace/SDL-1.3.0-6050/include/
LOCAL_C_INCLUDES + =/home/baby/workspace/avs/ffmpeg-0.8.7/
LOCAL_LDLIBS + =-L $ (SYSROOT)/usr/lib-lffmpeg-lSDL-llog
Include $ (BUILD_SHARED_LIBRARY)
Where:
Add the source files to be compiled to the end of the line starting with LOCAL_SRC_FILES.
The two lines starting with LOCAL_C_INCLUDES are used to contain the header file of the c library. Add the directory of your ffmpeg and SDL header files here.
Add-llog to the line starting with LOCAL_LDLIBS for debugging.
Step 3: Implement java classes.
To enable Android to support SDL, The SDLActivity. java file in the SDL source file is required. Copy SDLActivity. java to the same directory as our own TVplayer. java.
Change TVplayer. java:
Public class TVplayer extends SDLActivity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
}
}
The method used here is to use your own class to inherit the SDLActivity class and call the parent class method in the onCreate method. For simplicity, you can directly change the SDLActivity class to TVplayer. In order to add other classes in the future, this method is not too bloated. SDLActivity. java needs to be slightly modified: Comment out all the original System. loadLibrary functions, and then add System. loadLibrary ("TVplayer") in the original position. The result is as follows:
Static {
// System. loadLibrary ("SDL ");
// System. loadLibrary ("SDL_image ");
// System. loadLibrary ("SDL_mixer ");
// System. loadLibrary ("SDL_ttf ");
// System. loadLibrary ("main ");
System. loadLibrary ("TVplayer ");
}
Step 4: Modify the c file. Www.2cto.com
The TVplayer_Android/jni/src/TVplayer. c file must also be modified:
Implement native functions in java as follows:
Void Java_com_player_SDLActivity_nativeInit (JNIEnv * env, jclass cls, jobject obj ){
/* This interface cocould expand with ABI negotiation, calbacks, etc .*/
SDL_Android_Init (env, cls );
/* Run the application code! */
Int status;
Char * argv [2];
Argv [0] = strdup ("SDL_app ");
Argv [1] = NULL;
// Status = main (1, argv );
Status = main (2, argv );
/* We exit here for consistency with other platforms .*/
Exit (status );
// Java_org_libsdl_app_SDLActivity_nativeInit (env, cls, obj );
}
Void Java_com_player_SDLActivity_nativeQuit (JNIEnv * env, jclass cls, jobject obj ){
Java_org_libsdl_app_SDLActivity_nativeQuit (env, cls, obj );
}
Void Java_com_player_SDLActivity_onNativeResize (JNIEnv * env, jclass cls, jobject obj, int x, int y, int format ){
Java_org_libsdl_app_SDLActivity_onNativeResize (env, cls, obj, x, y, format );
}
Void Java_com_player_SDLActivity_onNativeAccel (JNIEnv * env, jclass cls, jobject obj, float x, float y, float z ){
Java_org_libsdl_app_SDLActivity_onNativeAccel (env, cls, obj, x, y, z );
}
The implementation method is to call the original implementation method of SDL. If you have run the previous example of migrating SDL to understand the entire process, you can find these functions in SDL.
After these functions are added, java will call the initialization method designed for Android by SDL, and delete the SDL_Init function called in the original main function.
Step 5: Compile and run:
First, generate libTVplayer. so in the ndk-build under the project directory.
Then compile and run in eclipse.
From the BABY Column