Reputation: 695
I have created a database in mysql and added videos to it as blobs. I am trying to figure out how to now play these videos in my android application by connecting to the database and playing them based on their id. I don't even know where to begin on how I might do this. I have looked online and there are no tutorials of how to play a video from a mysql database. If anyone knows any useful tutorial on how to do this that would be really helpful or if they could explain it to me.
I have already tried to create a basic login android app to try and understand how to get and post info to a database. below is the code for it. But as I said there are no tutorials that I could find on how to play videos from a database. So If anyone knows or could show me how i can modify this code to download videos from the database instead that would also be extremely helpful.
public class MainActivity extends AppCompatActivity {
EditText ET_NAME,ET_PASS;
String login_name, login_pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ET_NAME = (EditText)findViewById(R.id.user_name);
ET_PASS = (EditText)findViewById(R.id.user_pass);
}
public void userLogin(View view){
login_name = ET_NAME.getText().toString();
login_pass = ET_PASS.getText().toString();
String method = "login";
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(method, login_name,login_pass);
}
}
public class BackgroundTask extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
BackgroundTask(Context context){
this.context = context;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("login info...");
}
@Override
protected String doInBackground(String... params) {
String login_url = "http://192.168.157.1/login.php";
String method = params[0];
if(method.equals("login")){
String login_name = params[1];
String login_pass = params[2];
try {
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data = URLEncoder.encode("login_name", "UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+
URLEncoder.encode("login_pass", "UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response = "";
String line = "";
while((line = bufferedReader.readLine()) != null){
response += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}
}
Upvotes: 2
Views: 1978
Reputation: 25481
Just to add to the comments above - you may want a database so you can do queries on titles, actors etc to help user find a video they want to watch.
Typically this 'metadata' about the videos is indeed stored in a database, along with a link to the video. The video itself is stored as a file.
In more sophisticated solutions, you may want to actually serve the video via a dedicated streaming server (e.g. http://www.videolan.org/vlc/streaming.html) which will allow your client on the Android device automatically choose a steaming size that matches the devices display and the current network conditions.
In this case the streaming server may actually create multiple versions of the video, depending on how important user quality is for your deployment.
Upvotes: 1