nativescript-audio icon indicating copy to clipboard operation
nativescript-audio copied to clipboard

Audio won't play with Silent Switch on

Open coderReview opened this issue 6 years ago • 7 comments

Audio won't play with Silent Switch on.

  • iPhone 5s, iOS 12.1.

Error in log:

AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Alto-falante (type: Speaker)

coderReview avatar Nov 24 '18 05:11 coderReview

@coderReview is it resolved ?

JijeshP avatar Dec 11 '18 20:12 JijeshP

@JijeshP not that I know of.

coderReview avatar Dec 11 '18 21:12 coderReview

After beating my head against the same problem I see the issue. The project uses AVAudioSessionCategoryPlayAndRecord which is controlled by the silent switch to either play or not play. What you guys (and I) want is AVAudioSessionCategoryPlayback which plays whether or not the mute switch is on.

To fix this the project could expose a "play even if muted option" but till that happens since the .play() function doesn't set the category and it's only set on the initWithFile / initWithUrl you can init the player, change the Category then play it and it'll work.

If you need to access the native apis you might need to set that up if you haven't previously, see this https://www.npmjs.com/package/tns-platform-declarations

    this.player
      .initFromUrl({
        audioFile: 'http://www.noiseaddicts.com/samples_1w72b820/4938.mp3', //this.sound.file,
        loop: false,
        completeCallback: () => {
          this.isPlaying = false
          this.digest()
        }
      })
      .then(() => {
        if (!this.player.isAudioPlaying()) {
          const audioSession = AVAudioSession.sharedInstance();
          audioSession.setCategoryError(AVAudioSessionCategoryPlayback);

          this.isPlaying = true
          this.player.play()
        }
      })

DanLatimer avatar Dec 18 '18 15:12 DanLatimer

Thanks for that. I would like to see a setting for play with silent on, too.

bradrice avatar May 09 '19 16:05 bradrice

@DanLatimer it seems not working, i don't know why, i'm using javascript but the error is the same AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Altoparlante (type: Speaker)

ghost avatar May 09 '19 17:05 ghost

To give some direction for people having issues in Nativescript 6 with background playback (i.e. when the device is locked etc), @DanLatimer solution does seem to fix this. Just make sure to add a conditional check that it is iOS, since this is platform-specific.

I've been previously setting the playback category in main.js (NS Vue, JS) without any issues, until the NS 6 update. I ended up adjusting to the given solution above, after some time troubleshooting, and got it to work again. Thanks! :)

apsaros avatar Aug 04 '19 19:08 apsaros

After beating my head against the same problem I see the issue. The project uses AVAudioSessionCategoryPlayAndRecord which is controlled by the silent switch to either play or not play. What you guys (and I) want is AVAudioSessionCategoryPlayback which plays whether or not the mute switch is on.

To fix this the project could expose a "play even if muted option" but till that happens since the .play() function doesn't set the category and it's only set on the initWithFile / initWithUrl you can init the player, change the Category then play it and it'll work.

If you need to access the native apis you might need to set that up if you haven't previously, see this https://www.npmjs.com/package/tns-platform-declarations

    this.player
      .initFromUrl({
        audioFile: 'http://www.noiseaddicts.com/samples_1w72b820/4938.mp3', //this.sound.file,
        loop: false,
        completeCallback: () => {
          this.isPlaying = false
          this.digest()
        }
      })
      .then(() => {
        if (!this.player.isAudioPlaying()) {
          const audioSession = AVAudioSession.sharedInstance();
          audioSession.setCategoryError(AVAudioSessionCategoryPlayback);

          this.isPlaying = true
          this.player.play()
        }
      })

This fixed the issue for me, thanks :)

StefanNedelchev avatar Dec 23 '20 13:12 StefanNedelchev