【发布时间】:2018-02-05 07:33:01
【问题描述】:
我正在拍摄一个时长为 5 分钟的视频并尝试将其上传到 API,但 API 的响应是错误的,并且该视频不存在于数据库中。我将小型视频上传到 API。
我发现视频的大小很大,上传需要更多时间,并且 API 在一段时间后超时。如何在不减少内容的情况下减小视频的大小,或在几秒钟内将长时间的视频上传到 API。
请帮帮我..
这是我的代码
private async void TakeVideo_Clicked(object sender, EventArgs e)
{
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakeVideoSupported)
{
await DisplayAlert("No Camera", ":( No camera avaialble.", "OK");
return;
}
var _file = await CrossMedia.Current.TakeVideoAsync(new Plugin.Media.Abstractions.StoreVideoOptions
{
Name = "video.mp4",
Directory = "Videos",
});
if (_file == null)
{
return;
}
else
{
_path = _file.Path;
using (var _streamReader = new StreamReader(_file.GetStream()))
{
var _array = default(byte[]);
using (MemoryStream _memoryStream = new MemoryStream())
{
_streamReader.BaseStream.CopyTo(_memoryStream);
_array = _memoryStream.ToArray();
if (await DisplayAlert(App._confirmation, "Do you want to save the video?", "Yes", "Cancel"))
{
FileUploadAsync(_array, false);
activity_Indicator.IsVisible = true;
activity_Indicator.IsRunning = true;
}
else
{
return;
}
}
}
}
}
public async void FileUploadAsync(byte[] fileUpload, bool IsImage)
{
APIResponse _response = await App.DataManager.UpdateFilesAsync(ID, fileUpload, IsImage);
if (_response != null)
{
activity_Indicator.IsRunning = false;
if (IsImage)
{
DependencyService.Get<IAlertPlayer>().AlertMessege("Image upload successfully");
}
else
{
DependencyService.Get<IAlertPlayer>().AlertMessege("Video upload successfully");
}
}
else
{
DisplayAlertMessage();
}
}
public async Task<APIResponse>UpdateFilesAsync(int id,byte[] file,bool IsImage)
{
Url _url = new Url(BaseURL).AppendPathSegment("sample/UploadFiles");
_url.QueryParams["ID"] = id;
return await Service.POSTFILE<APIResponse>(_url, file,IsImage);
}
这是我的 POST 方法
public async Task<T> POSTFILE<T>(Url url, byte[] uploadFile, bool IsImage)
{
try
{
using (MultipartFormDataContent content = new MultipartFormDataContent())
{
ByteArrayContent filecontent = new ByteArrayContent(uploadFile);
if (IsImage)
{
filecontent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = Guid.NewGuid().ToString() + ".png"
};
}
else
{
filecontent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = Guid.NewGuid().ToString() + ".mp4"
};
}
content.Add(filecontent);
using (HttpResponseMessage response = await Client.PostAsync(url, content))
{
string result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(result);
}
}
}
catch (Exception ex)
{
}
return default(T);
}
}
【问题讨论】:
-
请回复我...
-
你必须尝试一些视频加密。试试这个链接。forums.xamarin.com/discussion/62029/…
标签: c# xaml xamarin.forms xamarin.android