在android中从http服务器播放视频

时间:2014-02-21 05:49:49

标签: android video android-asynctask android-mediaplayer android-videoview

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

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

当然我在google和SO搜索过但是我没有得到满意的结果。

Folowwing是我的log cat消息。

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)

以下是我的代码。

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

这是我的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);
        }
    }

以下是我的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);

2 个答案:

答案 0 :(得分:2)

您可以使用以下代码

删除AsyncTask并直接播放视频
String url1 = null; 
Object o1 = myList.get(position); 
url1 = o1.toString().replace(" ", "%20").trim(); 
playVideo(url1);

答案 1 :(得分:1)

你可以在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;
    }