如何在Android编程中启动App时添加视频文件?

时间:2011-07-14 07:09:56

标签: android

每当我启动App显示详细信息时,我都需要添加视频文件。所以我需要帮助。我需要知道如何编写代码。

2 个答案:

答案 0 :(得分:2)

将您的视频文件保存在raw文件夹

中提供您的视频文件名称
path="android.resource://yourpackagename/"+R.raw.yourvideofilename;

见下面的课程

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这样的文件

 <?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>

在清单文件

 <activity android:name=".HomeVideo" >
    <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>             

   </activity>  

答案 1 :(得分:0)

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

并在 XML 文件中添加thid:

<?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>