My app lost connection when I try play a sound using audioplayers - flutter

I am using Flutter and whenever I want to to play a sound it crashes and throw me this:
iOS => call startHeadlessService, playerId d580eb03-1cab-4a44-9b7d-5b80ac32a53c
iOS => call play, playerId d580eb03-1cab-4a44-9b7d-5b80ac32a53c
play!
Lost connection to device.
I don't know why, I followed step by step.
Here's the code I write if it's helping:
import 'package:flutter/material.dart';
import 'package:audioplayers/audio_cache.dart';
void main() => runApp(XylophoneApp());
class XylophoneApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Center(
child: FlatButton(
onPressed: () {
final player = AudioCache();
player.play('note1.wav');
},
child: Text('Play me!'),
),
),
),
),
);
}
}
And Here is my pubspec.yaml :
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
audioplayers: ^0.16.0
I've done everthing that was in that course step by step, but instead of asking me to allow microphones ( that was in course ), the app just lost connection.
I don't know why, in the course never explain what to do if you meet errors.
Can you help me understand why is this happening and how can be handled? It would help me a lot. Thank you in advance!
The emulator is iPhone 11 pro max.
Also I tried running it by calling " flutter run " in the terminal

With the help of J. Saw from the comments above, we changed the package version from 0.16 to 0.10 and it's working

Related

The method 'play' isn't defined for the type 'AudioCache'

issue The method 'play' isn't defined for the type 'AudioCache'.
import 'package:flutter/material.dart';
import 'package:audioplayers/src/audio_cache.dart';
void main() {
runApp(XylophoneApp());
}
class XylophoneApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Center(
child: TextButton(
onPressed: () {
final player = AudioCache();
player.play('note1.wave');
},
child: Text('click me'),
),
),
),
),
);
}
}
The code below is no longer valid from audioplayers v1.0.1
final player = AudioCache();
player.play('note1.wave');
Instead U can do this
final player = AudioPlayer();
//
player.play(UrlSource('note1.wave'));
// If file located in assets folder like assets/sounds/note01.wave"
await player.play(AssetSource('sounds/note1.wave'));
consider look in migration guide from audioplayers
AudioCache is dead because of confusion in name. Now, if you want to play an audio file from assets you can use this.
// add this in imports
import 'package:audioplayers/audioplayers.dart';
// play audio
final player = AudioPlayer();
player.play(AssetSource('note1.wav'));
Use this instead of AssetSource if you want don't want to play from assets.
UrlSource: get the audio from a remote URL from the Internet
DeviceFileSource: access a file in the user's device, probably selected by a file picker
AssetSource: play an asset bundled with your app, normally within the assets directory
BytesSource (only some platforms): pass in the bytes of your audio directly (read it from anywhere).
You can see more from audioplayers documentation
There seems to be the issue with your import. Do import this👇
import 'package:audioplayers/audioplayers.dart';
If the issue still exists, then use an older version of it.
I think version 0.19.0 should work for you.
#Raj if You are doing LinkedIn course by London App Brewery and Angela Yu then an exact version that would work perfectly would be 0.10.0
audioplayers: 0.10.0
It's the one used by Angela and it worked perfectly for me :-)
Wouldn't try it though if not for #Zain Basharat Ali advice.
Thanks for Your tip! :-)

How to fix animation glitches in Flutter Lottie

I have an app with lots of Lottie animations. They work fine in a Windows UWP app and AirBnB sample Android app. Also look fine in lottiefiles.com online test tool.
But they are glitchy in Flutter using Lottie for Flutter package. Not all of them, but many, maybe around a third.
Full source code below, you can try for yourself.
Glitches are either frame overlaps or blinking, as if there is a gap between frames.
Glitches are consistent, i.e. always happen in the same place, so does not seem like a performance issue.
Also, they are not happening between repeats, but in the middle of the animation, so again not an application issue but seems to be the issue with how they are rendered.
I have tried loading them as composition from memory with a controller. I have also tried a vanilla asset load to rule out issues with my implementation, and they are behaving the same. The issue appears both in the emulator and the actual phone. So it seems to me it is not caused by the way I implemented but by the Lottie package itself, or perhaps an issue with Lottie JSON that for some reason only affects Android.
Full example is below. Just paste this into main.dart and add dependency to yaml: lottie: ^1.2.1.
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(),
darkTheme: ThemeData.dark(),
themeMode: ThemeMode.dark,
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Lottie Test"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Lottie.network(
'https://assets6.lottiefiles.com/packages/lf20_uz92k22x.json'),
),
],
),
),
);
}
}
Try adding frameRate: FrameRate.max like stated below.
Expanded(
child: Lottie.network('https://assets6.lottiefiles.com/packages/lf20_uz92k22x.json',
frameRate: FrameRate.max,
),
),
this will render the Lottie animation with the same frame rate of that of the app. In Lottie plugin it will render the json as per the frame rate mentioned in the json file. An animation encoded with a 25 FPS will only be painted 25 times per seconds even though the AnimationController will invalidate the widget 60 times per seconds. Hence this framerate was introduced from Lottie 0.6.0
I use Lottie for Flutter on my applications and it works well also on emulator or on low-performance phones. The problem can be related to bad converted animations, or you can try to use Lottie.asset(yourjson) to avoid potential network fetch issues.
According to the official package documentation
For Flutter Web, run the app with: flutter run --web-renderer canvaskit
Works fine for me in the latest version: lottie: ^1.2.1

flutter ,My app dies whenever i enter the videoPlayerScreen

