对 SD卡中的文件使用资产文件描述符



我正在尝试在超级应用程序中实现用户偏好选择音乐。我无法通过 AssetFileDescriptor 提取更多 SD 卡中的音乐。请指导,以便我可以访问用户音乐数据。我无法使用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);

提前非常感谢..

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

相关内容

  • 没有找到相关文章

最新更新