nstudio / nativescript-audio

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

Audio won't play with Silent Switch on #132

Open coderReview opened 6 years ago

coderReview commented 6 years ago

Audio won't play with Silent Switch on.

Error in log:

AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Alto-falante (type: Speaker)
JijeshP commented 5 years ago

@coderReview is it resolved ?

coderReview commented 5 years ago

@JijeshP not that I know of.

DanLatimer commented 5 years ago

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()
        }
      })
bradrice commented 5 years ago

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

ghost commented 5 years ago

@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)

apsaros commented 5 years ago

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! :)

StefanNedelchev commented 3 years ago

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 :)