use of android.content.res.AssetFileDescriptor in project LuaViewSDK by alibaba.
the class UDAudio method play.
/**
* start playing audio
*
* @param uriOrName
* @param loopTimes
* @return
*/
public synchronized UDAudio play(String uriOrName, Integer loopTimes) {
stopAndReset();
if (uriOrName != null && uriOrName.equals(this.mUriOrName) == false) {
// url 不同
this.mUriOrName = uriOrName;
}
if (loopTimes != null) {
this.mLoopTimes = loopTimes;
}
if (this.mUriOrName != null) {
final MediaPlayer player = getMediaPlayer();
if (player != null && player.isPlaying() == false) {
String uri = null;
boolean assetFileExist = false;
if (URLUtil.isNetworkUrl(this.mUriOrName) || URLUtil.isFileUrl(this.mUriOrName) || URLUtil.isAssetUrl(this.mUriOrName)) {
// net & file & asset
uri = this.mUriOrName;
} else {
// plain text, use as file path
uri = getLuaResourceFinder().buildFullPathInBundleOrAssets(this.mUriOrName);
assetFileExist = AssetUtil.exists(getContext(), uri);
}
try {
if (assetFileExist) {
final AssetFileDescriptor descriptor = getContext().getAssets().openFd(uri);
player.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
} else {
player.setDataSource(uri);
}
player.setOnErrorListener(this);
player.setOnCompletionListener(this);
player.setOnPreparedListener(this);
player.setLooping((this.mLoopTimes != null && this.mLoopTimes > 1) ? true : false);
player.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return this;
}
use of android.content.res.AssetFileDescriptor in project u2020 by JakeWharton.
the class MockRequestHandler method load.
@Override
public Result load(Request request, int networkPolicy) throws IOException {
// Grab only the path sans leading slash.
String imagePath = request.uri.getPath().substring(1);
// Check the disk cache for the image. A non-null return value indicates a hit.
boolean cacheHit = emulatedDiskCache.get(imagePath) != null;
// If there's a hit, grab the image stream and return it.
if (cacheHit) {
return new Result(loadBitmap(imagePath), Picasso.LoadedFrom.DISK);
}
// If we are not allowed to hit the network and the cache missed return a big fat nothing.
if (NetworkPolicy.isOfflineOnly(networkPolicy)) {
return null;
}
// to fake an network error.
if (behavior.calculateIsFailure()) {
SystemClock.sleep(behavior.calculateDelay(MILLISECONDS));
throw new IOException("Fake network error!");
}
// We aren't throwing a network error so fake a round trip delay.
SystemClock.sleep(behavior.calculateDelay(MILLISECONDS));
// Since we cache missed put it in the LRU.
AssetFileDescriptor fileDescriptor = assetManager.openFd(imagePath);
long size = fileDescriptor.getLength();
fileDescriptor.close();
emulatedDiskCache.put(imagePath, size);
// Grab the image stream and return it.
return new Result(loadBitmap(imagePath), Picasso.LoadedFrom.NETWORK);
}
use of android.content.res.AssetFileDescriptor in project NotificationPeekPort by lzanita09.
the class ContactHelper method openDisplayPhoto.
/**
* Get the InputStream object of the contact photo with given contact ID.
*
* @param context Context object of the caller.
* @param contactId Contact ID.
* @return InputStream object of the contact photo.
*/
public static InputStream openDisplayPhoto(Context context, long contactId) {
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
Uri displayPhotoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
try {
AssetFileDescriptor fd = context.getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r");
return fd.createInputStream();
} catch (IOException e) {
return null;
}
}
use of android.content.res.AssetFileDescriptor in project aFileChooser by iPaulPro.
the class LocalStorageProvider method openDocumentThumbnail.
@Override
public AssetFileDescriptor openDocumentThumbnail(final String documentId, final Point sizeHint, final CancellationSignal signal) throws FileNotFoundException {
// Assume documentId points to an image file. Build a thumbnail no
// larger than twice the sizeHint
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(documentId, options);
final int targetHeight = 2 * sizeHint.y;
final int targetWidth = 2 * sizeHint.x;
final int height = options.outHeight;
final int width = options.outWidth;
options.inSampleSize = 1;
if (height > targetHeight || width > targetWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// height and width larger than the requested height and width.
while ((halfHeight / options.inSampleSize) > targetHeight || (halfWidth / options.inSampleSize) > targetWidth) {
options.inSampleSize *= 2;
}
}
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(documentId, options);
// Write out the thumbnail to a temporary file
File tempFile = null;
FileOutputStream out = null;
try {
tempFile = File.createTempFile("thumbnail", null, getContext().getCacheDir());
out = new FileOutputStream(tempFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (IOException e) {
Log.e(LocalStorageProvider.class.getSimpleName(), "Error writing thumbnail", e);
return null;
} finally {
if (out != null)
try {
out.close();
} catch (IOException e) {
Log.e(LocalStorageProvider.class.getSimpleName(), "Error closing thumbnail", e);
}
}
// AssetFileDescriptor
return new AssetFileDescriptor(ParcelFileDescriptor.open(tempFile, ParcelFileDescriptor.MODE_READ_ONLY), 0, AssetFileDescriptor.UNKNOWN_LENGTH);
}
use of android.content.res.AssetFileDescriptor in project ABPlayer by winkstu.
the class MediaPlayerDemo_Audio method createMediaPlayer.
public MediaPlayer createMediaPlayer(Context context, int resid) {
try {
AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
MediaPlayer mp = new MediaPlayer(context);
mp.setDataSource(afd.getFileDescriptor());
afd.close();
mp.prepare();
return mp;
} catch (IOException ex) {
Log.d(TAG, "create failed:", ex);
// fall through
} catch (IllegalArgumentException ex) {
Log.d(TAG, "create failed:", ex);
// fall through
} catch (SecurityException ex) {
Log.d(TAG, "create failed:", ex);
// fall through
}
return null;
}
Aggregations