2016-03-07 54 views
2

我正在将一些视频集成到我的一个活动中。它玩的很好,但我必须点击播放才能真正播放。我搜索了论坛和开发者网站,找不到与自动播放或自动启动相关的任何内容。有什么建议么?我想在Android中添加自动播放媒体

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

我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> 
+1

mVideoView.start()? – Dhina

回答

3

mVideoView.start()用于启动视频

开始视频

,或者您也可以使用

mVideoView.seekTo(1) 
+0

亲爱Nirav我得到新的错误“无法播放此视频” – amisha

+0

意味着Android的 –

+0

我的视频格式不支持的视频格式为MP4 – amisha