javadi3d
پنج شنبه 01 اسفند 1392, 15:36 عصر
سلام
در کد زیر وقتی در حین دانلود موزیک back را میزنی موزیک را به صورت ناقص با نام و مکانی که برای آن در نظر گرفته شده است،ذخیره می کند و اگر دوباره روی دکمه دانلود کلیک کنیم با توجه به پیغام درون کد هشدار میگوید فایل دانلود شده است و دیگر کاربر نمیتواند فایل را به صورت کامل دانلود کند
چکار کنم تا وقتی back را میزنیم دانلود به صورت کامل قطع شده وفایل ناقص ذخیره نشود
خواهشا کمک کنید
این چندمین تاپیکی است که در موضوعات مختلف میگذارم و جواب نمیگیرم!
public class DoaMatn1 extends Activity implements OnClickListener {
MediaPlayer mp;
ImageButton btndowndoa;
ImageButton btnplaydoa;
SeekBar seek_bar;
Handler seekHandler = new Handler();
private ProgressDialog pDialog;
public static final int progress_bar_type = 0;
private static String file_url = "http://upir.ir/files92be/9e6cbb43de76.mp3";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.doamatn);
WebView wv = (WebView) findViewById(R.id.wvdoa);
wv.setBackgroundColor(0x00000000);
wv.setHorizontalScrollBarEnabled(false);
wv.loadUrl("file:///android_asset/doatavasol.html");
WebSettings webSettings = wv.getSettings();
webSettings.setDefaultFontSize(22);
mp = MediaPlayer.create(this,Uri.fromFile(audioFile));
btnplaydoa = (ImageButton) findViewById(R.id.btnplaydoa);
btnplaydoa.setOnClickListener(this);
btndowndoa = (ImageButton) findViewById(R.id.btndowndoa);
btndowndoa.setOnClickListener(this);
getInit();
seekUpdation();
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
File sdcard = Environment.getExternalStorageDirectory();
File audioFile = new File(sdcard.getPath() + "/EBKH/basem-tavasol.mp3");
public void getInit() {
if(audioFile.exists())
{
seek_bar = (SeekBar) findViewById(R.id.sbdoa);
seek_bar.setMax(mp.getDuration());
}}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnplaydoa :
if(audioFile.exists())
{
if(mp!=null)
{
if(mp.isPlaying())
{
mp.pause();
btnplaydoa.setImageResource(R.drawable.play);
}
else
{
mp.start();
btnplaydoa.setImageResource(R.drawable.puse);
}
}
}
else
{
Builder alert = new AlertDialog.Builder( DoaMatn1.this);
alert.setMessage("فایل دانلود نشده است،برای دانلود از گزینه سمت راست استفاده کنید.");
alert.setPositiveButton("OK", null);
alert.show();
}
break;
case R.id.btndowndoa :
if(!new File(Environment.getExternalStorageDirectory().toS tring() + "/EBKH/basem-tavasol.mp3").exists())
new DownloadFileFromURL().execute(file_url);
else
{
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(DoaMatn1.this);
dialogBuilder.setMessage("فایل دانلود شده است");
dialogBuilder.setPositiveButton("OK", null);
AlertDialog dialog = dialogBuilder.create();
dialog.show();
}
break;
}
}
Runnable run = new Runnable() {
@Override
public void run() {
seekUpdation();
}
};
public void seekUpdation() {
if(audioFile.exists())
{
seek_bar.setProgress(mp.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
}
}
@Override
public void onBackPressed() {
if( mp != null && mp.isPlaying() ) {
mp.stop();
}
super.onBackPressed();
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("در حال دانلود،لطفا صبور باشید...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORI ZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream("/sdcard/EBKH/basem-tavasol.mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
if(audioFile.exists())
{
Builder alert = new AlertDialog.Builder( DoaMatn1.this);
alert.setMessage("برای پخش فایل از صفحه خارج و دوباره داخل شوید.");
alert.setPositiveButton("OK", null);
alert.show();
}
}
}}
در کد زیر وقتی در حین دانلود موزیک back را میزنی موزیک را به صورت ناقص با نام و مکانی که برای آن در نظر گرفته شده است،ذخیره می کند و اگر دوباره روی دکمه دانلود کلیک کنیم با توجه به پیغام درون کد هشدار میگوید فایل دانلود شده است و دیگر کاربر نمیتواند فایل را به صورت کامل دانلود کند
چکار کنم تا وقتی back را میزنیم دانلود به صورت کامل قطع شده وفایل ناقص ذخیره نشود
خواهشا کمک کنید
این چندمین تاپیکی است که در موضوعات مختلف میگذارم و جواب نمیگیرم!
public class DoaMatn1 extends Activity implements OnClickListener {
MediaPlayer mp;
ImageButton btndowndoa;
ImageButton btnplaydoa;
SeekBar seek_bar;
Handler seekHandler = new Handler();
private ProgressDialog pDialog;
public static final int progress_bar_type = 0;
private static String file_url = "http://upir.ir/files92be/9e6cbb43de76.mp3";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.doamatn);
WebView wv = (WebView) findViewById(R.id.wvdoa);
wv.setBackgroundColor(0x00000000);
wv.setHorizontalScrollBarEnabled(false);
wv.loadUrl("file:///android_asset/doatavasol.html");
WebSettings webSettings = wv.getSettings();
webSettings.setDefaultFontSize(22);
mp = MediaPlayer.create(this,Uri.fromFile(audioFile));
btnplaydoa = (ImageButton) findViewById(R.id.btnplaydoa);
btnplaydoa.setOnClickListener(this);
btndowndoa = (ImageButton) findViewById(R.id.btndowndoa);
btndowndoa.setOnClickListener(this);
getInit();
seekUpdation();
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
File sdcard = Environment.getExternalStorageDirectory();
File audioFile = new File(sdcard.getPath() + "/EBKH/basem-tavasol.mp3");
public void getInit() {
if(audioFile.exists())
{
seek_bar = (SeekBar) findViewById(R.id.sbdoa);
seek_bar.setMax(mp.getDuration());
}}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnplaydoa :
if(audioFile.exists())
{
if(mp!=null)
{
if(mp.isPlaying())
{
mp.pause();
btnplaydoa.setImageResource(R.drawable.play);
}
else
{
mp.start();
btnplaydoa.setImageResource(R.drawable.puse);
}
}
}
else
{
Builder alert = new AlertDialog.Builder( DoaMatn1.this);
alert.setMessage("فایل دانلود نشده است،برای دانلود از گزینه سمت راست استفاده کنید.");
alert.setPositiveButton("OK", null);
alert.show();
}
break;
case R.id.btndowndoa :
if(!new File(Environment.getExternalStorageDirectory().toS tring() + "/EBKH/basem-tavasol.mp3").exists())
new DownloadFileFromURL().execute(file_url);
else
{
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(DoaMatn1.this);
dialogBuilder.setMessage("فایل دانلود شده است");
dialogBuilder.setPositiveButton("OK", null);
AlertDialog dialog = dialogBuilder.create();
dialog.show();
}
break;
}
}
Runnable run = new Runnable() {
@Override
public void run() {
seekUpdation();
}
};
public void seekUpdation() {
if(audioFile.exists())
{
seek_bar.setProgress(mp.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
}
}
@Override
public void onBackPressed() {
if( mp != null && mp.isPlaying() ) {
mp.stop();
}
super.onBackPressed();
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("در حال دانلود،لطفا صبور باشید...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORI ZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream("/sdcard/EBKH/basem-tavasol.mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
if(audioFile.exists())
{
Builder alert = new AlertDialog.Builder( DoaMatn1.this);
alert.setMessage("برای پخش فایل از صفحه خارج و دوباره داخل شوید.");
alert.setPositiveButton("OK", null);
alert.show();
}
}
}}