如何在点击播放视频在Android的?如何在、视频、Android

2023-09-07 01:58:24 作者:醉凡尘

我想打从远程URL和放视频;它工作正常,现在的问题是,视频自动开始播放,我希望视频出现像YouTube视频,在中心与一个播放按钮;在点击该视频应该发挥。

I tried playing a Video from a remote URL & it works fine , the problem now is that the video starts playing automatically, I want the video to show up like a youtube video with a play button in center & on clicking that the video should play .

public class Tabs extends Activity {
TabHost th;
VideoView video;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //Create tabs using XML 

    video=(VideoView) findViewById(R.id.VideoView);
    String path1="http://www.w3schools.com/html5/movie.mp4";
    MediaController mc = new MediaController(this);
    mc.setAnchorView(video);
    mc.setMediaPlayer(video);
    Uri uri=Uri.parse(path1);
    video.setMediaController(mc);
    video.setVideoURI(uri);
    video.start();
}
}

下面是我的code的工作完全正常,但视频将自动开始播放,我需要它只是显示的第一个场景,如YouTube视频,在中心与一个播放按钮;上点击该视频应该开始播放。此外,它应显示视频的缩略图

Here is my code which works perfectly fine , but the video automatically starts playing , i need it to just show the first scene like youtube video with a play button in center & on clicking that the video should start playing. Also it should show a thumbnail of the video

任何帮助是非常AP preciated

Any help is much appreciated

感谢很多提前。

推荐答案

设置视频播放器和网址在的onCreate 和实施 onClickListener 。

Setup the video player and URLs in the onCreate and do the actual playing by implementing onClickListener.

在你的榜样(记得要导入丢失的包,并与ID创建一个按钮 buttonStart ):

In your example (remember to import missing packages and create a button with ID of buttonStart):

public class Test extends Activity {
    TabHost th;
    VideoView video;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Create tabs using XML

        video = (VideoView) findViewById(R.id.VideoView);
        String path1 = "http://www.w3schools.com/html5/movie.mp4";
        MediaController mc = new MediaController(this);
        mc.setAnchorView(video);
        mc.setMediaPlayer(video);
        Uri uri = Uri.parse(path1);
        video.setMediaController(mc);
        video.setVideoURI(uri);
        Button buttonStart = (Button) findViewById(R.id.buttonStart);
        buttonStart.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                video.start();
            }

        });

    }
}

至于显示缩略图,如果你有它的URL,就可以启动一个异步任务,将检索远程图像和覆盖它在视频播放器(使用的FrameLayout 实现覆盖查看)。然后设置一个onClickListener是叠加图像上(见它是如何为一个按钮来完成的code,同样的逻辑可以遵循一个的ImageView )。只要确保你设置的ImageView 是点击。

As for displaying the thumbnail, if you have its URL, you can start an Async task that will retrieve the remote image and overlay it over the video player (use FrameLayout to achieve overlaying Views). Then setup an onClickListener on the overlaying image (see the code on how it's done for a button, the same logic can be followed for an ImageView). Just make sure you set the ImageView to be clickable.