douhui3760 2017-01-04 07:26
浏览 157

如何从android中的mysql数据库播放视频

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();
    }
    }
  • 写回答

1条回答 默认 最新

  • dqlhsm9820 2017-01-04 11:05
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类