Ответ 1
У меня нет фактического решения для фрагмента YouTube, но я использовал некоторое обходное решение для отображения вида YouTube в RN.
(Возможно, сначала вы можете попробовать взломать 3.
в своем фрагменте.)
-
Создайте свой собственный
ReactActivity
, который расширяетYouTubeBaseActivity
, и пустьMainActivity
расширяет его.public abstract class ReactActivity extends YouTubeBaseActivity implements DefaultHardwareBackBtnHandler { // copy & paste RN ReactActivity.java source
-
Сделайте
YoutubeManager
класс дляYouTubePlayerView
.Он должен взять экземпляр
Activity
изcreateViewManagers
, когда вы реализуетеReactPackage
.public class YoutubeManager extends SimpleViewManager<YouTubePlayerView> implements YouTubePlayer.OnInitializedListener, YouTubePlayer.PlayerStateChangeListener, YouTubePlayer.PlaybackEventListener { public static final String REACT_CLASS = "RCTYoutube"; private static final String YOUTUBE_API_KEY = "<YOUR_OWN_KEY>"; private YouTubeBaseActivity mActivity = null; private YouTubePlayerView mYouTubePlayerView = null; public YoutubeManager(Activity activity) { super(); mActivity = (YouTubeBaseActivity) activity; } @Override public String getName() { return REACT_CLASS; } @Override public YouTubePlayerView createViewInstance(ThemedReactContext context) { mContext = context; mYouTubePlayerView = new YouTubePlayerView(mActivity); mYouTubePlayerView.initialize(YOUTUBE_API_KEY, this); return mYouTubePlayerView; }
и создайте для него модуль js.
module.exports = requireNativeComponent('RCTYoutube', iface);
-
Теперь вы можете отображать вид на YouTube, но он кажется пустым. Вам нужно немного взломать его.
componentDidMount() { this.setState({ width: this.props.style.width, height: this.props.style.height - 1 }) this.timer = setInterval(()=>{ this.setState({ width: this.props.style.width, height: this.props.style.height + Math.floor(Math.random() * 2) }) }, 500) } render() { return (<YoutubeView style={[style, { width: this.state.width, height: this.state.height }]} />) } componentWillUnmount() { if(this.timer) clearTimeout(this.timer); }
Вы можете увидеть, как это работает на самом деле из моего приложения.
https://play.google.com/store/apps/details?id=kr.dobbit.sharehows