我正在查询Parse Backend的视频。我想使用getInBackground从解析下载视频。在下载视频后,我想使用onClick立即开始在我的VideoView活动中播放视频
以下是我的代码流程。 GridView的片段
query.orderByDescending("createdAt");
ob = query.find();
for (ParseObject country : ob) {
ParseFile image = (ParseFile) country.get("videoThumbs");
ParseFile video = (ParseFile)country.get("file");
video.getDataInBackground(new GetDataCallback(){
@Override
public void done(byte[] bytes, ParseException e) {
}
}, new ProgressCallback() {
public void done(Integer percentDone) {
// Update your progress spinner here. percentDone will be between 0 and 100.
}
});
String user = country.getString(ParseConstants.KEY_SENDER_NAME);
//Uri fileUri = Uri.parse(video.getUrl());
ParseFeeds map = new ParseFeeds();
map.setPhone(image.getUrl());
map.setVideo(video.getUrl());
map.setUser(user);
phonearraylist.add(map);
GridViewAdapter
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.feeds_image, null);
// Locate the ImageView in gridview_item.xml
holder.phone = (ImageView) view.findViewById(R.id.videoThumb);
holder.user = (TextView)view.findViewById(R.id.grid_item_title);
view.setTag(holder);
holder.progressBar = (ProgressBar)view.findViewById(R.id.thumbProgress);
} else {
holder = (ViewHolder) view.getTag();
}
// Load image into GridView
//imageLoader.DisplayImage(phonearraylist.get(position).getPhone(),
//holder.phone);
Picasso.with(context)
.load(phonearraylist.get(position).getPhone())
.transform(new CircleTransform())
.error(R.drawable.people)
.into(holder.phone);
holder.user.setText(phonearraylist.get(position).getUser());
// Capture GridView item click
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Send single item click data to SingleItemView Class
Intent intent = new Intent(context, SingleVideoView.class);
// Pass all data phone
intent.putExtra("video", phonearraylist.get(position)
.getVideo());
context.startActivity(intent);
}
});
return view;
这是我的VideoView活动
Intent i = getIntent();
// Get the intent from ListViewAdapter
phone = i.getStringExtra("video");
setContentView(R.layout.video_layout);
mVideoView = (VideoView)findViewById(R.id.videoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(mVideoView);
// mVideoView.setMediaController(mediaController);
mVideoView.setVideoPath(phone);
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
}
});
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
finish();
}
});
}
答案 0 :(得分:2)
使用此代码下载视频..您需要找到更新进度的方法。我也可以提供帮助,但现在我不必编写代码。如果你不能做到,我会在早上更新。
public void DownloadVideoFile(String url, final String path) {
rootDir = Environment.getExternalStorageDirectory() + "/."
+ context.getResources().getString(R.string.app_name);
new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... aurl) {
int count;
Log.e("Download ", "downloading");
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
String fileExtenstion = MimeTypeMap
.getFileExtensionFromUrl(aurl[0]);
String name = URLUtil.guessFileName(aurl[0], null,
fileExtenstion);
int lenghtOfFile = conexion.getContentLength();
File folder;
if (!path.isEmpty()) {
String path2 = path.replaceAll("/", "_");
folder = new File(rootDir + "/" + path2);
} else {
folder = new File(rootDir);
}
if (!folder.exists()) {
folder.mkdir();
}
File mypath;
if (!path.isEmpty()) {
String path2 = path.replaceAll("/", "_");
mypath = new File(rootDir + "/" + path2 + "/" + name);
} else {
mypath = new File(rootDir + "/" + name);
}
if (mypath.exists()) {
if (mypath.length() == lenghtOfFile) {
Log.e("Download ", "file already exists => "
+ mypath.toString());
return name;
}
} else {
Log.e("Download ",
"file doest not exists downloading => "
+ mypath.toString());
InputStream input = new BufferedInputStream(
url.openStream());
OutputStream output = new FileOutputStream(
mypath.toString());
byte data[] = new byte[1024];
// long total = 0;
while ((count = input.read(data)) != -1) {
// total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
return name;
}
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}.execute(url);
}
使用解析下载视频和图片,您需要检查this链接