android Youtube Iframe API,视频中的视觉和声音不一致

bq3bfh9z  于 2023-04-18  发布在  Android
关注(0)|答案(1)|浏览(111)

我有一个活动,其中有视频缩略图的列表.当我点击视频缩略图我在屏幕中间创建这个片段.我发送视频id片段.当片段打开,我点击视频启动它.声音开始来,但图像似乎加载并开始有点晚.由于声音不停止,图片和声音不同步。在此之前我使用pierfrancescosoffritti库,结果是一样的。可能是什么问题?

public class YoutubeVideoFragment extends BaseFragment<YoutubeVideoPresenter> implements YoutubeVideoContract.View {

    public static YoutubeVideoFragment newInstance(String videoId) {

        Bundle args = new Bundle();
        args.putString("ARG_VIDEO_ID", videoId);
        YoutubeVideoFragment fragment = new YoutubeVideoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @BindView(R.id.root_view)
    LinearLayout root_view;
    @BindView(R.id.webView)
    WebView ytWebView;

    private String videoId;

  

    @Override
    public void readBundle(Bundle bundle) {

        if (bundle != null) {
            videoId = bundle.getString("ARG_VIDEO_ID");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = onCustomCreateView(inflater, container, savedInstanceState, R.layout.fragment_youtube_video);

        WebSettings webSettings = ytWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);

        String html = getHtmlString(videoId);
        ytWebView.loadDataWithBaseURL("https://www.youtube.com/", html, "text/html", "UTF-8", null);

        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
      
        if (ytWebView != null){
            ytWebView.destroy();
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
       
        if (ytWebView != null){
            ytWebView.destroy();
        }

    }
    private String getHtmlString(String videoId) {
        return "<html><body>" +
                "<div id=\"player\"></div>" +
                "<script>" +
                "   var tag = document.createElement('script');" +
                "   tag.src = \"https://www.youtube.com/iframe_api\";" +
                "   var firstScriptTag = document.getElementsByTagName('script')[0];" +
                "   firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);" +
                "   var player;" +
                "   function onYouTubeIframeAPIReady() {" +
                "       player = new YT.Player('player', {" +
                "           height: 'wrap-content'," +
                "           width: '100%'," +
                "           videoId: '" + videoId + "'," +
                "           playerVars: {" +
                "               'playsinline': 1," +
                "               'rel': 0" +
                "           }," +
                "           events: {" +
                "               'onReady': onPlayerReady," +
                "               'onStateChange': onPlayerStateChange" +
                "           }" +
                "       });" +
                "   }" +
                "   function onPlayerReady(event) {" +
                "       event.target.playVideo();" +
                "   }" +
                "   var done = false;" +
                "   function onPlayerStateChange(event) {" +
                "       if (event.data == YT.PlayerState.PLAYING && !done) {" +
                "           setTimeout(stopVideo, 6000);" +
                "           done = true;" +
                "       }" +
                "   }" +
                "   function stopVideo() {" +
                "       player.stopVideo();" +
                "   }" +
                "</script>" +
                "</body></html>";
    }
}```
tjrkku2a

tjrkku2a1#

问题出在模拟器上,我在真实的设备上试用时没有遇到这样的问题。

相关问题