I want to add auto play media in android

amisha picture amisha · Mar 7, 2016 · Viewed 7.7k times · Source

I am working on integrating some video into one of my Activities. It plays fine, but I have to click play for it to actually play. I searched the forums and the developer site and cannot find anything relating to autoplay or autostart. Any suggestions?

MainActivity.java

...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });


    VideoView mVideoView = (VideoView) findViewById(R.id.videoView);
    mVideoView.setVideoPath("file:///android_asset/video1.MP4");
    mVideoView.setMediaController(new MediaController(this));
    mVideoView.seekTo(1);


    //mVideoView.requestFocus();
}

My contain_main.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment"
    android:name="imaker.MediaAutoPlay.MainActivityFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:layout="@layout/fragment_main">

<VideoView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/videoView"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" />

</fragment>

Answer

Nirav Ranpara picture Nirav Ranpara · Mar 7, 2016

mVideoView.start() for start video

to start video

or you can also use

mVideoView.seekTo(1)