How to create a music player app with sketchware


To create an app that can download any file from any web url, we have to do following:

Define an AsyncTask which can do the download in background, i.e. in a thread different from the main UI thread. We need this because downloading large files on main thread may consume more memory and may lead the app to crash.
Use HttpURLConnection to openConnection() for the url and create an InputStream for downloading the url.

Create an OutputStream and write data from InputStream to File path in sdcard.
Show download progress using onProgressUpdate method of AsyncTask.
In onPreExecute() method of AsyncTask make ProgressBar VISIBLE.
In onPostExecute method of AsyncTask, make ProgressBar GONE.
To create such an app in Sketchware which can download file from link provided, follow the steps given below.

1. Create a new project in Sketchware. In VIEW area add an EditText edittext1 and a Button button1 . Add a LinearH
progresslinear , inside this add a circular ProgressBar progressbar1 and a LinearV containing a TextView
textview2 , and horizontal ProgressBar
progressbar2 .

2. Add a RequestNetwork component
network. This is required only for adding INTERNET permissions in the Sketchware app.

3. Add four String variables path,
filename , myurl and result , and two Number variables size and sumCount .

4. In onCreate event make
progresslinear GONE.

5. Create a More block called extra . Here use codes and blocks as shown in image below.
i. In an add sources directly block put a } to close _extra(){ and then define an
AsyncTask with name DownloadTask.
}
private class DownloadTask extends AsyncTask<String, Integer, String> {
ii. In another add source directly block, add onPreExecute method of AsyncTask.
@Override
protected void onPreExecute() {
iii. Use blocks to make progresslinear VISIBLE and set "Starting Download" as text of textview2, in onPreExecute.
iv. Close onPreExecute Method with a }.
}
v. Define doInBackground method of AsyncTask. Use HttpURLConnection to openConnection() for the url and create an InputStream for downloading the url.
In the code below ' in' is the InputStream, address[0] is the url, and
filename is String variable retrieved from url, and result is the String variable which is used later in onPostExecute.
protected String doInBackground(String... address) {
try {
filename= URLUtil.guessFileName(address[0], null, null);
int resCode = -1;
java.io.InputStream in = null;
java.net.URL url = new java.net.URL(address[0]);
java.net.URLConnection urlConn = url.openConnection();
if (!(urlConn instanceof java.net.HttpURLConnection)) {
throw new java.io.IOException("URL is not an Http URL"); }
java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) urlConn; httpConn.setAllowUserInteraction(false); httpConn.setInstanceFollowRedirects(true); httpConn.setRequestMethod("GET"); httpConn.connect();
resCode = httpConn.getResponseCode();
if (resCode == java.net.HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
size = httpConn.getContentLength();
} else { result = "There was an error"; }
vi. Set String path to Join
DIRECTORY_DOWNLOADS and / and
filename.
vii. After that use block to write empty String to path. This is required for adding WRITE EXTERNAL STORAGE permissions to the Sketchware app.
viii. Use codes to create an OutputStream and write data from InputStream to a File at path in sdcard.
Here publishProgress( (int)Math.round( sumCount*100 / size)); is use to display the percentage of download completed. This is displayed using the onProgressUpdate method of AsyncTask.
java.io.File file = new java.io.File(path);
java.io.OutputStream output = new java.io.FileOutputStream(file);
try {
int bytesRead;
sumCount = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
sumCount += bytesRead;
if (size > 0) {
publishProgress((int)Math.round(sumCount*100 / size));
}
}
} finally {
output.close();
}
result = filename + " saved";
in.close();
} catch (java.net.MalformedURLException e) {
result = e.getMessage();
} catch (java.io.IOException e) {
result = e.getMessage();
} catch (Exception e) {
result = e.toString();
}
return result;
}
Upto here is the doInBackground method.
ix. Define onProgressUpdate method of AsyncTask. Here we display the data received from publishProgress(..) used in doInBackground method. Thus we display the percentage of download completed in textview2 and
progressbar2 .
protected void onProgressUpdate(Integer... values) {
textview2.setText(values[values.length - 1] + "% downloaded");
progressbar2.setProgress(values[values.length - 1]);
}
x. Define onPostExecute method of AsyncTask. Here we display the String
result returned by doInBackground method as a Toast message.
protected void onPostExecute(String s){
showMessage(s);
xi. Use block to make progresslinear GONE and add } at end to close onPostExecute method.
}
We do not put } at end to close AsyncTask because we already placed a } in beginning of this more block.

6. In button1 onClick event set String
myurl to edittext1 getText and use add source directly block to put following code in it:
new DownloadTask().execute(myurl);
This code calls the AsyncTask on Button Click, to download the URL from edittext1 in background.

7. Save and run the project.

In the app, write any url in EditText field and click the button. If the link is valid it will be downloaded.

Post a Comment

0 Comments