如何在android应用程序中显示所需视频id的youtube视频,以便在服务器上设置id,以便稍后更改id?

qpgpyjmq  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(326)

我对java和androidstudio是新手。
我目前正在使用android studio,我可以从视频id加载youtube视频,但是在我的应用程序中,我不能保持视频id静态,我需要经常更改id,而不需要用户下载应用程序更新。我尝试过firebase实时数据库和远程配置,但在这两种情况下,视频都会延迟,因为它们是异步下载数据的。这个问题的最佳解决方案是什么?
谢谢你提前帮忙。
以下是我的youtube视频片段:

package shah.vatsal.kinitro

public class youtubefragment extends Fragment {

private static final String API_KEY = 
"AIzaSyAXjBSS9NVAewJ2Z0Z86JrlHsJbJzoP_Ns";

private static String VIDEO_ID = "mMMerxh_12U";

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.youtube_videos, container, 
false);

    YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.youtube_layout, youTubePlayerFragment).commit();

    youTubePlayerFragment.initialize(API_KEY, new OnInitializedListener() {
        @Override
        public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
            if (!wasRestored) {
                player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
                player.loadVideo(VIDEO_ID);
                player.play();
            }
        }
        @Override
        public void onInitializationFailure(Provider provider, YouTubeInitializationResult error) {
            // YouTube error
            String errorMessage = error.toString();
            Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
            Log.d("errorMessage:", errorMessage);
        }
    });
    return v;
}
}

youtube布局:

<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/youtube_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<FrameLayout
    android:id="@+id/youtube_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
hts6caw3

hts6caw31#

我肯定我的建议跟不上你想走的路,但是你可以很容易地通过设置一个网页来实现这一点,在网页中放置视频,然后从网络视图中加载视频,这样你就可以简单地更改网页,并且如果你没有设置缓存系统,或者你只是清除了它,那么下次用户运行应用程序时,视频就会被更新。
这比尝试处理firebase开发环境(我发现这很复杂)简单,但是如果页面没有得到很好的优化,在加载页面时可能会遇到一些延迟;不过,你现在可以通过简单地设置tumblr空间来解决这个问题,而且你还可以不时地更改提供的链接来展示不同的视频。
即使你想在活动中显示网页视图之外的其他元素,你也可以做一些限制,或者只在网页上插入你喜欢的内容(这主要取决于你的实际需要)。
这里有一个kotlinwebview示例,以防您不熟悉它,您可以在这里了解有关webview的更多信息。

相关问题