Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(iOS): crash with space is in source url #748

Closed
pishguy opened this issue Jan 31, 2021 · 10 comments · Fixed by #1625
Closed

bug(iOS): crash with space is in source url #748

pishguy opened this issue Jan 31, 2021 · 10 comments · Fixed by #1625
Labels
bug platform-ios Affects the ios platform

Comments

@pishguy
Copy link

pishguy commented Jan 31, 2021

when i try to play audio i get this error and that cause crashing application:

Fatal error: Unexpectedly found nil while unwrapping an Optional value: file audioplayers/SwiftAudioplayersPlugin.swift, line 518
@pishguy pishguy added the bug label Jan 31, 2021
@LiJinJinJIn
Copy link

+1

@luanpotter
Copy link
Member

that file goes up to line 518, so I don't think that error is possible. probably using an outdated version? can you please try on the latest version?

@mcradha
Copy link

mcradha commented Mar 22, 2021

I faced this same error 'Unexpectedly found nil while unwrapping an Optional value' on iOS. The application crashes giving this same error when I start the app and it tries to resume the background music if the sound setting is set to 'on'.

The app does not crash in Android but in Android also I do get an error and this is the stack trace:

Error log

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: PlatformException(Unexpected error!, uriString, java.lang.NullPointerException: uriString
E/flutter ( 6132): at android.net.Uri$StringUri.(Uri.java:496)
E/flutter ( 6132): at android.net.Uri$StringUri.(Uri.java:486)
E/flutter ( 6132): at android.net.Uri.parse(Uri.java:458)
E/flutter ( 6132): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1169)
E/flutter ( 6132): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1162)
E/flutter ( 6132): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1127)
E/flutter ( 6132): at xyz.luan.audioplayers.WrappedMediaPlayer.setSource(WrappedMediaPlayer.java:422)
E/flutter ( 6132): at xyz.luan.audioplayers.WrappedMediaPlayer.actuallyPlay(WrappedMediaPlayer.java:265)
E/flutter ( 6132): at xyz.luan.audioplayers.WrappedMediaPlayer.play(WrappedMediaPlayer.java:252)
E/flutter ( 6132): at xyz.luan.audioplayers.AudioplayersPlugin.handleMethodCall(AudioplayersPlugin.java:111)
E/flutter ( 6132): at xyz.luan.audioplayers.AudioplayersPlugin.onMethodCall(AudioplayersPlugin.java:60)
E/flutter ( 6132): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/flutter ( 6132): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/flutter ( 6132): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
E/flutter ( 6132): at android.os.MessageQueue.nativePollOnce(Native Method)
E/flutter ( 6132): at android.os.MessageQueue.next(MessageQueue.java:335)
E/flutter ( 6132): at android.os.Looper.loop(Looper.java:183)
E/flutter ( 6132): at android.app.ActivityThread.main(ActivityThread.java:7656)
E/flutter ( 6132): at java.lang.reflect.Method.invoke(Native Method)
E/flutter ( 6132): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/flutter ( 6132): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/flutter ( 6132): , null)
E/flutter ( 6132): #0 StandardMethodCodec.decodeEnvelope
package:flutter/…/services/message_codecs.dart:582
E/flutter ( 6132): #1 MethodChannel._invokeMethod
package:flutter/…/services/platform_channel.dart:159
E/flutter ( 6132):
E/flutter ( 6132): #2 MethodChannel.invokeMethod
package:flutter/…/services/platform_channel.dart:332
E/flutter ( 6132): #3 AudioPlayer._invokeMethod
package:audioplayers/audioplayers.dart:337
E/flutter ( 6132): #4 AudioPlayer.resume
package:audioplayers/audioplayers.dart:491
E/flutter ( 6132): #5 Score.resumeBGMusic
package:JW/providers/score.dart:238
E/flutter ( 6132): #6 Score.updateSoundChoice
package:JW/providers/score.dart:65
E/flutter ( 6132): #7 _HomeScreenState.initParams
package:JW/screens/homeScreen.dart:88
E/flutter ( 6132): #8 _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter ( 6132): #9 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 6132): #10 _FutureListener.handleValue (dart:async/future_impl.dart:143:18)
E/flutter ( 6132): #11 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:696:45)
E/flutter ( 6132): #12 Future._propagateToListeners (dart:async/future_impl.dart:725:32)
E/flutter ( 6132): #13 Future._completeWithValue (dart:async/future_impl.dart:529:5)
E/flutter ( 6132): #14 Future._asyncCompleteWithValue. (dart:async/future_impl.dart:567:7)
E/flutter ( 6132): #15 _rootRun (dart:async/zone.dart:1190:13)
E/flutter ( 6132): #16 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 6132): #17 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 6132): #18 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1037:23)
E/flutter ( 6132): #19 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 6132): #20 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)

