use of android.content.res.AssetFileDescriptor in project android_frameworks_base by ParanoidAndroid.
the class MediaExtractor method setDataSource.
/**
* Sets the data source as a content Uri.
*
* @param context the Context to use when resolving the Uri
* @param uri the Content URI of the data you want to extract from.
* @param headers the headers to be sent together with the request for the data
*/
public final void setDataSource(Context context, Uri uri, Map<String, String> headers) throws IOException {
String scheme = uri.getScheme();
if (scheme == null || scheme.equals("file")) {
setDataSource(uri.getPath());
return;
}
AssetFileDescriptor fd = null;
try {
ContentResolver resolver = context.getContentResolver();
fd = resolver.openAssetFileDescriptor(uri, "r");
if (fd == null) {
return;
}
// a full file.
if (fd.getDeclaredLength() < 0) {
setDataSource(fd.getFileDescriptor());
} else {
setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getDeclaredLength());
}
return;
} catch (SecurityException ex) {
} catch (IOException ex) {
} finally {
if (fd != null) {
fd.close();
}
}
setDataSource(uri.toString(), headers);
}
use of android.content.res.AssetFileDescriptor in project android_frameworks_base by ResurrectionRemix.
the class SoundPool method load.
/**
* Load the sound from the specified APK resource.
*
* Note that the extension is dropped. For example, if you want to load
* a sound from the raw resource file "explosion.mp3", you would specify
* "R.raw.explosion" as the resource ID. Note that this means you cannot
* have both an "explosion.wav" and an "explosion.mp3" in the res/raw
* directory.
*
* @param context the application context
* @param resId the resource ID
* @param priority the priority of the sound. Currently has no effect. Use
* a value of 1 for future compatibility.
* @return a sound ID. This value can be used to play or unload the sound.
*/
public int load(Context context, int resId, int priority) {
AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
int id = 0;
if (afd != null) {
id = _load(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength(), priority);
try {
afd.close();
} catch (java.io.IOException ex) {
//Log.d(TAG, "close failed:", ex);
}
}
return id;
}
use of android.content.res.AssetFileDescriptor in project android_frameworks_base by ResurrectionRemix.
the class MediaPlayer method create.
/**
* Same factory method as {@link #create(Context, int)} but that lets you specify the audio
* attributes and session ID to be used by the new MediaPlayer instance.
* @param context the Context to use
* @param resid the raw resource id (<var>R.raw.<something></var>) for
* the resource to use as the datasource
* @param audioAttributes the {@link AudioAttributes} to be used by the media player.
* @param audioSessionId the audio session ID to be used by the media player,
* see {@link AudioManager#generateAudioSessionId()} to obtain a new session.
* @return a MediaPlayer object, or null if creation failed
*/
public static MediaPlayer create(Context context, int resid, AudioAttributes audioAttributes, int audioSessionId) {
try {
AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
if (afd == null)
return null;
MediaPlayer mp = new MediaPlayer();
final AudioAttributes aa = audioAttributes != null ? audioAttributes : new AudioAttributes.Builder().build();
mp.setAudioAttributes(aa);
mp.setAudioSessionId(audioSessionId);
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
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;
}
use of android.content.res.AssetFileDescriptor in project android_frameworks_base by ResurrectionRemix.
the class SimplePlayer method playOrPause.
public void playOrPause() {
if (mMediaPlayer == null || !mMediaPlayer.isPlaying()) {
if (mMediaPlayer == null) {
try {
mMediaPlayer = new MediaPlayer();
if (mSession != 0) {
mMediaPlayer.setAudioSessionId(mSession);
Log.d(TAG, "mMediaPlayer.setAudioSessionId(): " + mSession);
}
if (mFileName.equals("")) {
Log.d(TAG, "Playing from resource");
AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(mFileResId);
mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
} else {
Log.d(TAG, "Playing file: " + mFileName);
mMediaPlayer.setDataSource(mFileName);
}
mMediaPlayer.setAudioStreamType(mStreamType);
mMediaPlayer.prepare();
mMediaPlayer.setLooping(true);
} catch (IOException ex) {
Log.e(TAG, "mMediaPlayercreate failed:", ex);
mMediaPlayer = null;
} catch (IllegalArgumentException ex) {
Log.e(TAG, "mMediaPlayercreate failed:", ex);
mMediaPlayer = null;
} catch (SecurityException ex) {
Log.e(TAG, "mMediaPlayercreate failed:", ex);
mMediaPlayer = null;
}
if (mMediaPlayer != null) {
mMediaPlayer.setAuxEffectSendLevel(mSendLevel);
mMediaPlayer.attachAuxEffect(mEffectId);
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
updatePlayPauseButton();
}
});
mSessionText.setText("Session: " + Integer.toString(mMediaPlayer.getAudioSessionId()));
}
}
if (mMediaPlayer != null) {
mMediaPlayer.start();
}
} else {
mMediaPlayer.pause();
}
updatePlayPauseButton();
}
use of android.content.res.AssetFileDescriptor in project android_frameworks_base by ResurrectionRemix.
the class DocumentsContract method getDocumentThumbnail.
/** {@hide} */
public static Bitmap getDocumentThumbnail(ContentProviderClient client, Uri documentUri, Point size, CancellationSignal signal) throws RemoteException, IOException {
final Bundle openOpts = new Bundle();
openOpts.putParcelable(ContentResolver.EXTRA_SIZE, size);
AssetFileDescriptor afd = null;
Bitmap bitmap = null;
try {
afd = client.openTypedAssetFileDescriptor(documentUri, "image/*", openOpts, signal);
final FileDescriptor fd = afd.getFileDescriptor();
final long offset = afd.getStartOffset();
// Try seeking on the returned FD, since it gives us the most
// optimal decode path; otherwise fall back to buffering.
BufferedInputStream is = null;
try {
Os.lseek(fd, offset, SEEK_SET);
} catch (ErrnoException e) {
is = new BufferedInputStream(new FileInputStream(fd), THUMBNAIL_BUFFER_SIZE);
is.mark(THUMBNAIL_BUFFER_SIZE);
}
// We requested a rough thumbnail size, but the remote size may have
// returned something giant, so defensively scale down as needed.
final BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
if (is != null) {
BitmapFactory.decodeStream(is, null, opts);
} else {
BitmapFactory.decodeFileDescriptor(fd, null, opts);
}
final int widthSample = opts.outWidth / size.x;
final int heightSample = opts.outHeight / size.y;
opts.inJustDecodeBounds = false;
opts.inSampleSize = Math.min(widthSample, heightSample);
if (is != null) {
is.reset();
bitmap = BitmapFactory.decodeStream(is, null, opts);
} else {
try {
Os.lseek(fd, offset, SEEK_SET);
} catch (ErrnoException e) {
e.rethrowAsIOException();
}
bitmap = BitmapFactory.decodeFileDescriptor(fd, null, opts);
}
// Transform the bitmap if requested. We use a side-channel to
// communicate the orientation, since EXIF thumbnails don't contain
// the rotation flags of the original image.
final Bundle extras = afd.getExtras();
final int orientation = (extras != null) ? extras.getInt(EXTRA_ORIENTATION, 0) : 0;
if (orientation != 0) {
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
final Matrix m = new Matrix();
m.setRotate(orientation, width / 2, height / 2);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, false);
}
} finally {
IoUtils.closeQuietly(afd);
}
return bitmap;
}
Aggregations