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

我有一个列表视图,其中我从服务器的特定文件夹加载所有视频。 现在我想要做的是通过点击一个列表项目,它应该在视频视图中播放该视频。 一切似乎都工作完美,但只有我面临的问题是当我点击列表视图项目它不播放该视频。

PS。 我得到以下登录错误日志,但我的应用程序不会停止不幸的是,我的显示器不断显示视频加载进度栏,我在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.<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 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);


你可以删除你的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/19159.html

上一篇: Playing video from http server in android

下一篇: Complete bibliography with markdown for scientific papers