فایل های صوتی

باسلام و عر ض خسته نباشید

من می خواهم در برنامه نویسی اندروید تعدادی فایل صوتی از پیش تعین شده در وب را از آدرس های اینترنتی (آدرس های URL) بگیرم و سپس آنها را در حافظه خارجی (External Storage) ذخیره کنم و سپس در برنامه اندروید نمایش دهم و با قابلیت Cache کردن صوت ها به گونه ای که صوت هایی که قبلا دانلود شده اند، دوباره دانلود نشوند.

من خودم این کار را برای عکس ها انجام داده ام ولی هر چه تلاش کردم نتوانستم این کار را برای فایل های صوتی انجام دهم.

در زیر کدهایی که خودم باهاش این کار را برای عکس ها انجام داده ام را نوشته ام.

پاسخ ها

sokanacademy forum
کاربر سکان آکادمی 8 سال پیش

کدهای فایل FileCache.java به صورت زیر می باشد :

public class FileCache { private File cacheDir; public FileCache(Context context){ //Find the dir to save cached images if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"myFolder/.hidden_folder"); // point to folder be hidden else cacheDir=context.getCacheDir(); if(!cacheDir.exists()) cacheDir.mkdirs(); } public File getFile(String url){ //I identify images by hashcode. Not a perfect solution, good for the demo. String filename=String.valueOf(url.hashCode()); //Another possible solution (thanks to grantland) //String filename = URLEncoder.encode(url); File f = new File(cacheDir, filename); return f; } public void clear(){ File[] files=cacheDir.listFiles(); if(files==null) return; for(File f:files) f.delete(); } }
sokanacademy forum
کاربر سکان آکادمی 8 سال پیش
کدهای فایل ImageLoader.java به صورت زیر می باشد : public class ImageLoader { MemoryCache memoryCache=new MemoryCache(); FileCache fileCache; private Map imageViews=Collections.synchronizedMap(new WeakHashMap()); ExecutorService executorService; public ImageLoader(Context context){ fileCache=new FileCache(context); executorService=Executors.newFixedThreadPool(5); } final int stub_id=R.drawable.ic_launcher; public void DisplayImage(String url, ImageView imageView) { imageViews.put(imageView, url); Bitmap bitmap=memoryCache.get(url); if(bitmap!=null) imageView.setImageBitmap(bitmap); else { queuePhoto(url, imageView); imageView.setImageResource(stub_id); } } private void queuePhoto(String url, ImageView imageView) { PhotoToLoad p=new PhotoToLoad(url, imageView); executorService.submit(new PhotosLoader(p)); } private Bitmap getBitmap(String url) { File f=fileCache.getFile(url); //from SD cache Bitmap b = decodeFile(f); if(b!=null) return b; //from web try { Bitmap bitmap=null; URL imageUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setInstanceFollowRedirects(true); InputStream is=conn.getInputStream(); OutputStream os = new FileOutputStream(f); Utils.CopyStream(is, os); os.close(); bitmap = decodeFile(f); return bitmap; } catch (Throwable ex){ ex.printStackTrace(); if(ex instanceof OutOfMemoryError) memoryCache.clear(); return null; } } //decodes image and scales it to reduce memory consumption private Bitmap decodeFile(File f){ try { //decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream(f),null,o); //Find the correct scale value. It should be the power of 2. final int REQUIRED_SIZE=70; int width_tmp=o.outWidth, height_tmp=o.outHeight; int scale=1; while(true){ if(width_tmp/2
sokanacademy forum
کاربر سکان آکادمی 8 سال پیش

کدهای فایل MemoryCache.java به صورت زیر می باشد :

public class MemoryCache { private static final String TAG = "MemoryCache"; private Map<String, Bitmap> cache=Collections.synchronizedMap( new LinkedHashMap<String, Bitmap>(10,1.5f,true));//Last argument true for LRU ordering private long size=0;//current allocated size private long limit=1000000;//max memory in bytes public MemoryCache(){ //use 25% of available heap size setLimit(Runtime.getRuntime().maxMemory()/4); } public void setLimit(long new_limit){ limit=new_limit; Log.i(TAG, "MemoryCache will use up to "+limit/1024./1024.+"MB"); } public Bitmap get(String id){ try{ if(!cache.containsKey(id)) return null; //NullPointerException sometimes happen here http://code.google.com/p/osmdroid/issues/detail?id=78 return cache.get(id); }catch(NullPointerException ex){ ex.printStackTrace(); return null; } } public void put(String id, Bitmap bitmap){ try{ if(cache.containsKey(id)) size-=getSizeInBytes(cache.get(id)); cache.put(id, bitmap); size+=getSizeInBytes(bitmap); checkSize(); }catch(Throwable th){ th.printStackTrace(); } } private void checkSize() { Log.i(TAG, "cache size="+size+" length="+cache.size()); if(size>limit){ Iterator<Entry<String, Bitmap>> iter=cache.entrySet().iterator();//least recently accessed item will be the first one iterated while(iter.hasNext()){ Entry<String, Bitmap> entry=iter.next(); size-=getSizeInBytes(entry.getValue()); iter.remove(); if(size<=limit) break; } Log.i(TAG, "Clean cache. New size "+cache.size()); } } public void clear() { try{ //NullPointerException sometimes happen here http://code.google.com/p/osmdroid/issues/detail?id=78 cache.clear(); size=0; }catch(NullPointerException ex){ ex.printStackTrace(); } } long getSizeInBytes(Bitmap bitmap) { if(bitmap==null) return 0; return bitmap.getRowBytes() * bitmap.getHeight(); } }
sokanacademy forum
کاربر سکان آکادمی 8 سال پیش

کدهای فایل Utils.java به صورت زیر می باشد :

public class Utils { public static void CopyStream(InputStream is, OutputStream os) { final int buffer_size=1024; try { byte[] bytes=new byte[buffer_size]; for(;;) { int count=is.read(bytes, 0, buffer_size); if(count==-1) break; os.write(bytes, 0, count); } } catch(Exception ex){} } }
sokanacademy forum
کاربر سکان آکادمی 8 سال پیش

کدهای فایل MainActivity.java عبارتند از :

public class MainActivity extends Activity { private ImageLoader imgLoader; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imgLoader = new ImageLoader(this); // important ImageView iv_1 = (ImageView) findViewById(R.id.imageView1); String image_url_1 = "http://vw2.ir/image_1.jpg"; imgLoader.DisplayImage(image_url_1, iv_1); } }

خیلی ببخشید که طولانی است ولی ممنون می شوم پاسخم را بدهید.(خودم فکر می کنم که باید در فایل جاوای ImageLoader.java تغیراتی ایجاد کنم)

sokanacademy forum
کاربر سکان آکادمی 8 سال پیش
سلام،از این استراچر برو : public File getCacheFolder(Context context) { File cacheDir = null; if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { cacheDir = new File(Environment.getExternalStorageDirectory(), "cachefolder"); if(!cacheDir.isDirectory()) { cacheDir.mkdirs(); } } if(!cacheDir.isDirectory()) { cacheDir = context.getCacheDir(); //get system cache folder } return cacheDir; } http://jmsliu.com/1954/android-save-downloading-file-locally.html فقط قدم به قدم برو جواب می گیری
sokanacademy forum
کاربر سکان آکادمی 8 سال پیش
باسلام و عرض خسته نباشید ببخشید که دیر جواب دادم . یعنی اگه کدهایی را که برایم فرستادید فقط بذارم کاری را که می خواهم بکنم می کند؟ یه مطلب دیگر در کجا باید آدرس فایل صوتی موجود در اینترنت را لذارم تا از اون دانلودش کند؟ باتشکر فراوان از پاسخگویی بی نظیرتان
sokanacademy forum
کاربر سکان آکادمی 8 سال پیش
سلام کلا سورس به صورت کلاس ها ومتد تعریف می کنه URL wallpaperURL = new URL(wallpaperURLStr); URLConnection connection = wallpaperURL.openConnection(); = InputStream inputStream = new BufferedInputStream(wallpaperURL.openStream(), 10240); کلاس URL را تعریف کرده بعد متد کانکت و ادامه کار
online-support-icon