Search in sources :

Example 21 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project robolectric by robolectric.

the class ShadowAssetManagerTest method openFd_shouldProvideFileDescriptorForAsset.

@Test
public void openFd_shouldProvideFileDescriptorForAsset() throws Exception {
    AssetFileDescriptor assetFileDescriptor = assetManager.openFd("assetsHome.txt");
    assertThat(Strings.fromStream(assetFileDescriptor.createInputStream())).isEqualTo("assetsHome!");
    assertThat(assetFileDescriptor.getLength()).isEqualTo(11);
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) Test(org.junit.Test)

Example 22 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project android-ocr by rmtheis.

the class BeepManager method buildMediaPlayer.

private static MediaPlayer buildMediaPlayer(Context activity) {
    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    // When the beep has finished playing, rewind to queue up another one.
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        public void onCompletion(MediaPlayer player) {
            player.seekTo(0);
        }
    });
    AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
    try {
        mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
        file.close();
        mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
        mediaPlayer.prepare();
    } catch (IOException ioe) {
        Log.w(TAG, ioe);
        mediaPlayer = null;
    }
    return mediaPlayer;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException) MediaPlayer(android.media.MediaPlayer)

Example 23 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project platform_frameworks_base by android.

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.&lt;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;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException)

Example 24 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project platform_frameworks_base by android.

the class MediaPlayer method addTimedTextSource.

/**
     * Adds an external timed text source file (Uri).
     *
     * Currently supported format is SubRip with the file extension .srt, case insensitive.
     * Note that a single external timed text source may contain multiple tracks in it.
     * One can find the total number of available tracks using {@link #getTrackInfo()} to see what
     * additional tracks become available after this method call.
     *
     * @param context the Context to use when resolving the Uri
     * @param uri the Content URI of the data you want to play
     * @param mimeType The mime type of the file. Must be one of the mime types listed above.
     * @throws IOException if the file cannot be accessed or is corrupted.
     * @throws IllegalArgumentException if the mimeType is not supported.
     * @throws IllegalStateException if called in an invalid state.
     */
public void addTimedTextSource(Context context, Uri uri, String mimeType) throws IOException, IllegalArgumentException, IllegalStateException {
    String scheme = uri.getScheme();
    if (scheme == null || scheme.equals("file")) {
        addTimedTextSource(uri.getPath(), mimeType);
        return;
    }
    AssetFileDescriptor fd = null;
    try {
        ContentResolver resolver = context.getContentResolver();
        fd = resolver.openAssetFileDescriptor(uri, "r");
        if (fd == null) {
            return;
        }
        addTimedTextSource(fd.getFileDescriptor(), mimeType);
        return;
    } catch (SecurityException ex) {
    } catch (IOException ex) {
    } finally {
        if (fd != null) {
            fd.close();
        }
    }
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException) ContentResolver(android.content.ContentResolver)

Example 25 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project platform_frameworks_base by android.

the class Ringtone method playFallbackRingtone.

private boolean playFallbackRingtone() {
    if (mAudioManager.getStreamVolume(AudioAttributes.toLegacyStreamType(mAudioAttributes)) != 0) {
        int ringtoneType = RingtoneManager.getDefaultType(mUri);
        if (ringtoneType == -1 || RingtoneManager.getActualDefaultRingtoneUri(mContext, ringtoneType) != null) {
            // Default ringtone, try fallback ringtone.
            try {
                AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(com.android.internal.R.raw.fallbackring);
                if (afd != null) {
                    mLocalPlayer = new MediaPlayer();
                    if (afd.getDeclaredLength() < 0) {
                        mLocalPlayer.setDataSource(afd.getFileDescriptor());
                    } else {
                        mLocalPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
                    }
                    mLocalPlayer.setAudioAttributes(mAudioAttributes);
                    synchronized (mPlaybackSettingsLock) {
                        applyPlaybackProperties_sync();
                    }
                    mLocalPlayer.prepare();
                    startLocalPlayer();
                    afd.close();
                    return true;
                } else {
                    Log.e(TAG, "Could not load fallback ringtone");
                }
            } catch (IOException ioe) {
                destroyLocalPlayer();
                Log.e(TAG, "Failed to open fallback ringtone");
            } catch (NotFoundException nfe) {
                Log.e(TAG, "Fallback ringtone does not exist");
            }
        } else {
            Log.w(TAG, "not playing fallback for " + mUri);
        }
    }
    return false;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) NotFoundException(android.content.res.Resources.NotFoundException) IOException(java.io.IOException)

Aggregations

AssetFileDescriptor (android.content.res.AssetFileDescriptor)187 IOException (java.io.IOException)98 ParcelFileDescriptor (android.os.ParcelFileDescriptor)49 FileNotFoundException (java.io.FileNotFoundException)41 Test (org.junit.Test)28 MediaPlayer (android.media.MediaPlayer)27 ContentResolver (android.content.ContentResolver)26 RemoteException (android.os.RemoteException)19 File (java.io.File)18 FileInputStream (java.io.FileInputStream)16 Nullable (android.annotation.Nullable)15 Parcel (android.os.Parcel)14 Bitmap (android.graphics.Bitmap)13 FileDescriptor (java.io.FileDescriptor)13 DeadObjectException (android.os.DeadObjectException)12 Resources (android.content.res.Resources)11 FileOutputStream (java.io.FileOutputStream)11 InputStream (java.io.InputStream)11 Uri (android.net.Uri)10 Bundle (android.os.Bundle)10