我正在尝试在 react native 上使用本机模块,并且我想在 android 本机 mediaplayer 中播放已准备好的视频(mediaplayer.prepare())。然后将媒体播放器添加到表面视图,并将表面视图添加到 Adview(见下文)。实施此操作后,我看不到视频,只有音频是可听的
在 App.js 中查看
<AdView adType = {adFormat} adspotKey = {adspotId} />
视图管理器中的代码
package com.adsandroidreactsdk;
import android.view.View;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.bridge.ReactMethod;
import android.util.Log;
import androidx.annotation.Nullable;
import java.util.Map;
public class AdViewManager extends SimpleViewManager<AdViewGroup> {
@Override
public String getName() {
return "RCTNativeView";
}
@ReactProp(name = "adType")
public void setAdspotKey(AdViewGroup view, @Nullable int adType) {
view.setAdType(adType);
}
@ReactProp(name = "adspotKey")
public void setAdspotKey(AdViewGroup view, @Nullable String adspot) {
view.setAdspotId(adspot);
}
@ReactProp(name = "adHeight")
public void setHeight(AdViewGroup view, @Nullable int adHeight) {
view.setHeight(adHeight);
}
@ReactProp(name = "adWidth")
public void setWidth(AdViewGroup view, @Nullable int adWidth) {
view.setWidth(adWidth);
}
@Override
protected AdViewGroup createViewInstance(ThemedReactContext reactContext) {
return new AdViewGroup(reactContext);
}
}
AdViewGroup 类
public class AdViewGroup extends ReactViewGroup implements LifecycleEventListener {
protected AdViewLayout mAdView;
private AdView.AD_TYPE mAdType;
private String mAdspotKey;
private RCTEventEmitter mEventEmitter;
int pxW, mWidth;
int pxH, mHeight;
FrameLayout mIcon;
RelativeLayout mMedia;
ThemedReactContext context;
public void setAdType(int adType) {
Log.e("Test", "inside setAdType: " + adType);
if (adType == 1)
mAdType = AdView.AD_TYPE.INFEED;
else if (adType == 2)
mAdType = AdView.AD_TYPE.CONTENT_STREAM;
else if (adType == 3)
mAdType = AdView.AD_TYPE.CUSTOM_NATIVE;
else if (adType == 4)
mAdType = AdView.AD_TYPE.INSTREAM_VIDEO;
if (mAdType != null && !TextUtils.isEmpty(mAdspotKey))
createAdViewIfCan();
}
public void setHeight(int height) {
Log.e("Test", "inside setHeight: " + height);
mHeight = height;
}
public void setWidth(int width) {
Log.e("Test", "inside setWidth: " + width);
mWidth = width;
}
public void setAdspotId(String adspotKey) {
Log.e("Test", "inside setAdspotId: " + adspotKey);
mAdspotKey = adspotKey;
if (mAdType != null && !TextUtils.isEmpty(mAdspotKey))
createAdViewIfCan();
}
private int dp2px(int dp, DisplayMetrics dm) {
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, dm));
}
private void createAdViewIfCan() {
if (mAdView == null && mAdspotKey != null) {
context = (ThemedReactContext) getContext();
if (context.hasCurrentActivity()) {
Ads.getInstance().setLogLevel(Ads.LogLevel.DEBUG);
mAdView = new AdViewLayout(context.getCurrentActivity(), mAdspotKey, mAdType);
DisplayMetrics dm = context.getResources().getDisplayMetrics();
pxW = dp2px(mWidth, dm); //Put width of required adView
pxH = dp2px(mHeight, dm); //Put height of required adView
Log.e("Test", "pxW: " + pxW);
Log.e("Test", "pxH: " + pxH);
mAdView.measure(pxW, pxH);
mAdView.layout(0, 0, pxW, pxH);
removeAllViews();
addView(mAdView, new FrameLayout.LayoutParams(pxW, pxH));
setListenerToAdview(context.getCurrentActivity());
if (mAdType == AdView.AD_TYPE.CUSTOM_NATIVE) {
RelativeLayout container = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout._custom_native_layout, null);
mIcon = (FrameLayout) container.findViewById(context.getResources().getIdentifier("_custom_icon", "id",
context.getPackageName()));
mMedia = (RelativeLayout) container.findViewById(context.getResources().getIdentifier("_custom_media_view", "id",
context.getPackageName()));
TextView descView = (TextView) container.findViewById(context.getResources().getIdentifier("_custom_desc", "id",
context.getPackageName()));
TextView titleView = (TextView) container.findViewById(context.getResources().getIdentifier("_tv_title", "id",
context.getPackageName()));
LayoutParams params = descView.getLayoutParams();
LayoutParams params1 = mIcon.getLayoutParams();
params.width = dm.widthPixels - params1.width;
descView.setLayoutParams(params);
titleView.setLayoutParams(params);
mAdView.setCustomNativeAdContainer(container);
mAdView.setCustomImageSize(320, 180);
}
mAdView.cacheAd();
}
}
}
public AdViewGroup(ThemedReactContext context) {
super(context);
context.addLifecycleEventListener(this);
mEventEmitter = context.getJSModule(RCTEventEmitter.class);
}
@Override
public void onHostResume() {
}
@Override
public void onHostPause() {
}
@Override
public void onHostDestroy() {
if (mAdView != null) {
mAdView.onDestroy();
}
}
public void setListenerToAdview(final Activity activity) {
mAdView.setAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(AdView adview, AdError AdError) {
Log.d("Test", "onAdFailedToLoad callback." + AdError.getErrorDescription());
Toast.makeText(context, "" + AdError.getErrorDescription(), Toast.LENGTH_SHORT).show();
}
@Override
public void onAdClosed(AdView adview, boolean isVideoCompleted) {
Log.d("Test", "onAdClosed callback=> " + "isVideoCompleted: " + isVideoCompleted);
Toast.makeText(context, "onAdClosed", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdPrepared(AdView adView) {
Log.d("Test", "onAdPrepared callback");
Toast.makeText(context, "onAdPrepared", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdReceived(AdView AdView) {
Log.d("Test", "onAdReceived callback");
Toast.makeText(context, "onAdReceived", Toast.LENGTH_SHORT).show();
mAdView.loadAd();
}
@Override
public void onAdClicked(AdView adview) {
Log.d("Test", "onAdClicked callback");
Toast.makeText(context, "onAdClicked", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdRender(AdView adview) {
Log.d("Test", "onAdRender callback");
Toast.makeText(context, "onAdRender", Toast.LENGTH_SHORT).show();
measureView(mIcon, 48, 48);
measureView(mMedia, 320, 200);
}
@Override
public void onAdMediaStart(AdView adview) {
Log.d("Test", "onAdMediaStart callback");
Toast.makeText(context, "onAdMediaStart", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdMediaEnd(AdView adview, long l) {
Log.d("Test", "onAdMediaEnd callback");
Toast.makeText(context, "onAdMediaEnd", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdRefresh(AdView adview) {
Log.d("Test", "onAdRefresh callback");
Toast.makeText(context, "onAdRefresh", Toast.LENGTH_SHORT).show();
measureView(mIcon, 48, 48);
measureView(mMedia, 320, 200);
}
@Override
public void onAdMediaExpand(AdView adview) {
Log.d("Test", "onAdMediaExpand callback");
Toast.makeText(context, "onAdMediaExpand", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdMediaCollapse(AdView adview) {
Log.d("Test", "onAdMediaCollapse callback");
Toast.makeText(context, "onAdMediaCollapse", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdSkippable(AdView AdView) {
Log.d("Test", "onAdSkippable callback");
Toast.makeText(context, "onAdSkippable", Toast.LENGTH_SHORT).show();
}
});
}
private void measureView(ViewGroup viewGroup, int width/*in DP*/, int height/*in DP*/) {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
View child = null;
if (viewGroup != null) {
child = viewGroup.getChildAt(0);
}
if (child != null) {
Log.e("Test", " width: " + dp2px(width, dm));
Log.e("Test", " height: " + dp2px(height, dm));
child.measure(dp2px(width, dm), dp2px(height, dm));
child.layout(0, 0, dp2px(width, dm), dp2px(height, dm));
}
}
}
AdViewPackage 类
class AdViewPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return new ArrayList<>();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Arrays.<ViewManager>asList(new AdViewManager());
}
}