Flutter 应用程序的音频无法通过扬声器工作

Flutter app's audio not working through speaker

我无法让我的 flutter 应用程序通过 iOS 扬声器播放音频。我试过多个 iOS 设备。音频通过 android 扬声器、android 头phone 和 iOS 头phone 播放。我没有收到有关它的错误消息,并且根据显示音频文件位置更改的日志消息,音频似乎正在播放。

我正在使用音频播放器包 (https://pub.dartlang.org/packages/audioplayer)

我什至试过 运行 这个例子 (https://pub.dartlang.org/packages/audioplayer#-example-tab-),但我遇到了同样的问题。

我也尝试过使用音频播放器包 (https://pub.dartlang.org/packages/audioplayers)。一样。如果有人能指出我的任何解释......我想我需要以某种方式默认为扬声器,但我找不到任何关于如何在颤动中做到这一点的文档,而且我还没有学会如何覆盖某些方面带有原生 swift/objective c 代码的 flutter 应用程序。如果有人有任何建议,我很乐意听取他们的意见。

附件是有和没有头phone插入我的iPhone的日志。

有头phones:

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...

无头phones:

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...

在我看来是相同的日志,两端都没有错误。但这就像声音被静音了-我已经确定请勿打扰模式没有打开,静音模式没有打开,我已经检查了 phone 旁边的静音按钮上的按钮没有在...我已经通过 Xcode 在模拟器上试过了,然后音频确实播放了...但我不知道如何让它在我的物理设备上工作。 感谢您的任何建议!

在这个linkAVAudioSession setCategory error

的帮助下,我设法解决了它

我将以下代码添加到在 ios/Pods/.symlinks/plugins/audioplayer/ios/Classes

中创建的 AudioplayerPlugin.m 文件中
-(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"];

只需将 <uses-permission android:name="android.permission.INTERNET"/> 添加到 AndroidManifest.xml 文件,在 </application> 结束标记之后。