Skip to content

Commit

Permalink
feat(android): create player by name
Browse files Browse the repository at this point in the history
  • Loading branch information
I-m-SuperMan authored and pingkai committed Aug 4, 2020
1 parent 95c2ad9 commit 07c2456
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ public static CicadaPlayer createCicadaPlayer(Context context) {
return createCicadaPlayer(context, null);
}

/**
* 创建播放器
*
* @param context 上下文。
* @param name 外部播放器的名字
* @return 播放器对象。
*/
/****
* Create a player.
*
* @param context The context.
* @param name external player name
* @return The player object.
*/
public static CicadaPlayer createCicadaPlayer(Context context , String name) {
return createCicadaPlayer(context, name , null);
}

/**
* 创建播放器
*
Expand All @@ -59,8 +77,8 @@ public static CicadaPlayer createCicadaPlayer(Context context) {
* @param traceId A trace ID for troubleshooting with the relevant log.
* @return The player object.
*/
public static CicadaPlayer createCicadaPlayer(Context context, String traceId) {
return new CicadaPlayerImpl(context, traceId);
public static CicadaPlayerImpl createCicadaPlayer(Context context, String name, String traceId) {
return new CicadaPlayerImpl(context, name , traceId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,10 @@ private void onVideoRendered(long timeMs, long pts){
}
};

public CicadaPlayerImpl(Context context, String traceID) {
public CicadaPlayerImpl(Context context, String name, String traceID) {
mContext = context;
mTraceID = traceID;
mCorePlayer = new NativePlayerBase(context);
mCorePlayer = new NativePlayerBase(context , name);
mCorePlayer.setTraceId(mTraceID);

bindListeners();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected void setNativeContext(long l) {
mNativeContext = l;
}

public NativePlayerBase(Context context) {
public NativePlayerBase(Context context, String name) {

mContext = context;

Expand All @@ -115,7 +115,7 @@ public NativePlayerBase(Context context) {
//TODO Later : 线程的情况下回调的问题。
mCurrentThreadHandler = new MainHandler(this, Looper.getMainLooper());

construct(context);
construct(context , name);
}

private static String getUserNativeLibPath(Context context) {
Expand All @@ -139,8 +139,8 @@ private static String getUserNativeLibPath(Context context) {
return userPath;
}

private void construct(Context context) {
nConstruct();
private void construct(Context context, String name) {
nConstruct(name);
if (context != null) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
nSetConnectivityManager(connectivityManager);
Expand Down Expand Up @@ -458,7 +458,7 @@ public static void setBlackType(int type) {

////===============-------------------==================------------------////

protected native void nConstruct();
protected native void nConstruct(String name);

protected native void nSetConnectivityManager(Object connectManager);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <utils/Android/GetStringUTFChars.h>
#include <utils/Android/JniException.h>
#include <utils/Android/FindClass.h>
#include <utils/CicadaJSON.h>


#include "NativeBase.h"
Expand Down Expand Up @@ -57,12 +58,21 @@ jmethodID gj_NativePlayer_onBufferPositionUpdate = nullptr;
jmethodID gj_NativePlayer_onCurrentPositionUpdate = nullptr;
jmethodID gj_NativePlayer_onSubtitleExtAdded = nullptr;

void NativeBase::java_Construct(JNIEnv *env, jobject instance)
void NativeBase::java_Construct(JNIEnv *env, jobject instance , jstring name)
{
AF_TRACE;
PlayerPrivateData *privateData = new PlayerPrivateData();
privateData->j_instance = env->NewGlobalRef(instance);
privateData->player = new MediaPlayer();

if(name != nullptr) {
GetStringUTFChars jName(env, name);
CicadaJSONItem opts{};
opts.addValue("name", jName.getChars());
privateData->player = new MediaPlayer(opts.printJSON().c_str());
}else {
privateData->player = new MediaPlayer();
}

env->CallVoidMethod(instance, gj_NativePlayer_setNativeContext, (jlong) privateData);
JniException::clearException(env);
jobject userData = privateData->j_instance;
Expand Down Expand Up @@ -936,7 +946,7 @@ void NativeBase::unInit(JNIEnv *pEnv)
}

static JNINativeMethod nativePlayer_method_table[] = {
{"nConstruct", "()V", (void *) NativeBase::java_Construct},
{"nConstruct", "(Ljava/lang/String;)V", (void *) NativeBase::java_Construct},
{"nSetConnectivityManager", "(Ljava/lang/Object;)V", (void *) NativeBase::java_SetConnectivityManager},
{"nEnableHardwareDecoder", "(Z)V", (void *) NativeBase::java_EnableHardwareDecoder},
{"nSetSurface", "(Landroid/view/Surface;)V", (void *) NativeBase::java_SetView},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class NativeBase {


public:
static void java_Construct(JNIEnv *env, jobject instance);
static void java_Construct(JNIEnv *env, jobject instance, jstring name);

static void java_SetConnectivityManager(JNIEnv *env, jobject instance, jobject connectManager);

Expand Down

0 comments on commit 07c2456

Please sign in to comment.