nstudio / nativescript-audio

:microphone: NativeScript plugin to record and play audio :musical_note:
Other
150 stars 104 forks source link

android.media.AudioFocusRequest.Builder crash #195

Open chris-praxis opened 2 years ago

chris-praxis commented 2 years ago

Crash happens on Android 7 where this API is not available. Code appears to be missing a if (android.os.Build.VERSION.SDK_INT >= 26) check.

djmaze commented 2 years ago

AFAICS, that check just needs to to be removed. The rest of the code seems to support Android < 8 correctly.

chris-praxis commented 2 years ago

I'm saying a version check needs to be added, and you're saying some check needs to be removed? I don't follow. Here's my change, simply wrapping some audio focus code in a version check...

node_modules/nativescript-audio/android/player.js 38,46c38,48 < const playbackAttributes = new android.media.AudioAttributes.Builder() < .setUsage(options.usage) < .setContentType(options.contentType) < .build(); < this._audioFocusRequest = new android.media.AudioFocusRequest.Builder(options.durationHint) < .setAudioAttributes(playbackAttributes) < .setAcceptsDelayedFocusGain(true) < .setOnAudioFocusChangeListener(this._mOnAudioFocusChangeListener) < .build();

    if (android.os.Build.VERSION.SDK_INT >= 26) {
        const playbackAttributes = new android.media.AudioAttributes.Builder()
            .setUsage(options.usage)
            .setContentType(options.contentType)
            .build();
        this._audioFocusRequest = new android.media.AudioFocusRequest.Builder(options.durationHint)
            .setAudioAttributes(playbackAttributes)
            .setAcceptsDelayedFocusGain(true)
            .setOnAudioFocusChangeListener(this._mOnAudioFocusChangeListener)
            .build();
    }
djmaze commented 2 years ago

Ohh.. looking at it again, I was wrong I guess. But a few lines earlier, there is the following: https://github.com/nstudio/nativescript-audio/blob/master/src/android/player.ts#L40-L42

So the additional check should not be necessary.