How to add video file at the startup of App in Android Programming?
I need to add video file whenever I start App showing details. So I need help for that. I need to know how to write 开发者_Go百科a code for that.
save your video file in raw
folder
give your video file name in
path="android.resource://yourpackagename/"+R.raw.yourvideofilename;
see the below class
public class HomeVideo extends Activity{
private VideoView videoView;
String extStorageDirectory;
protected static final int PLAY = 0x101;
protected static final int STOP = 0x102;
protected static final int PAUSE = 0x103;
int State;
private String current;
private String path ;
private VideoView mVideoView;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.homevideo);
path="android.resource://yourpackagename/"+R.raw.yourvideofilename;
mVideoView = (VideoView) findViewById(R.id.video);
if (path == null || path.length() == 0) {
Toast.makeText(HomeVideo.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
} else {
// If the path has not changed, just start the media player
if (path.equals(current) && mVideoView != null) {
mVideoView.start();
mVideoView.requestFocus();
return;
}
current = path;
mVideoView.setVideoPath(path);
mVideoView.start();
mVideoView.requestFocus();
}
mVideoView.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
Intent in =new Intent(HomeVideo.this,NextActivity.class);
startActivity(in);
finish();
}
});
}
}
xml
file like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView
android:id="@+id/video"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true">
</VideoView>
</LinearLayout>
In manifest file
<activity android:name=".HomeVideo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
VideoViewExample *.java*
package com.sample.VideoViewExample;
public class VideoViewExample extends Activity {
private VideoView mVideoView;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.documentariesandyou));
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
}
}
and add thid in the XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView<br/>
android:id="@+id/surface_view"
android:layout_width="320px"
android:layout_height="240px"/>
</LinearLayout>
精彩评论