Unable to run flutter after audio players package import

Viewed 1524

I imported following sound package from flutter packages.

audioplayers: ^0.12.1

It threw errors saying I needed to install some things. Based on the error messages, I ran following commands.

brew install cocoa

pods pod setup

The setup was completed successfully.

It started throwing errors when I tried to run my application after this. The app is nothing but a stateless widget with an empty MaterialApp() widget. This was working fine before I did the above installs.

I tried removing the audioplayers dependencies but the error persists. Trying to rebuild and restart my IDE doesn't help. Please advice whats wrong. I don't get why the error persists even after removing the dependency. I am using a Mac if that matters. Please advice. Thanks.

Error Log:

Launching lib/main.dart on iPhone X in debug mode...
Running pod install...
Running Xcode build...
Xcode build done.                                            1.9s
Failed to build iOS app
Error output from Xcode build:
↳
    ** BUILD FAILED **


Xcode's output:
↳
    === BUILD TARGET path_provider OF PROJECT Pods WITH CONFIGURATION Debug ===
    /Users/myusername/.pub-cache/hosted/pub.dartlang.org/audioplayers-0.12.1/ios/Classes/AudioplayersPlugin.m:232:5: error: use of undeclared identifier 'AVAudioSessionCategory'; did you mean 'AVAudioSessionCategoryRecord'?
        AVAudioSessionCategory category;
        ^~~~~~~~~~~~~~~~~~~~~~
        AVAudioSessionCategoryRecord
    In module 'AVFoundation' imported from /Users/myusername/.pub-cache/hosted/pub.dartlang.org/audioplayers-0.12.1/ios/Classes/AudioplayersPlugin.m:4:
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator11.2.sdk/System/Library/Frameworks/AVFoundation.framework/Frameworks/AVFAudio.framework/Headers/AVAudioSession.h:642:28: note: 'AVAudioSessionCategoryRecord' declared here
    AVF_EXPORT NSString *const AVAudioSessionCategoryRecord;
                               ^
    /Users/myusername/.pub-cache/hosted/pub.dartlang.org/audioplayers-0.12.1/ios/Classes/AudioplayersPlugin.m:232:27: error: expected ';' after expression
        AVAudioSessionCategory category;
                              ^
                              ;
    /Users/myusername/.pub-cache/hosted/pub.dartlang.org/audioplayers-0.12.1/ios/Classes/AudioplayersPlugin.m:232:28: error: use of undeclared identifier 'category'
        AVAudioSessionCategory category;
                               ^
    /Users/myusername/.pub-cache/hosted/pub.dartlang.org/audioplayers-0.12.1/ios/Classes/AudioplayersPlugin.m:234:9: error: use of undeclared identifier 'category'
            category = AVAudioSessionCategoryAmbient;
            ^
    /Users/myusername/.pub-cache/hosted/pub.dartlang.org/audioplayers-0.12.1/ios/Classes/AudioplayersPlugin.m:236:9: error: use of undeclared identifier 'category'
            category = AVAudioSessionCategoryPlayback;
            ^
    /Users/myusername/.pub-cache/hosted/pub.dartlang.org/audioplayers-0.12.1/ios/Classes/AudioplayersPlugin.m:239:34: error: use of undeclared identifier 'category'
                        setCategory: category
                                     ^
    /Users/myusername/.pub-cache/hosted/pub.dartlang.org/audioplayers-0.12.1/ios/Classes/AudioplayersPlugin.m:232:5: warning: expression result unused [-Wunused-value]
        AVAudioSessionCategory category;
        ^~~~~~~~~~~~~~~~~~~~~~
    1 warning and 6 errors generated.

Could not build the application for the simulator.
Error launching application on iPhone X.
1 Answers

I have a solution, though I admit it isn't the best solution.

My guess is that the error has something with Xcode trying to build this as a Universal app instead of an iOS app, though I don't know enough about Xcode to verify this (I'm mainly an Android developer playing with Flutter). The AVAudioSessionCategory doesn't work with MacOS...only iOS.

After building, I have the same error as above. In AudioplayersPlugin.m, Line 232, you'll see this:

AVAudioSessionCategory category;
if (respectSilence) {
    category = AVAudioSessionCategoryAmbient;
} else {
    category = AVAudioSessionCategoryPlayback;
}
BOOL success = [[AVAudioSession sharedInstance]
                setCategory: category
                error:&error];

Replace with:

BOOL success;
if (respectSilence) {
    success = [[AVAudioSession sharedInstance]
               setCategory: AVAudioSessionCategoryAmbient
               error:&error];
} else {
    success = [[AVAudioSession sharedInstance]
               setCategory: AVAudioSessionCategoryPlayback
               error:&error];
}

Your app should run on iOS now.

Related