无法播放从android上传到php服务器的视频

时间:2015-03-10 11:53:16

标签: php android video

我已经将模拟器捕获的mp4视频上传到php服务器,但我无法使用任何播放器(窗口媒体播放器,vlc等)播放此上传的视频。

当我使用文件浏览器从模拟器中提取此vidoe时,它正在使用vlc媒体播放器。

 protected String doInBackground(Void... params)
        {
            HttpURLConnection connection = null;
            DataOutputStream outputStream = null;
            DataInputStream inputStream = null;
            String urlServer = "http://10.0.2.2:80/maria/upload_file2.php";
            String pathToOurFile="/storage/sdcard/DCIM/Camera/VID_20150225_152244.mp4";


            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary =  "123*****sdf";

            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1*1024*1024;

            try
            {
                FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

                URL url = new URL(urlServer);
                connection = (HttpURLConnection) url.openConnection();

                // Allow Inputs & Outputs
                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.setUseCaches(false);

                // Enable POST method
                connection.setRequestMethod("POST");

                connection.setRequestProperty("Connection", "Keep-Alive");
                connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=123*****sdf");

                outputStream = new DataOutputStream( connection.getOutputStream() );
                outputStream.writeBytes(twoHyphens + boundary + lineEnd);
                outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile );
                outputStream.writeBytes(lineEnd);

                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];

                // Read file
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                while (bytesRead > 0)
                {
                    outputStream.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                }

                outputStream.writeBytes(lineEnd);
                outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                // Responses from the server (code and message)
                int serverResponseCode = connection.getResponseCode();
                String serverResponseMessage = connection.getResponseMessage();
                Log.d("ServerCode",""+serverResponseCode);
                Log.d("serverResponseMessage",""+serverResponseMessage);
                fileInputStream.close();
                outputStream.flush();
                outputStream.close();
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }

                       return "Success";

        }

服务器端的PHP代码。

$targetfolder = "images/";

$targetfolder = $targetfolder . basename( $_FILES['uploadedfile']['name']) ;




if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $targetfolder))

 {

 echo "The file ". basename( $_FILES['uploadedfile']['name']). " is uploaded";

 }

 else {

 echo "Problem uploading file";

 }

我已经通过从html表单上传视频测试了PHP代码,这个视频播放正常,我觉得android代码有问题。有人可以帮助我在代码中找到问题。

1 个答案:

答案 0 :(得分:0)

在php服务器上传视频时,请在您的数据库中保留每个视频名称及其直接mp4网址。例如,在您的PHP代码中,您将视频上传到服务器的图像文件夹中。如果您的视频名称为“myvideo”,则其网址为http://your-domain-name.com/images/myvideo.mp4。将这些链接保存在您的数据库中,并使Web服务获取所有视频链接。从android调用该Web服务并使用android videoview播放该视频。这是视频的xml。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

这里是播放视频的代码,不需要asynctask在android中播放视频。 videoview将流式传输。

VideoView videoView =(VideoView)findViewById(R.id.videoView);

        MediaController mediaController= new MediaController(this);
        mediaController.setAnchorView(videoView);
        Uri uri=Uri.parse(" http://your-domain-name.com/images/myvideo.mp4");
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(uri);

        videoView.requestFocus();
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                // TODO Auto-generated method stub

                videoView.start();
            }
        });