1

我正在使用超级强大的 sdk 来播放音频文件。为此,我使用

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

我还添加了 READ_EXTERNAL_STORAGE 权限。

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

4

2 回答 2

3
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

于 2018-07-11T05:02:03.560 回答
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 文件中。在这个只有一件事比这个改变

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

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

于 2018-07-06T11:24:48.233 回答