i created a xamarin.forms application. in this app, the students must download the video lessons according to their grades and the subject in concern. i stored the .mp4 videos in a folder in a web api. the thing is that the video is taking a lot of time to be downloaded that the app crashes. here is my web api code for getting the video:
public DataTable PostvideoFilesFromfolder([FromBody] uploadPdfModel user)
{
DataTable dt = new DataTable();
dt.TableName = "videoDetails";
DataTable dt2 = new DataTable();
dt2.TableName = "videoDetails2";
dt2.Columns.Add("pages", typeof(string));
dt2.Columns.Add("videofile", typeof(byte[]));
dt2.Columns.Add("videoFileName", typeof(string));
SqlConnection conn = new SqlConnection(DBConnection);
try
{
SqlCommand cmd = new SqlCommand("getvideoFilesdownload", conn);
cmd.CommandType = CommandType.StoredProcedure;
if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
}
// cmd.Parameters.Add("@id", SqlDbType.Text).Value = user.id;
cmd.Parameters.Add("@grade", SqlDbType.Text).Value = user.grade;
cmd.Parameters.Add("@section", SqlDbType.Text).Value = user.section;
cmd.Parameters.Add("@subject", SqlDbType.Text).Value = user.subject;
cmd.Parameters.Add("@lesson", SqlDbType.Int).Value = user.lesson;
cmd.Parameters.Add("@pages", SqlDbType.Text).Value = user.pages;
cmd.CommandTimeout = 300;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
// string name= dt.Rows[0]["videoFileName"].ToString();
foreach(DataRow row in dt.Rows)
{
string filePath = "videoFiles/"+user.subject+"/" + row["videoFileName"].ToString();
string fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath);
byte[] fileBytes = File.ReadAllBytes(fullPath);
dt2.Rows.Add(row["pages"].ToString(), fileBytes, row["videoFileName"].ToString());
}
}
finally
{
conn.Close();
}
return dt2;
}
and here is the code in the app where i download the video and try to display it:
async private void videoContent_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
videolst.IsVisible = false;
// Image img = sender as Image;
videoFileDetailspages selecteditem = videoContent.SelectedItem as videoFileDetailspages;
//await DisplayAlert("l", "l", "k");
int lessonNb = selecteditem.lesson;
videoFileDetails lesson = new videoFileDetails();
lesson = await App.Database.GetvideoAsync(grd, subj, lessonNb,selecteditem.pages);
//await DisplayAlert("l", "l", "k");
if (lesson == null)
{
popupLoadingView.IsVisible = true;
activityIndicator.IsRunning = true;
urlClass urldata = new urlClass();
string uri = urldata.url + "/PostvideoFilesFromfolder";
videodownload data = new videodownload();
data.grade = grd;
data.section = "";
data.subject = subj;
data.lesson = lessonNb;
data.pages=selecteditem.pages;
StringContent content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
try
{
HttpResponseMessage responsepost = await client.PostAsync(uri, content);
if (responsepost.IsSuccessStatusCode == true)
{
string outcome = await responsepost.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<List<videodownload>>(outcome);
await App.Database.SavevideoAsync(new videoFileDetails
{
subject = subj,
grade = grd,
Name = result[0].videoFileName,
lesson = lessonNb
});
Stream writingStream = new MemoryStream(result[0].videofile);
string downloadpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), result[0].videoFileName);
File.WriteAllBytes(downloadpath, result[0].videofile);
// await DisplayAlert(";oijohjhj", "Please, Check Your Credentials!", "Cancel");
popupLoadingView.IsVisible = false;
activityIndicator.IsRunning = false;
var stack = this.Navigation.NavigationStack;
if (stack[stack.Count - 1].GetType() != typeof(videoDisplayPage))
{
await Navigation.PushAsync(new videoDisplayPage(downloadpath));
}
}
else
{
await DisplayAlert("Operation Failed", responsepost.IsSuccessStatusCode.ToString(), "Cancel");
}
}
catch (System.Net.WebException exp)
{
await DisplayAlert("Connection Failed", "Please Check Your Internet Connection!", "Cancel");
}
}
else
{
string pathopen = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), App.Database.GetvideoAsync(grd, subj, lessonNb,selecteditem.pages).Result.Name);
var stack = this.Navigation.NavigationStack;
if (stack[stack.Count - 1].GetType() != typeof(videoDisplayPage))
{
await Navigation.PushAsync(new videoDisplayPage(pathopen));
}
//await DisplayAlert("s", "ll", "kk");
}
}
what should i do to make it faster or to make the app wait until the video is downloaded and not crash?
thanks in advance
i updated my web service code to this:
public byte [] PostvideoFilesFromfolder([FromBody] uploadPdfModel user)
{
DataTable dt = new DataTable();
dt.TableName = "videoDetails";
byte[] videodata = { };
SqlConnection conn = new SqlConnection(DBConnection);
try
{
SqlCommand cmd = new SqlCommand("getvideoFilesdownload", conn);
cmd.CommandType = CommandType.StoredProcedure;
if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
}
cmd.Parameters.Add("@grade", SqlDbType.Text).Value = user.grade;
cmd.Parameters.Add("@section", SqlDbType.Text).Value = user.section;
cmd.Parameters.Add("@subject", SqlDbType.Text).Value = user.subject;
cmd.Parameters.Add("@lesson", SqlDbType.Int).Value = user.lesson;
cmd.Parameters.Add("@pages", SqlDbType.Text).Value = user.pages;
cmd.CommandTimeout = 300;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
// string name= dt.Rows[0]["videoFileName"].ToString();
foreach(DataRow row in dt.Rows)
{
string filePath = "videoFiles/"+user.subject+"/" + row["videoFileName"].ToString();
string fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath);
videodata = File.ReadAllBytes(fullPath);
}
}
finally
{
conn.Close();
}
return videodata;
}
but it is still taking a lot of time to download