在sdcard中使用AssetFileDescriptor for Files

时间:2015-06-17 01:18:54

标签: android audio file-descriptor

我正在尝试在超级应用中实现用户偏好选择音乐。 我无法通过AssetFileDescriptor提取sd-card中的更多音乐。 请引导以便我可以访问用户音乐数据。 我无法使用FileDescriptor,因为它不接受。 请帮助,以便我可以通过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);

提前多多感谢..

1 个答案:

答案 0 :(得分:0)

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());
    }