Created
January 3, 2019 10:32
-
-
Save alitamoor65/8093f5742ce68f0168eb39489399a8e5 to your computer and use it in GitHub Desktop.
DownloadAudioAndPlay.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.alitamoor.downloadersampleapplication; | |
import android.media.MediaPlayer; | |
import android.os.AsyncTask; | |
import android.os.Environment; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.widget.ProgressBar; | |
import java.io.BufferedInputStream; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.URL; | |
import java.net.URLConnection; | |
public class MainActivity extends AppCompatActivity { | |
ProgressBar progressbar; | |
File file; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
progressbar = (ProgressBar)findViewById(R.id.progress_view); | |
new DownloadFile().execute("https://s3-us-west-2.amazonaws.com/1hbcf/Maboob.mp3"); | |
} | |
private class DownloadFile extends AsyncTask<String, Integer, String> { | |
@Override | |
protected String doInBackground(String... url) { | |
int count; | |
try { | |
URL urls = new URL(url[0]); | |
URLConnection connection = urls.openConnection(); | |
connection.connect(); | |
// this will be useful so that you can show a tipical 0-100% progress bar | |
int lenghtOfFile = connection.getContentLength(); | |
InputStream input = new BufferedInputStream(urls.openStream()); | |
String filename = "Maboob.mp3"; | |
file = new File(Environment.getExternalStorageDirectory(), filename); | |
OutputStream output = new FileOutputStream(file); | |
Log.i("Downloader", "doInBackground: " + file.getAbsolutePath()); | |
byte data[] = new byte[1024]; | |
long total = 0; | |
while ((count = input.read(data)) != -1) { | |
total += count; | |
// publishing the progress.... | |
publishProgress((int) (total * 100 / lenghtOfFile)); | |
output.write(data, 0, count); | |
Log.i("Downloader", "doInBackground: " + (total * 100 / lenghtOfFile)); | |
} | |
output.flush(); | |
output.close(); | |
input.close(); | |
} catch (Exception e) { | |
} | |
return null; | |
} | |
@Override | |
protected void onPreExecute() { | |
super.onPreExecute(); | |
progressbar.setVisibility(ProgressBar.VISIBLE); | |
} | |
@Override | |
protected void onPostExecute(String s) { | |
super.onPostExecute(s); | |
progressbar.setVisibility(ProgressBar.GONE); | |
MediaPlayer mp = new MediaPlayer(); | |
if(file != null){ | |
try { | |
mp.setDataSource(file.getAbsolutePath()); | |
mp.prepare(); | |
mp.start(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment