Playing video from http server in android
I have a list-view in which i am loading all video's from server's particular folder. now what i am trying to do is by clicking a list item it should play that video in video view. Everything seems to work perfect, but only problem i am facing is when i click on list-view item it is not playing that video.
PS. I am getting following log in error log, but my application is not stopping unfortunately, my display is keep showing me that video is loading in progress bar which i set in Asynctask, but when i checked in log cat i saw this error.
Of course i have searched in google as well as SO but i did not got satisfactory result.
Folowwing is my log cat message.
02-21 11:08:52.964: W/SurfaceFlinger(101): nothing to do with mask 0x00000000
02-21 11:08:52.974: W/System.err(1992): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
02-21 11:08:52.976: W/System.err(1992): at android.os.Handler.<init>(Handler.java:121)
02-21 11:08:52.976: W/System.err(1992): at android.widget.MediaController$3.<init>(MediaController.java:474)
02-21 11:08:52.977: W/System.err(1992): at android.widget.MediaController.<init>(MediaController.java:474)
02-21 11:08:52.978: W/System.err(1992): at android.widget.MediaController.<init>(MediaController.java:210)
02-21 11:08:52.978: W/System.err(1992): at iqual.fidol_final.ServerFileList.playVideo(ServerFileList.java:225)
02-21 11:08:52.979: W/System.err(1992): at iqual.fidol_final.ServerFileList.access$3(ServerFileList.java:221)
02-21 11:08:52.979: W/System.err(1992): at iqual.fidol_final.ServerFileList$PlayVideo$1.run(ServerFileList.java:181)
02-21 11:08:52.979: W/System.err(1992): at java.lang.Thread.run(Thread.java:856)
And following is my code.
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String url1 = null;
Object o1 = myList.get(position);
url1 = o1.toString().replace(" ", "%20").trim();
playVideo = (PlayVideo) new PlayVideo(url1).execute();
}
});
and this is my Asynctask to playvideo
class PlayVideo extends AsyncTask<String, Void, Boolean> {
String baseURL;
public PlayVideo(String baseURL) {
this.baseURL = baseURL;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = ProgressDialog.show(ServerFileList.this,
" Buffering...", "please wait..", false);
pDialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
playVideo(baseURL);
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
return true;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
}
}
and following is my playVideo method.
private void playVideo(String baseURL) {
Uri myUri = Uri.parse(baseURL);
MediaController mc;
mc = new MediaController(this);
mVideoView.setMediaController(mc);
mVideoView.setVideoURI(myUri);
System.out.println("====Myuri=====" + myUri);
mVideoView.requestFocus();
mVideoView.start();
}
I also tried to creating new thread in doInbackground method and by removing thread also, but this tricks does not worked for me.
also one thing that it showing error on line 225 that is mc = new MediaController(this);
你可以删除你的AsyncTask并使用下面的代码直接播放你的视频,
String url1 = null;
Object o1 = myList.get(position);
url1 = o1.toString().replace(" ", "%20").trim();
playVideo(url1);
U可以在VideoView中播放视频试试这个使用Asynctask
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playvideo);
VideoView v=(VideoView)findViewById(R.id.videoview);
new BackgroundAsyncTask().execute("Video Url");
}
public class BackgroundAsyncTask extends AsyncTask<String, Uri, Void> {
Integer track = 0;
ProgressDialog dialog;
protected void onPreExecute() {
dialog = new ProgressDialog(PlayVideo.this);
dialog.setMessage("Loading, Please Wait...");
dialog.setCancelable(true);
dialog.show();
}
protected void onProgressUpdate(final Uri... uri) {
try {
media = new MediaController(PlayVideo.this);
v.setMediaController(media);
media.setPrevNextListeners(new View.OnClickListener() {
@Override
public void onClick(View v) {
// next button clicked
}
}, new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
media.show(10000);
v.setVideoURI(uri[0]);
v.requestFocus();
v.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
v.start();
dialog.dismiss();
}
});
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalStateException e) {
e.printStackTrace();
}
catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected Void doInBackground(String... params) {
try {
Uri uri = Uri.parse(params[0]);
publishProgress(uri);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
链接地址: http://www.djcxy.com/p/19160.html