List of usage examples for android.content.res AssetFileDescriptor getLength
public long getLength()
From source file:Main.java
/** * Play video file from res folder.//from ww w.ja v a 2s .c o m * Then call mediaPlayer.start(); * @param fileName * @param listener * @return * @throws Exception */ public static MediaPlayer playSound(AssetManager assetManager, String fileName, MediaPlayer.OnCompletionListener listener) throws Exception { MediaPlayer mediaPlayer = new MediaPlayer(); if (listener != null) { mediaPlayer.setOnCompletionListener(listener); } AssetFileDescriptor descriptor = assetManager.openFd(fileName); mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength()); descriptor.close(); mediaPlayer.prepare(); return mediaPlayer; }
From source file:com.phonegap.plugins.xapkreader.XAPKReader.java
private static byte[] readFile(Context ctx, String filename) throws IOException { // Get APKExpensionFile ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(ctx, XAPKReader.mainVersion, XAPKReader.patchVersion);/*from w w w.j a va2s . c o m*/ if (null == expansionFile) { Log.e("XAPKReader", "APKExpansionFile not found."); return null; } // Find file in ExpansionFile String fileName = Helpers.getExpansionAPKFileName(ctx, true, XAPKReader.patchVersion); fileName = fileName.substring(0, fileName.lastIndexOf(".")); AssetFileDescriptor file = expansionFile.getAssetFileDescriptor(fileName + "/" + filename); if (null == file) { Log.e("XAPKReader", "File not found (" + filename + ")."); return null; } // Read file int size = (int) file.getLength(); byte[] data = new byte[size]; BufferedInputStream buf = new BufferedInputStream(file.createInputStream()); buf.read(data, 0, data.length); buf.close(); return data; }
From source file:Main.java
public static void playNotifycationMusic(Context context, String voicePath) throws IOException { // paly music ... AssetFileDescriptor fileDescriptor = context.getAssets().openFd(voicePath); if (mediaPlayer == null) { mediaPlayer = new MediaPlayer(); }/*www . j ava2s. com*/ if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); } mediaPlayer.reset(); mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength()); mediaPlayer.prepare(); mediaPlayer.setLooping(false); mediaPlayer.start(); }
From source file:org.kontalk.util.MediaStorage.java
public static long getLength(Context context, Uri media) throws IOException { AssetFileDescriptor stat = null; long length = 0; try {/*from ww w . j a va 2 s . com*/ stat = context.getContentResolver().openAssetFileDescriptor(media, "r"); if (stat != null) length = stat.getLength(); } finally { try { if (stat != null) stat.close(); } catch (IOException e) { // ignored } } if (length == 0) { // try to count bytes by reading it InputStream in = null; try { in = context.getContentResolver().openInputStream(media); CountingInputStream counter = new CountingInputStream(in); counter.consume(); length = counter.getByteCount(); } finally { try { if (in != null) in.close(); } catch (IOException e) { // ignored } } } return length; }
From source file:com.xlythe.engine.theme.Theme.java
public static long getDurationOfSound(Context context, Theme.Res res) { int millis = 0; MediaPlayer mp = new MediaPlayer(); try {/*from w w w .ja va2 s. c o m*/ AssetFileDescriptor afd; int id = getId(context, res.getType(), res.getName()); if (id == 0) { id = context.getResources().getIdentifier(res.getName(), res.getType(), context.getPackageName()); afd = context.getResources().openRawResourceFd(id); } afd = getThemeContext(context).getResources().openRawResourceFd(id); mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); afd.close(); mp.prepare(); millis = mp.getDuration(); } catch (Exception e) { e.printStackTrace(); } finally { mp.release(); mp = null; } return millis; }
From source file:com.aujur.ebookreader.catalog.DownloadLocalFileTask.java
@Override protected String doInBackground(String... params) { try {/*from w w w. j a v a2 s. com*/ String url = params[0]; // url = "vademecum/Editora AuJur/Codigo Civil (1)/Codigo Civil - Editora AuJur.epub"; // vademecum/Editora AuJur/Codigo Civil (1)/Codigo Civil - Editora AuJur.epub LOG.debug("Downloading: " + url); String fileName = url.substring(url.lastIndexOf('/') + 1); fileName = fileName.replaceAll("\\?|&|=", "_"); File destFolder = new File(config.getDownloadsFolder()); if (!destFolder.exists()) { destFolder.mkdirs(); } /** * Make sure we always store downloaded files as .epub, so they show * up in scans later on. */ if (!fileName.endsWith(".epub")) { fileName = fileName + ".epub"; } destFile = new File(destFolder, URLDecoder.decode(fileName)); if (destFile.exists()) { destFile.delete(); } // lenghtOfFile is used for calculating download progress AssetFileDescriptor fd = context.getAssets().openFd(url); long lenghtOfFile = fd.getLength(); // this is where the file will be seen after the download FileOutputStream f = new FileOutputStream(destFile); try { // file input is from the url AssetManager assetManager = context.getAssets(); InputStream in = assetManager.open(url); // here's the download code byte[] buffer = new byte[1024]; int len1 = 0; long total = 0; while ((len1 = in.read(buffer)) > 0 && !isCancelled()) { // Make sure the user can cancel the download. if (isCancelled()) { return null; } total += len1; publishProgress(total, lenghtOfFile, (long) ((total * 100) / lenghtOfFile)); f.write(buffer, 0, len1); } } finally { f.close(); } if (!isCancelled()) { // FIXME: This doesn't belong here really... Book book = new EpubReader().readEpubLazy(destFile.getAbsolutePath(), "UTF-8"); libraryService.storeBook(destFile.getAbsolutePath(), book, false, config.isCopyToLibrayEnabled()); } } catch (Exception e) { LOG.error("Download failed.", e); this.failure = e; } return null; }
From source file:com.galois.qrstream.MainActivity.java
private byte[] readFileUri(Uri uri) throws IOException { ContentResolver contentResolver = getContentResolver(); AssetFileDescriptor fd = contentResolver.openAssetFileDescriptor(uri, "r"); long fileLength = fd.getLength(); Log.d(Constants.APP_TAG, "fd length " + fileLength); DataInputStream istream = new DataInputStream(contentResolver.openInputStream(uri)); byte[] buf = new byte[(int) fileLength]; istream.readFully(buf);// w w w . j a va 2 s . c o m return buf; }
From source file:it.interfree.leonardoce.bootreceiver.AlarmKlaxon.java
private void setDataSourceFromResource(Resources resources, MediaPlayer player, int res) throws java.io.IOException { AssetFileDescriptor afd = resources.openRawResourceFd(res); if (afd != null) { player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); afd.close();/* w ww.j ava2 s. c o m*/ } }
From source file:com.wsi.audiodemo.ScrollingActivity.java
private void playSound2(String assetName) { try {/*from ww w. j a v a 2 s. c o m*/ // Syntax : android.resource://[package]/[res type]/[res name] // Example : Uri.parse("android.resource://com.my.package/raw/sound1"); // // Syntax : android.resource://[package]/[resource_id] // Example : Uri.parse("android.resource://com.my.package/" + R.raw.sound1); String RESOURCE_PATH = ContentResolver.SCHEME_ANDROID_RESOURCE + "://"; String path; if (false) { path = RESOURCE_PATH + getPackageName() + "/raw/" + assetName; } else { int resID = getResources().getIdentifier(assetName, "raw", getPackageName()); path = RESOURCE_PATH + getPackageName() + File.separator + resID; } Uri soundUri = Uri.parse(path); mSoundName.setText(path); mMediaPlayer = new MediaPlayer(); if (true) { mMediaPlayer.setDataSource(getApplicationContext(), soundUri); mMediaPlayer.prepare(); } else if (false) { // How to load external audio files. // "/sdcard/sample.mp3"; // "http://www.bogotobogo.com/Audio/sample.mp3"; mMediaPlayer.setDataSource(path); mMediaPlayer.prepare(); } else { ContentResolver resolver = getContentResolver(); AssetFileDescriptor afd = resolver.openAssetFileDescriptor(soundUri, "r"); mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); afd.close(); } mMediaPlayer.start(); } catch (Exception ex) { Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show(); } }
From source file:com.adkdevelopment.simpleflashlightadfree.ui.EmergencyFragment.java
/** * Starts emergency sound from assets/*w w w .ja va 2 s.c o m*/ */ public void emergencySignal() { try { if (mMediaPlayer != null && mMediaPlayer.isPlaying() && status == FlashlightService.STATUS_OFF) { mMediaPlayer.stop(); mMediaPlayer.reset(); mMediaPlayer.release(); mMediaPlayer = null; mLinearLayout.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorBackground)); } else if (status == FlashlightService.STATUS_BLINK) { final AnimationDrawable drawable = new AnimationDrawable(); final Handler handler = new Handler(); drawable.addFrame(new ColorDrawable(Color.RED), 400); drawable.addFrame(new ColorDrawable(Color.BLUE), 400); drawable.setOneShot(false); mLinearLayout.setBackground(drawable); handler.postDelayed(new Runnable() { @Override public void run() { drawable.start(); } }, 100); mMediaPlayer = new MediaPlayer(); AssetFileDescriptor descriptor = getActivity().getAssets().openFd("sews.mp3"); mMediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength()); descriptor.close(); mMediaPlayer.prepare(); mMediaPlayer.setVolume(1f, 1f); mMediaPlayer.setLooping(true); mMediaPlayer.start(); } } catch (Exception e) { e.printStackTrace(); } }