在android中从http服务器播放video

我有一个列表视图,我从服务器的特定文件夹加载所有video。 现在我要做的是点击一个列表项,它应该在video视图中播放该video。 一切似乎都很完美,但我遇到的唯一问题是,当我点击列表视图项目时,它不会播放该video。

PS。 我正在跟踪登录错误日志,但我的应用程序没有停止不幸,我的显示器继续向我显示video正在加载进度栏我设置在Asynctask,但当我检查日志猫我看到这个错误。

当然我已经在谷歌搜索过以及SO但我没有得到满意的结果。

Folowwing是我的日志猫消息。

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.(Handler.java:121) 02-21 11:08:52.976: W/System.err(1992): at android.widget.MediaController$3.(MediaController.java:474) 02-21 11:08:52.977: W/System.err(1992): at android.widget.MediaController.(MediaController.java:474) 02-21 11:08:52.978: W/System.err(1992): at android.widget.MediaController.(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) 

以下是我的代码。

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

这是我播放video的Asynctask

 class PlayVideo extends AsyncTask { 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); } } 

以下是我的playVideo方法。

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

我也尝试在doInbackground方法中创建新线程并通过删除线程,但这个技巧对我不起作用。

还有一件事,它在第225行显示错误,即mc = new MediaController(this);

您可以使用以下代码删除AsyncTask并直接播放video,

 String url1 = null; Object o1 = myList.get(position); url1 = o1.toString().replace(" ", "%20").trim(); playVideo(url1); 

你可以在VideoView中播放video尝试使用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 { 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; }