Use AssetFileDescriptor for Files in sdcard

1.5k views Asked by At

I am trying to implement user preference choice music in Super-powered app. I am unable to pull more music that is in sd-card via AssetFileDescriptor. Please guide so that I can access the user-Music data. I am unable to use FileDescriptor as it doesn't accept. Please help so I can access user Music via AssetFileDescriptor.

 String samplerateString = null, buffersizeString = null;
        if (Build.VERSION.SDK_INT >= 17) {
            AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
            samplerateString = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
            buffersizeString = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
        }
    if (samplerateString == null) samplerateString = "44100";
    if (buffersizeString == null) buffersizeString = "512";

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, SelectActivity.class);
            startActivityForResult(intent,1);
        }
    });
    String audioPath= Environment.getExternalStorageDirectory().getPath()+"/Music/x.mp3";
    Log.d("xyz",audioPath);


    try {
        FileDescriptor fd0 = context.getAssets().openFd(audioPath).getFileDescriptor();
        FileInputStream fis = new FileInputStream(audioPath);
        fd = fis.getFD();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    fd1 = getResources().openRawResourceFd(R.raw.nuyorica);

    long[] params = {
            fd0.getStartOffset(),
            fd0.getLength(),
            fd1.getStartOffset(),
            fd1.getLength(),
            Integer.parseInt(samplerateString),
            Integer.parseInt(buffersizeString)
    };
    try {

        fd0.getParcelFileDescriptor().close();
        fd1.getParcelFileDescriptor().close();
    } catch (IOException e) {}
    SuperpoweredExample(getPackageResourcePath(), params);

Thanks a lot in advance..

1

There are 1 answers

1
HimalayanCoder On
private static MappedByteBuffer loadModelFileFromSdcard(AssetManager assets, String modelFilename)
            throws IOException {
        File file = new File("sdcard/"+modelFilename);
        FileInputStream inputStream = new FileInputStream(file);
        FileChannel fileChannel = inputStream.getChannel();
        return fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
    }