【问题标题】:How to play youtube video upon click of an image?点击图片后如何播放 youtube 视频?
【发布时间】:2016-03-22 20:29:13
【问题描述】:

我正在开发一个 Android 上的应用程序,我想通过单击图像来播放 youtube 视频。我不想移动到不同的活动或屏幕。

另外,除了分配给图像的空间之外,我不希望视频占用任何额外空间。

有人可以提出任何解决方案吗?我没有找到任何关于 Android 的帖子。

【问题讨论】:

    标签: android android-layout youtube-api


    【解决方案1】:

    假设在 OnClickListener 中,你捕获了点击事件:

    1) 你可以通过调用setVisibility(View.GONE) 使图像不可见。

    2) 您将拥有一个扩展 YouTubePlayerSupportFragmentVideoFragment 类,并且它将包含在一个不可见的容器中,因此您可以调用

    VideoFragment videoFragment = (VideoFragment) getFragmentManager().findFragmentById(R.id.video_fragment_container);
    videoFragment.setVideoId(videoId);
    container.setVisibility(View.VISIBLE);
    

    详情请看the sample applications

    【讨论】:

    • 。感谢您的回答。那么,您是否建议我可以重叠视图?当我点击它并出现视频时,我可以有一个消失的图像吗?并且视频适合相同的图像空间?
    【解决方案2】:

    感谢您的帮助。这对我的应用程序很有效。让我解释一下我最终是如何做到的。为此,我们需要相对布局,我们可以在其中重叠视图。我已经重叠了 ImageView 和 youtubeView。最初,我将 ImageView 设置为可见,将 youtubeView 设置为不可见。在 ImageView 的 View.onClickListener 中,我将 ImageView 设为 INVISIBLE,将 youtubeView 设为 VISIBLE。这对我很有效。

    注意:除非您想创建自己的自定义布局,否则只能在 RelativeLayout 中而不是在任何其他布局中重叠视图。

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/listPreferredItemHeight"
            android:paddingStart="@dimen/activity_horizontal_margin_less"
            android:paddingRight="@dimen/activity_horizontal_margin_less"
            android:paddingTop="@dimen/activity_vertical_margin_less"
            android:paddingBottom="@dimen/activity_vertical_margin_less"
            tools:context="com.example.puneet.movieout.MovieInfoDisplay"
            >
    
    
            <TextView
                android:layout_width="wrap_content"
                android:id="@+id/textView2"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:text="I am Puneet Chugh. I am Puneet Chugh"
                android:layout_centerHorizontal="true"
                android:textSize="24sp"
                android:textStyle="bold"
                android:textColor="@color/black"/>
    
            <com.google.android.youtube.player.YouTubePlayerView
                android:id="@+id/youtube_view"
                android:layout_below="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
    
            />
            <ImageView
                android:layout_marginTop="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:id="@+id/imageView"
                android:layout_below="@+id/textView2"/>
        </RelativeLayout>
    

    活动部分:

        youTubeView.setVisibility(View.INVISIBLE);
    
        moviePoster.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                moviePoster.setVisibility(View.INVISIBLE);
                youTubeView.setVisibility(View.VISIBLE);
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2017-07-31
      • 2021-10-30
      • 1970-01-01
      • 2023-03-30
      • 2016-12-16
      • 2016-02-12
      • 2011-07-13
      • 2016-06-17
      • 1970-01-01
      相关资源
      最近更新 更多