Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6178951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:33:41+00:00 2026-05-24T00:33:41+00:00

How to Play Video on OnCreate Method? I Used Following code for that.But The

  • 0

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.

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();
}

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();
}

Thanks in Advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:33:42+00:00Added an answer on May 24, 2026 at 12:33 am

    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. Implement SurfaceHolder callbacks in your activity. Register your class with surfaceholder (surfaceHolder.addCallback(this); )
    2. When the surface is created, surfaceCreated() gets called, where you ll get an instance of surface holder. Now you can call playVideo(with the instance of surfaceHolder). Inside playVideo() get the surface and set it on media player instance.

    Shash

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that needs to play video but not allow downloading. I'd
I want to play video in WP7. This is my code: MediaPlayerLauncher player =
i am writing code to play video file in ownservices.m class in a method
I am trying to play video(.mp4) in my app using following code. NSString *doc
I want to develop a j2me application that will play a video file(.mp4) by
I can't figure out why I'm not able to play the video in my
Live Wallpapers app can play video on lockscreen. How does it do this?
I have written code to play video in iPhone OS 3.1.3 and video is
I am using the following code to display a video file in the android
I have implement the following code in order to test playing a video from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.