无法使用超级功能的SDK打开存储中的文件

时间:2018-07-06 09:37:19

标签: android sdk superpowered

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

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

我还添加了权限READ_EXTERNAL_STORAGE。

问题是,没有显示错误,但不能播放文件。 如果我从资产中获取了相同的文件,则说明文件已成功播放。 请有人告诉我我对内部存储的播放有哪些更改?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

现在可以使用。我正在尝试使用Superpowered sdk更改音高和tampo。我正在尝试从手机的存储设备中获取mp3文件。我正在使用superpowered-ndk-example来完成工作。下面是示例代码。

在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);

它可以改变音高和弹力。