dongyi6543 2015-03-10 11:53
浏览 142

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

I have uploaded mp4 video captured by emulator to php server, but i can not play this uploaded video with any player(window media player, vlc etc).

when i extracted this vidoe from emulator using file explorer, it is playing with vlc media player.

 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 = "
";
            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 code on server side.

$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";

 }

i have tested php code by uploading video from html form , this video is playing fine, i think there is problem with android code. can someone help me to find problem in code.

  • 写回答

1条回答 默认 最新

  • dongzuoyue6556 2015-03-16 21:40
    关注

    While uploading videos on php server keep every video name and its direct mp4 url in your database. For example in your php code you are uploading your videos in your image folder of your server. if your video name is "myvideo" then its url will be http://your-domain-name.com/images/myvideo.mp4. save those links in your database and make web service to get all video links. call that web service from android and use android videoview to play that video. here is the xml of videoview.

    <?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>
    

    here is the code to play video and there is no need of asynctask to play video in android. videoview will stream it.

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

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题