it's all good on android but not good on iOS.
whenever i try to enter the videoPlayerScreen
error occurs
I tryed another project with same class code ,
it was fine.
but in this project doens't work.
Here's my code
...
class _VideoTestState extends State<VideoTest> {
final FijkPlayer player = FijkPlayer();
#override
void initState() {
super.initState();
player.setDataSource( // ✅ App dies in here
'http://baishan.oversketch.com/2019/11/05/d07f2f1440e51b9680f4bcfe63b0ab67.MP4',
autoPlay: true);
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.green,
child: Center(
child: FijkView(
player: player,
),
),
),
);
}
}
in Android studio when I enter it , it dies immediately, Log( Lost connection to device. )
but in Xcode show log like this.
I assume its package problem.
not sure about it but only difference between another project and this one is
another project has few package ( actually minimum packages to play video )
this one has alot include firebase things.
I tried ,
flutter clean ,
rm Podfile.lock
pod install.
is there any clue to solve this problem??

Flutter Audio Cache Error with player.play

Hi this code works wonderfully with Angela in my Flutter course haha but for some reason the player.play('notes1.wav') isnt working for me.. tips? I'm getting this error..
error: The method 'play' isn't defined for the type 'Type'. (undefined_method at [xylophone] lib/main.dart:17)
import 'package:flutter/material.dart';
import 'package:audioplayers/audio_cache.dart';
void main() => runApp(XylophoneApp());
class XylophoneApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Center(
child: FlatButton(
onPressed: () {
final player = AudioCache;
player.play('note1.wav');
},
child: Text('Click Me'),
),
),
),
),
);
}
}
I'm working on the same course! You've got the opening/closing brackets missing on the AudioCache declaration. It should be:
final player = AudioCache();
I'd also do this for playing the audio, as it will stop making a sound after you press a few buttons:
player.play('note$note.wav',
mode: PlayerMode.LOW_LATENCY,
stayAwake: false);
It's better than the original code, but not perfect. I think there are audio 32 channels that get used up quite quickly - the about change seems to release them a bit quicker. Good luck with the rest of the course!
Migration Guide
dependencies: audioplayers: ^0.x.x
to
dependencies: audioplayers: ^1.0.1
https://github.com/bluefireteam/audioplayers/blob/main/migration_guide.md
AudioCache is dead, long live Sources One of the main changes was my desire to "kill" the AudioCache API due to the vast confusion that it caused with users (despite our best efforts documenting everything).
We still have the AudioCache class but its APIs are exclusively dedicated to transforming asset files into local files, cache them, and provide the path. It however doesn't normally need be used by end users because the AudioPlayer itself is now capable of playing audio from any Source.
What is a Source? It's a sealed class that can be one of:
UrlSource: get the audio from a remote URL from the Internet DeviceFileSource: access a file in the user's device, probably selected by a file picker AssetSource: play an asset bundled with your app, normally within the assets directory BytesSource (only some platforms): pass in the bytes of your audio directly (read it from anywhere). If you use AssetSource, the AudioPlayer will use its instance of AudioCache (which defaults to the global cache if unchanged) automatically. This unifies all playing APIs under AudioPlayer and entirely removes the AudioCache detail for most users.
AudioCache is obsolete now, try this
import 'package:audioplayers/audioplayers.dart';
final player = AudioPlayer();
player.play(AssetSource('note1.wav'));
AudioCache is deprecated. It will not work now.
Instead use this code:
child: TextButton(
onPressed: () async {
final player = AudioPlayer();
await player.play(
AssetSource('note1.wav'),
);
},
child: Text("Play me"),
),

Is there a method in Flutter to open Android's (and iOS's) built in Bluetooth menu

I am currently developing an application with Flutter, I want to add bluetooth support to it, I was thinking that it could be cool to use Android's built in bluetooth menu to choose which bluetooth device to pair with instead of developing my own, but is it possible ?
I have searched on google but found no answer (let me know if I didn't searched enough), would be cool if someone could enlighten this topic. Thanks.
Try using system_setting package.
Here's an example
import 'package:flutter/material.dart';
import 'package:system_setting/system_setting.dart';
void main() => runApp(MaterialApp(
home: Scaffold(
body: Center(
child: RaisedButton(
onPressed: _jumpToSetting,
child: Text('Goto setting'),
),
),
),
));
_jumpToSetting() {
SystemSetting.goto(SettingTarget.BLUETOOTH);
}
Instead of system_setting package use app_setings package.
ElevatedButton(
child: Text("Bluetooth"),
onPressed: () {
AppSettings.openBluetoothSettings();
},
)
This Code will take you to the Bluetooth Settings of Android and iOS. More examples in https://pub.dev/packages/app_settings/example
https://pub.dev/packages/android_intent is discontinued
https://pub.dev/packages/intent is not nullsafe support
https://pub.dev/packages/android_intent_plus its ok
import 'dart:io' show Platform;
import 'package:flutter/material.dart';
import 'package:app_settings/app_settings.dart';
import 'package:android_intent_plus/android_intent.dart';
class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
if (Platform.isAndroid) {
const AndroidIntent(
action:
'android.bluetooth.adapter.action.REQUEST_ENABLE',
).launch().catchError(
(e) => AppSettings.openBluetoothSettings());
} else {
AppSettings.openBluetoothSettings();
}
}
return Center(child: CircularProgressIndicator());
}
If I understand correctly, what you want is to open the Android Bluetooth config screen when the user clicks a button in Flutter, is that right?
To achieve that you can use the plugin android_intent [1] to open the settings screen
AndroidIntent intent = AndroidIntent(
action: 'android.settings.BLUETOOTH_SETTINGS',
);
await intent.launch();
You may want to check if the platform is android before to do that in case that you export the app to iOS.
[1] https://pub.dev/packages/android_intent