Search code examples
iosaudiodartflutteravaudioplayer

Flutter app's audio not working through speaker


I am having trouble getting my flutter app to play audio through iOS speakers. I have tried on multiple iOS devices. The audio plays through android speakers, android headphones, and iOS headphones. I get no error messages about it, and the audio seems to be playing based on the log messages showing the position of the audio file changing.

I am using the package audioplayer (https://pub.dartlang.org/packages/audioplayer)

I have even tried running the example (https://pub.dartlang.org/packages/audioplayer#-example-tab-), and I get the same issue.

I have also tried using the package audioplayers (https://pub.dartlang.org/packages/audioplayers). Same thing. If anyone can point me towards any explanation... I suppose I need to default to the speakers somehow, but I can't find any documentation on how to do that in flutter, and I haven't yet learned how to override certain aspects of a flutter app with native swift/objective c code. If anyone has any suggestions, I'd love to hear them.

Attached are logs with and without the headphones plugged in to my iPhone.

With headphones:

flutter: attempting to play /var/mobile/Containers/Data/Application/1A17D73F-0130-4A6B-A1F4-B44ECAB4EF3E/Documents/English_mit_filler_David.mp3
iOS => call play
iOS => playerId dd4d9d29-2188-4795-abfb-6d785c6c19a7
play!
isLocal: 1 1
volume: 1.000000 1
togglePlay /var/mobile/Containers/Data/Application/1A17D73F-0130-4A6B-A1F4-B44ECAB4EF3E/Documents/English_mit_filler_David.mp3
ios -> updateDuration...nan
ios -> onTimeInterval...
flutter: _AudioAppState.setPositionHandler => p 0:00:00.000000
ios -> onTimeInterval...
player status: 1
ios -> updateDuration...19.435102
ios -> invokechannel
flutter: _AudioAppState.setPositionHandler => p 0:00:00.000000
ios -> onTimeInterval...
ios -> onTimeInterval...
flutter: _AudioAppState.setDurationHandler => d 0:00:19.435000
flutter: _AudioAppState.setPositionHandler => p 0:00:00.000000
flutter: _AudioAppState.setPositionHandler => p 0:00:00.000000
ios -> onTimeInterval...
flutter: _AudioAppState.setPositionHandler => p 0:00:00.200000
ios -> onTimeInterval...
flutter: _AudioAppState.setPositionHandler => p 0:00:00.401000
ios -> onTimeInterval...
flutter: _AudioAppState.setPositionHandler => p 0:00:00.600000
ios -> onTimeInterval...

Without headphones:

flutter: attempting to play /var/mobile/Containers/Data/Application/1A17D73F-0130-4A6B-A1F4-B44ECAB4EF3E/Documents/English_mit_filler_Dropbox.mp3
iOS => call play
iOS => playerId 60dc6e8c-4619-4abb-812d-33dcb03f23a8
play!
isLocal: 1 1
volume: 1.000000 1
togglePlay /var/mobile/Containers/Data/Application/1A17D73F-0130-4A6B-A1F4-B44ECAB4EF3E/Documents/English_mit_filler_Dropbox.mp3
ios -> updateDuration...nan
ios -> onTimeInterval...
ios -> onTimeInterval...
ios -> onTimeInterval...
flutter: _AudioAppState.setPositionHandler => p 0:00:00.000000
player status: 1
ios -> updateDuration...28.682449
ios -> invokechannel
flutter: _AudioAppState.setPositionHandler => p 0:00:00.000000
flutter: _AudioAppState.setPositionHandler => p 0:00:00.000000
flutter: _AudioAppState.setDurationHandler => d 0:00:28.682000
ios -> onTimeInterval...
ios -> onTimeInterval...
flutter: _AudioAppState.setPositionHandler => p 0:00:00.000000
flutter: _AudioAppState.setPositionHandler => p 0:00:00.000000
ios -> onTimeInterval...
flutter: _AudioAppState.setPositionHandler => p 0:00:00.201000
ios -> onTimeInterval...
flutter: _AudioAppState.setPositionHandler => p 0:00:00.401000
ios -> onTimeInterval...
flutter: _AudioAppState.setPositionHandler => p 0:00:00.601000
ios -> onTimeInterval...

Looks like the same logs to me, with no errors on either end. But it's like the sound is muted- and I've made sure that do not disturb mode is not on, silent mode is not on, I've checked button on the mute button on the side of the phone is not on... I have tried it on the emulator through Xcode and then the audio did play... But I can't figure out how to get it to work on my physical devices. Thanks for any advice!


Solution

  • I managed to solve it, with the help of this link AVAudioSession setCategory error

    I added the following code to the AudioplayerPlugin.m file that is created in ios/Pods/.symlinks/plugins/audioplayer/ios/Classes

    -(void) togglePlay: (NSString*) url isLocal: (int) isLocal
    {
    //ADDED START
            NSError *error = nil;
            BOOL success = [[AVAudioSession sharedInstance]
                            setCategory:AVAudioSessionCategoryPlayback
                            error:&error];
            if (!success) {
                NSLog(@"Error setting speaker");
                // Handle error here, as appropriate
            }
    //ADDED END
          NSLog(@"togglePlay %@",url );
          if (![lastUrl isEqualToString:url]) {
            [playerItem removeObserver:self
                            forKeyPath:@"player.currentItem.status"];