无法使用超级 SDK 从存储中打开文件

Can not open file from storage with superpowered sdk

我正在使用功能强大的 sdk 播放音频文件。 为此,我使用

player->open("/storage/emulated/0/0567894638.mp3");

我也加权限READ_EXTERNAL_STORAGE.

问题是,显示没有错误,但无法播放文件。 如果我从资产中获取相同的文件,那么它就会成功播放。 请有人告诉我我为从内部存储播放做了什么更改?

有效 now.I 我正在尝试使用 Superpowered 来改变音高和节奏 sdk.I 正在尝试从 phone.I 的存储设备中获取 mp3 文件 我正在使用 superpowered-ndk-example做我的 job.Below 是示例代码。

在onCreate方法中

 this.binding.setPlaying(playing);
    // Get the device's sample rate and buffer size to enable low-latency Android audio output, if available.
    Pair<Integer, Integer> deviceAudioInfo = getDeviceAudioInfo();
    Integer sampleRate = deviceAudioInfo.first;
    Integer bufferSize = deviceAudioInfo.second;
    Pair<Integer, Integer> trackLocationInfo = getTrackLocationInfo();
    Integer fileOffset = trackLocationInfo.first;
    Integer fileLength = trackLocationInfo.second;
    String pat = "/storage/emulated/0/rec.wav";
    // Arguments: path to the APK file, offset and length of the two resource files, sample rate, audio buffer size. 
    // this code works with assets         
    SuperpoweredExample(sampleRate, bufferSize, getPackageResourcePath(), fileOffset, fileLength);
    //this code works for storage devices
    SuperpoweredExample(sampleRate, bufferSize, pat, fileOffset, fileLength);
     setupViews();

所需方法

 private void setupViews() {
            binding.btnPlayPause.setOnClickListener((view) -> {
                setPlaying(!playing);
            });    
            binding.sbPitch.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    onPitchChanged(progress - 12);
                }    
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }    
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {    
                }
            });    
            binding.sbTrackProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    seekTo(progress);
                    if (progress == seekBar.getMax()) {
                        setPlaying(!playing);
                    }
                }    
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }    
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    seekTo(seekBar.getProgress() / 100.0f);
                }
            });    
            binding.radioGroup.check(binding.rbNormalTime.getId());
            binding.radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
                switch (checkedId) {
                    case R.id.rb_slow_down:
                        onTempoSelected(TEMPO_HALF);
                        break;
                    case R.id.rb_normal_time:
                        onTempoSelected(TEMPO_NORMAL);
                        break;
                    case R.id.rb_speed_up:
                        onTempoSelected(TEMPO_DOUBLE);
                        break;
                    default:
                        onTempoSelected(TEMPO_NORMAL);
                        break;
                }
            });
        }    
        private void setPlaying(boolean playing) {
            this.playing = playing;
            this.binding.setPlaying(this.playing);
            onPlayPause(this.playing);
        }    
        private Pair<Integer, Integer> getDeviceAudioInfo() {
            String samplerateString;
            String buffersizeString;

            AudioManager audioManager = (AudioManager) 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";

            return new Pair<>(Integer.parseInt(samplerateString), Integer.parseInt(buffersizeString));
        }    
        private Pair<Integer, Integer> getTrackLocationInfo() {
            AssetFileDescriptor fd;
            Pair<Integer, Integer> trackLocationInfo = null;

            try {
                fd = getTrackAsset();
                int fileOffset = (int) fd.getStartOffset();
                int fileLength = (int) fd.getLength();
                trackLocationInfo = new Pair<>(fileOffset, fileLength);
            } catch (IOException e) {
                e.printStackTrace();
            }    
            return trackLocationInfo;
        }    
        private AssetFileDescriptor getTrackAsset() throws IOException {
            return getAssets().openFd("kal.mp3");
        }    
        private native void SuperpoweredExample(int samplerate, int buffersize, String apkPath,
                                                int fileOffset, int fileLength);

        private native void onPlayPause(boolean play);

        private native void seekTo(double positionPercent);

        private native void onPitchChanged(int pitchValue);

        private native void onTempoSelected(int tempoValue);

        public native String stringFromJNI();

本机方法在 SuperpoweredExample.cpp 文件中使用。与 this

相比,只有一件事发生了变化

player->open(path, fileOffset, fileLength); 更改为 player->open(path);

它适用于改变音高和节奏。

String file_path = "/storage/emulated/0/kal.mp3";
File f = new File("" + file_path);
fileLength = (int) f.length();

Pair<Integer, Integer> deviceAudioInfo = getDeviceAudioInfo();
Integer sampleRate = deviceAudioInfo.first;
Integer bufferSize = deviceAudioInfo.second;
SuperpoweredExample(sampleRate, bufferSize, file_path, 0, fileLength);


SuperpoweredExample::SuperpoweredExample(unsigned int samplerate, unsigned int buffersize,
                                     const char *path, int fileOffset, int fileLength) : volume(1.0f * headroom) {
stereoBuffer = (float *) memalign(16, (buffersize + 16) * sizeof(float) * 2);

player = new SuperpoweredAdvancedAudioPlayer(&player, NULL, samplerate, 0);
player->open(path);

audioSystem = new SuperpoweredAndroidAudioIO(samplerate, buffersize, false, true,
                                             audioProcessing,
                                             this, -1, SL_ANDROID_STREAM_MEDIA, buffersize * 2);
}

添加权限READ_EXTERNAL_STORAGE