我已经构建了一个应用程序,其中包含从 Internet 流式传输的视频,我对它们的性能印象不深。有人愿意分享从 SD 卡加载视频的代码吗?
谢谢
我已经构建了一个应用程序,其中包含从 Internet 流式传输的视频,我对它们的性能印象不深。有人愿意分享从 SD 卡加载视频的代码吗?
谢谢
我希望这段代码能帮助你
public class video extends Activity{
VideoView video_view;
String ex_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.eccryption);
video_view = (VideoView) findViewById(R.id.videoView1);
ex_name = getIntent().getExtras().getString("video_name");
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(video_view);
video_view.setMediaController(new MediaController(this));
handler.sendEmptyMessage(1);
}
Handler handler = new Handler(){
public void handleMessage(Message msg){
int pos=msg.what;
if (pos == 1){
video_view.setVideoPath(Environment.getExternalStorageDirectory()+"/"+ex_name+".mp4");
video_view.requestFocus();
video_view.start();
Log.d("Before Video Finish", "i m in before video finish");
video_view.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
finish();
}
});
}
}
};
使用这个代码这个代码,我亲爱的朋友们!
创建一个活动并在需要播放视频时调用它。您可以在 Intent 中捆绑视频路径(无论是 url、sdcard 还是资源)。然后在你的活动中,它应该只包含一个FrameLayout
和一个VideoView
做类似的事情:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mVideoPath = extras.getString(INTENT_EXTRA_URI);
int resId = getResId(mVideoPath, R.raw.class);
String uriPath = null;
if (mVideoPath.startsWith("http://") || mVideoPath.startsWith("https://")) {
uriPath = mVideoPath;
} else if (mVideoPatah.startsWith("/mnt/sdcard/")) {
uriPath = mVideoPath;
} else {
int resId = getResId(mVideoPath, R.raw.class);
uriPath = "android.resource://" + getResources().getResourcePackageName(resId) + "/" + resId;
}
mVideoView.setVideoURI(Uri.parse(uriPath));
MediaController mediaController = new MediaController(this);
mVideoView.setMediaController(mediaController);
mVideoView.requestFocus();
mVideoView.start();
}