Search in sources :

Example 86 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 87 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project wechat by motianhuo.

the class CaptureActivity method initBeepSound.

/**
 * 扫描正确后的震动声音,如果感觉apk大了,可以删除
 */
private void initBeepSound() {
    if (playBeep && mediaPlayer == null) {
        // The volume on STREAM_SYSTEM is not adjustable, and users found it
        // too loud,
        // so we now play on the music stream.
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnCompletionListener(beepListener);
        AssetFileDescriptor file = 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 e) {
            mediaPlayer = null;
        }
    }
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException) MediaPlayer(android.media.MediaPlayer)

Example 88 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project Lazy by l123456789jy.

the class MediaPlayerUtiles method playAudio.

/**
 * 播放音频
 */
public static void playAudio(Context mContext, String fileName) {
    try {
        // 如果正在播放就停止
        stopAudio();
        AssetManager assetManager = mContext.getAssets();
        AssetFileDescriptor afd = assetManager.openFd(fileName);
        MediaPlayer mediaPlayer = getMediaPlayer();
        mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        // 循环播放
        mediaPlayer.setLooping(false);
        mediaPlayer.prepare();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.start();
            }
        });
    } catch (Exception e) {
        Log.e("播放音频失败", "");
    }
}
Also used : AssetManager(android.content.res.AssetManager) AssetFileDescriptor(android.content.res.AssetFileDescriptor) MediaPlayer(android.media.MediaPlayer)

Example 89 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project Aegis by Decad3nce.

the class AlarmService method alarmNotification.

@SuppressWarnings("deprecation")
private void alarmNotification(Context context) {
    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    NotificationManager mManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher, context.getResources().getString(R.string.receiver_alarm_override), System.currentTimeMillis());
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    boolean vibrate = preferences.getBoolean(SMSAlarmFragment.PREFERENCES_ALARM_VIBRATE, Boolean.parseBoolean(context.getResources().getString(R.string.config_default_alarm_vibrate)));
    int duration = Integer.parseInt(preferences.getString(SMSAlarmFragment.PREFERENCES_ALARM_DURATION, getResources().getString(R.string.config_default_alarm_duration)));
    int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
    am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, maxVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
    maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_RING);
    am.setStreamVolume(AudioManager.STREAM_RING, maxVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
    Intent i = new Intent(context, AlarmService.class);
    i.putExtra("stop", true);
    PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
    notification.setLatestEventInfo(context, "aeGis", context.getResources().getString(R.string.receiver_alarm_override), pi);
    ;
    if (vibrate) {
        notification.vibrate = new long[] { 100, 200, 100, 500 };
    }
    AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.alarm);
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
    mMediaPlayer.setLooping(true);
    try {
        mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        mMediaPlayer.prepare();
    } catch (IOException e) {
        stopForeground(true);
        mMediaPlayer.release();
        return;
    }
    mMediaPlayer.start();
    // Set duration time-out
    mHandler.postDelayed(new Runnable() {

        @Override
        public void run() {
            stopMediaPlayer();
            stopForeground(true);
        }
    }, duration * 1000);
    notification.flags |= Notification.FLAG_NO_CLEAR;
    startForeground(1242, notification);
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) NotificationManager(android.app.NotificationManager) SharedPreferences(android.content.SharedPreferences) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IOException(java.io.IOException) Notification(android.app.Notification) AudioManager(android.media.AudioManager) PendingIntent(android.app.PendingIntent) MediaPlayer(android.media.MediaPlayer)

Example 90 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project webimageloader by lexs.

the class ContentURLConnection method getContentLength.

@Override
public int getContentLength() {
    try {
        AssetFileDescriptor fd = mResolver.openAssetFileDescriptor(mUri, "r");
        long length = fd.getLength();
        if (length <= 0 && length <= Integer.MAX_VALUE) {
            return (int) length;
        }
    } catch (IOException e) {
    }
    return -1;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) IOException(java.io.IOException)

Aggregations

AssetFileDescriptor (android.content.res.AssetFileDescriptor)262 IOException (java.io.IOException)147 ParcelFileDescriptor (android.os.ParcelFileDescriptor)57 FileNotFoundException (java.io.FileNotFoundException)56 MediaPlayer (android.media.MediaPlayer)44 ContentResolver (android.content.ContentResolver)36 Test (org.junit.Test)31 Uri (android.net.Uri)30 FileInputStream (java.io.FileInputStream)30 InputStream (java.io.InputStream)23 RemoteException (android.os.RemoteException)22 File (java.io.File)21 Bundle (android.os.Bundle)16 FileDescriptor (java.io.FileDescriptor)16 Nullable (android.annotation.Nullable)15 Bitmap (android.graphics.Bitmap)15 Parcel (android.os.Parcel)14 Resources (android.content.res.Resources)13 BitmapFactory (android.graphics.BitmapFactory)13 DeadObjectException (android.os.DeadObjectException)12