如何发挥从SD卡视频 [英] how to play video from sdcard
问题描述
我在做的一个项目,我的产品类别的数量和每个产品类别包含一个视频。
我被从Web服务获取URL播放视频。当我第一次播放的视频,应该从URL中下载该视频只有一次。当没有互联网连接就应该从那里下载(SD卡)播放。假设我已经下载了所有三款产品3段视频,现在我必须发挥这些视频的时候没有从SD卡互联网即。这里来的问题,如何让特定的视频路径各自的产品。我没有开画廊。我必须直接播放视频,该产品。以及如何限制视频下载更多然后一次。
我的code播放视频
私人无效的playVideo(字符串路径){ 对话框=新的对话框(这一点,android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.layout_video_popup); ImageView的imgViewClose =(ImageView的)dialog.findViewById(R.id.imgViewClose);
imgViewClose.setOnClickListener(本); btnPlayPause =(按钮)dialog.findViewById(R.id.button1);
btnPlayPause.setOnClickListener(本); 搜索栏=(搜索栏)dialog.findViewById(R.id.seekBar1);
seekBar.setOnClickListener(本);
的LinearLayout =(的LinearLayout)dialog.findViewById(R.id.linerlayout_bottom);
mVideoView =(VideoView)dialog.findViewById(R.id.videoView);
mVideoView.setOnTouchListener(本);
mVideoView.setOn preparedListener(本);
mVideoView.setOnCompletionListener(本);
dialog.show(); 的playVideo(路径,mVideoView);
AppLog.d(视频,的playVideo(),路径);
}私人无效的playVideo(字符串路径,VideoView mVideoView){
如果(路径== NULL || path.isEmpty()){
返回;
} 尝试{
pDialpg = ProgressDialog.show(这一点,视频,请稍候...,假的,真正的);
mVideoView.setVideoPath(路径);
mVideoView.requestFocus();
RelativeLayout.LayoutParams relativeParams =新RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
mVideoView.setLayoutParams(relativeParams);
mVideoView.invalidate();
}赶上(例外五){
// TODO:处理异常
}
这就是你如何能做到这一点的例子...
您可以检查是否有连接:
公共布尔checkConnectivity(上下文的背景下){ Log.d(TAG检查连接);
尝试{
connectivityManager =(ConnectivityManager)上下文
.getSystemService(Context.CONNECTIVITY_SERVICE);
wifiInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
mobileInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 如果(wifiInfo.isConnected()|| mobileInfo.isConnected()){
Log.d(TAG,WIFI /手机连接找到);
返回true;
}
}赶上(例外五){
Log.w(TAG,问题+ E);
}
Log.d(TAG,没有找到连接 - 脱机工作);
返回false;
}
第一次,如果没有连接,你可以设置一个默认的警告,例如:没有互联网连接无法播放视频
如果有康涅狄格州。你可以只播放流,如:
公共无效playVideoStream(上下文的背景下,
字符串URL,最终VideoView videoView){ Log.d(TAG,播放视频流); 的MediaController的MediaController =新的MediaController(背景);
mediaController.setAnchorView(videoView); 乌里视频= Uri.parse(URL);
videoView.setMediaController(的MediaController);
videoView.setVideoURI(视频) videoView.setOn preparedListener(新在preparedListener(){ 在prepared(MediaPlayer的MP)公共无效{
videoView.start();
}
}); 返回null;
}
如果你已经下载了它:
公共无效playVideoOffline(上下文的背景下,
字符串文件路径,VideoView videoView){ Log.d(TAG,播放视频离线); videoView.setVideoPath(文件路径); 的MediaController的MediaController =新的MediaController(背景);
mediaController.setMediaPlayer(videoView); videoView.setMediaController(的MediaController);
videoView.requestFocus(); videoView.start(); 返回null;
}
这是一种方式,你可以做到这一点,不一定做的最好的方法。
i am doing on a project where i have numbers of product categories and each product category contains a single video. i am playing video by getting url from web service. when first time i play that video it should download that video from that url only once. and when there is no internet connectivity it should play from where it is downloaded(sdcard). suppose i have downloaded 3 videos for all three products now i have to play those videos when there is no internet i.e. from sdcard. here comes problem that how to get particular video path for respective products. i don't have to open gallery. i have to directly play that video for that product. and how to restrict video downloading more then once. my code for playing video is
private void playVideo(String path) {
dialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.layout_video_popup);
ImageView imgViewClose = (ImageView)dialog.findViewById(R.id.imgViewClose);
imgViewClose.setOnClickListener(this);
btnPlayPause = (Button) dialog.findViewById(R.id.button1);
btnPlayPause.setOnClickListener(this);
seekBar = (SeekBar) dialog.findViewById(R.id.seekBar1);
seekBar.setOnClickListener(this);
linearlayout = (LinearLayout) dialog.findViewById(R.id.linerlayout_bottom);
mVideoView = (VideoView)dialog.findViewById(R.id.videoView);
mVideoView.setOnTouchListener(this);
mVideoView.setOnPreparedListener(this);
mVideoView.setOnCompletionListener(this);
dialog.show();
playVideo(path, mVideoView);
AppLog.d("Video", "playVideo()", path);
}
private void playVideo(String path, VideoView mVideoView) {
if(path == null || path.isEmpty()) {
return;
}
try {
pDialpg = ProgressDialog.show(this, "Video", "Please wait..", false, true);
mVideoView.setVideoPath(path);
mVideoView.requestFocus();
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
mVideoView.setLayoutParams(relativeParams);
mVideoView.invalidate();
} catch (Exception e) {
// TODO: handle exception
}
This is just an example of how you could do it...
You can check if there's connection:
public Boolean checkConnectivity(Context context) {
Log.d(TAG, "Checking connectivity");
try {
connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
wifiInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
mobileInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
Log.d(TAG, "Wifi/mobile Connection found");
return true;
}
} catch (Exception e) {
Log.w(TAG, "PROBLEM " + e);
}
Log.d(TAG, "No connection found - working offline");
return false;
}
First time, if no connection, you could set a default warning for example: "Can't play video without internet connection"
if there's conn. you could just play streaming like:
public Void playVideoStream(Context context,
String url, final VideoView videoView) {
Log.d(TAG, "Playing video stream");
MediaController mediaController = new MediaController(context);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(url);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
videoView.start();
}
});
return null;
}
and if you have already downloaded it:
public Void playVideoOffline(Context context,
String filePath, VideoView videoView) {
Log.d(TAG, "Playing video offline");
videoView.setVideoPath(filePath);
MediaController mediaController = new MediaController(context);
mediaController.setMediaPlayer(videoView);
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.start();
return null;
}
This is one way you could do it, not necessarily the best way of doing it.
这篇关于如何发挥从SD卡视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!