Reputation: 53
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 = "\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 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.
Upvotes: 3
Views: 879
Reputation: 1068
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();
}
});
Upvotes: 1