gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:58:42 26 4
gpt4 key购买 nike

我已将模拟器捕获的 mp4 视频上传到 php 服务器,但我无法使用任何播放器(window media player、vlc 等)播放此上传的视频。

当我使用文件资源管理器从模拟器中提取此视频时,它正在使用 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 代码有问题。谁能帮我找出代码中的问题。

最佳答案

在 php 服务器上上传视频时,将每个视频名称及其直接 mp4 url​​ 保存在您的数据库中。例如,在您的 php 代码中,您正在将视频上传到服务器的图像文件夹中。如果您的视频名称是“myvideo”,那么它的 url 将是 http://your-domain-name.com/images/myvideo.mp4 .将这些链接保存在您的数据库中,并制作网络服务以获取所有视频链接。从 android 调用该网络服务并使用 android videoview 播放该视频。这是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>

这是播放视频的代码,在android中不需要asynctask来播放视频。 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();
}
});

关于php - 无法播放从android上传到php服务器的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28963044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com