Example usage for android.content.res AssetManager openFd

List of usage examples for android.content.res AssetManager openFd

Introduction

In this page you can find the example usage for android.content.res AssetManager openFd.

Prototype

public @NonNull AssetFileDescriptor openFd(@NonNull String fileName) throws IOException 

Source Link

Document

Open an uncompressed asset by mmapping it and returning an AssetFileDescriptor .

Usage

From source file:Main.java

/**
 * Play video file from res folder./*from  w w w .j  ava  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:at.wada811.app.fragment.VideoFragment.java

public void setDateSource(AssetManager assets, String fileName)
        throws IllegalArgumentException, IllegalStateException, IOException {
    mMediaPlayer.setDataSource(assets.openFd(fileName).getFileDescriptor());
}

From source file:com.google.android.gms.samples.vision.face.sleepAlert.DAssistActivity.java

/**
 * Initializes the UI and initiates the creation of a face detector.
 *///from   w  w  w .  j  av  a 2 s  .  c o  m
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    mPreview = (CameraSourcePreview) findViewById(R.id.preview);
    mGraphicOverlay = (GraphicOverlay) findViewById(R.id.faceOverlay);
    this.jumbleButton = (Button) findViewById(R.id.Button01);
    this.exitButton = (Button) findViewById(R.id.button);
    this.mp = new MediaPlayer();
    this.tv = (TextView) findViewById(R.id.DisplayMsg);
    this.layout = (RelativeLayout) findViewById(R.id.topLayout);
    this.sc = (CameraSourcePreview) findViewById(R.id.preview);
    this.go = (GraphicOverlay) findViewById(R.id.faceOverlay);
    AssetManager am = getApplicationContext().getAssets();
    AssetFileDescriptor afd = null;
    try {
        afd = am.openFd("Song.mp3");
        this.mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        this.mp.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Check for the camera permission before accessing the camera.  If the
    // permission is not granted yet, request permission.
    int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
    if (rc == PackageManager.PERMISSION_GRANTED) {
        createCameraSource();
    } else {
        requestCameraPermission();
    }
    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(final MenuItem menuItem) {
            System.out.println("Srini in onNavigationItemSelected part");
            // Handle navigation view item clicks here.
            int id = menuItem.getItemId();

            if (id == R.id.nav_slideshow) {
                // Handle the camera action
            } else if (id == R.id.nav_manage) {

            } else if (id == R.id.nav_manage_toggle) {

                if (checked == 0) {
                    go.setBackgroundColor(Color.TRANSPARENT);
                    checked = 1;
                } else {
                    go.setBackgroundColor(Color.BLUE);
                    checked = 0;

                }

            }

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }

    });
}

From source file:org.cyanogenmod.theme.chooser.AudiblePreviewFragment.java

private void loadThemeAudible(Context themeCtx, int type, PackageInfo pi) {
    AssetManager assetManager = themeCtx.getAssets();
    String assetPath;//w  w w .j a v a2 s .  c  om
    switch (type) {
    case RingtoneManager.TYPE_ALARM:
        assetPath = "alarms";
        break;
    case RingtoneManager.TYPE_NOTIFICATION:
        assetPath = "notifications";
        break;
    case RingtoneManager.TYPE_RINGTONE:
        assetPath = "ringtones";
        break;
    default:
        assetPath = null;
        break;
    }
    if (assetPath != null) {
        try {
            String[] assetList = assetManager.list(assetPath);
            if (assetList != null && assetList.length > 0) {
                AssetFileDescriptor afd = assetManager.openFd(assetPath + File.separator + assetList[0]);
                MediaPlayer mp = initAudibleMediaPlayer(afd, type);
                if (mp != null) {
                    addAudibleToLayout(type, mp);
                }
            }
        } catch (IOException e) {
            mMediaPlayers.put(type, null);
        }
    }
}