-1

音楽プレーヤーを作りたい

  • リストアイテムを持っています
  • 各項目のonclick
  • URLから.amrファイルをダウンロードし、フォルダに保存したファイルを音楽プレーヤーで再生します

onclick ダウンロード ファイルを作成するにはどうすればよいですか?

4

1 に答える 1

1

はい、AsyncTaskを使用して、ダウンロードの進行状況をダイアログに表示します。

//ダイアログをアクティビティのメンバーフィールドとして宣言しますProgressDialogmProgressDialog;

//onCreateメソッド内でインスタンス化します

mProgressDialog = new ProgressDialog(YourActivity.this);
mProgressDialog.setMessage("A message");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

DownloadFile downloadFile = new DownloadFile();

downloadFile.execute("Put here the URL .amr file");

private class DownloadFile extends AsyncTask<String, Integer, String> {
    @Override
    protected String doInBackground(String... sUrl) {
        try {
            URL url = new URL(sUrl[0]);
            URLConnection connection = url.openConnection();
            connection.connect();
            // this will be useful so that you can show a typical 0-100% progress bar
            int fileLength = connection.getContentLength();

        // download the file
        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream("/sdcard/file_name.extension");

        byte data[] = new byte[1024];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {
            total += count;
            // publishing the progress....
            publishProgress((int) (total * 100 / fileLength));
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {
    }
    return null;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    mProgressDialog.show();
}

@Override
protected void onProgressUpdate(Integer... progress) {
    super.onProgressUpdate(progress);
    mProgressDialog.setProgress(progress[0]);
}

}

于 2013-01-23T10:31:44.123 に答える