如何播放视频的onCreate方法? [英] How to Play Video on OnCreate Method?

查看:129
本文介绍了如何播放视频的onCreate方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何播放视频的onCreate方法?我曾经按照code为that.But的视频不是Played.If我用点击按钮事件这个code,那么这是合作。

How to Play Video on OnCreate Method? I Used Following code for that.But The Video is Not Played.If i used this Code on Click Event of Button then This is Worked.

我的onCreate方法: -

My OnCreate Method:-

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);      
    getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setFixedSize(176, 144);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mediaPlayer = new MediaPlayer();
    mediaPlayer.isLooping();
    playvideo();
}

我的playVideo功能: -

My PlayVideo Function:-

public void playvideo() {
    String stringPath = "/sdcard/video.mp4";

    if (mediaPlayer.isPlaying()) {
        mediaPlayer.reset();
    }

    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setDisplay(surfaceHolder);

    try {
        mediaPlayer.setDataSource(stringPath);
        mediaPlayer.prepare();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    mediaPlayer.start();
}

先谢谢了。

推荐答案

您不能起到的onCreate视频,因为打你需要一个面与面的时候,你曾呼吁的playVideo尚未创建视频内容()。

You cannot play the video in onCreate, because to play video content you need a surface and Surface has not been created when you had called playVideo().

要获取的视频播放工作,你需要做以下

To get video playback working, you need to do the following


  1. 实施您的活动SurfaceHolder回调。与surfaceholder注册类(surfaceHolder.addCallback(本);)

  2. 在创建表面,surfaceCreated()被调用,在那里你会得到表面持有者的一个实例。现在,你可以调用的playVideo(与surfaceHolder的实例)。里面的playVideo()得到的表面,放在媒体播放器实例。

Shash

这篇关于如何播放视频的onCreate方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