Here are the details of my Dart code:

class Score with ChangeNotifier {
  bool _soundsOn = true;
  
  final AudioPlayer advancedPlayer =
      new AudioPlayer(playerId: 'bgm_player', mode: PlayerMode.MEDIA_PLAYER)
        ..setReleaseMode(ReleaseMode.LOOP);

This function is called after reading the settings from local storage (if sounds are set to 'on' or 'off')

void updateSoundChoice(bool value) {
    _soundsOn = value;

    if (_soundsOn == false)
      pauseBGMusic();
    else
      resumeBGMusic();
}

Future<void> pauseBGMusic() async {
    int result = await advancedPlayer.pause();
    if (result != 1) print('Error pausing music');
  }

  Future<void> resumeBGMusic() async {
    advancedPlayer.setReleaseMode(ReleaseMode.LOOP);
    int result = await advancedPlayer.resume();
    if (result != 1) print('Error resuming music');
  }

I also have a function to load the background music of the app first time the app starts which is as below:

Future<void> loadBackgroundMusic() async {
    final String bgmusic =
        'https://xxxxxxxxx.com/sounds/background+music.mp3';
    if (_soundsOn == true) {
      await advancedPlayer.play(bgmusic, isLocal: false, volume: 0.3);
      advancedPlayer.onPlayerStateChanged.listen((AudioPlayerState state) {
        if (state == AudioPlayerState.COMPLETED) {
          advancedPlayer.play(bgmusic, isLocal: false, volume: 0.2);
        }
      });
    }

these errors are coming with audioplayers 0.17.1 in pubspec.yaml. I tried upgrading to audioplayers 0.18.0 but that requires minsdkversion to be 23 and my current minsdkversion is 19. changing that has impacts on other supported plugins in the app so its not my preferred choice.

Any suggestions on what I might be doing wrong?

@zsmiklos
Copy link

I got similar bug on iOS:
Fatal error: Unexpectedly found nil while unwrapping an Optional value: file audioplayers/WrappedMediaPlayer.swift, line 217
I can not play any file from internet on iOS. Could you fix it?

@usamaaelgendy
Copy link

same error

@umuieme
Copy link

umuieme commented Jun 10, 2021

Same error. For me, the error is because of space in the url of file. try to encode the url before sending it
Uri.encodeFull(url)

Maybe we can handle it in ios side in the library itself?

@pishguy
Copy link
Author

pishguy commented Jun 10, 2021

+1

@dariocavada
Copy link

Uri.encodeFull

Solved using:

final result = await _audioPlayer.play(Uri.encodeFull(url), position: playPosition);

thank you to @umuieme

@alfianhpratama
Copy link

+1

@Gustl22 Gustl22 added the platform-ios Affects the ios platform label Sep 27, 2022
@Gustl22 Gustl22 changed the title Unexpectedly found nil while unwrapping an Optional value bug(iOS): crash with space is in source url Sep 27, 2022
@LeoSandbox
Copy link

I am experiencing the same issue. Is there a workaround besides renaming all filenames to remove spaces?

Gustl22 added a commit that referenced this issue Sep 29, 2023
# Description

- feat: Improved error description for unsupported file formats
- fix: Handle white space and special characters in URL and Assets (Web
& Darwin)
- test: Test files without file extension (not playable Darwin)

## Related Issues

Closes #1494
Closes #748
Closes #972
Closes #1546

---------

Co-authored-by: Lukas Klingsbo <lukas.klingsbo@gmail.com>
Gustl22 added a commit that referenced this issue Sep 29, 2023
# Description

- feat: Improved error description for unsupported file formats
- fix: Handle white space and special characters in URL and Assets (Web
& Darwin)
- test: Test files without file extension (not playable Darwin)

## Related Issues

Closes #1494
Closes #748
Closes #972
Closes #1546

---------

Co-authored-by: Lukas Klingsbo <lukas.klingsbo@gmail.com>
(cherry picked from commit a4d8442)
Gustl22 added a commit that referenced this issue Nov 14, 2023
# Description

- feat: Improved error description for unsupported file formats
- fix: Handle white space and special characters in URL and Assets (Web
& Darwin)
- test: Test files without file extension (not playable Darwin)

## Related Issues

Closes #1494
Closes #748
Closes #972
Closes #1546

---------

Co-authored-by: Lukas Klingsbo <lukas.klingsbo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug platform-ios Affects the ios platform
Projects
None yet
Development

Successfully merging a pull request may close this issue.