Flutter Audio Cache Error with player.play - flutter

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"),
),

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! :-)

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

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

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

How to set default size of macos app in flutter?

I am trying to build a macOS desktop app with flutter. I want the app to be full-width, edge-to-edge. However, when I run the app via the simulator, or after the build, it always launches the app with size 800x600.
I have set the height and width of the root container to double.infinity. In fact, even if I set the height and width to 10.0, it always launches the app with 800x600. I am new to flutter, so probably missing some fundamentals. Most tutorials I have come across talk about building a mobile app where this is never a problem because the app always launches to its full width.
Here is my entire test app code:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(color: Colors.white),
height: double.infinity,
width: double.infinity,
child: Center(
child: Text(
'Hello World',
textDirection: TextDirection.ltr,
style: TextStyle(
fontSize: 32, fontWeight: FontWeight.bold, color: Colors.black),
),
),
);
}
}
There's now a plugin to do this, which is not a permanent thing as it is described as preliminary functionality before eventually being folded into the core libraries.
Using the plugin for now is still likely to be better than hard-coding directly modifying the native code, especially if you have multiple platforms you want to work on.
First add to the pubspec.yaml something like:
dependencies:
...
window_size:
git:
url: git://github.com/google/flutter-desktop-embedding.git
path: plugins/window_size
ref: 927f8cbc09b35d85245c095f2db8df9b186f6618
Using the specific Git reference to include this, as shown above, will give you good control over when you choose to pull updated code and make any changes this might entail.
You can then access various functions to set min/max window sizes, or frame, or get the current values, e.g.:
...
import 'dart:io'
import 'package:window_size/window_size.dart';
...
void main() {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
setWindowTitle("My Desktop App");
setWindowMinSize(Size(375, 750));
setWindowMaxSize(Size(600, 1000));
}
runApp(MyApp());
}
I hope this helps someone. I'll try and update this post when the real answer comes out. It seems likely that the interface will approximate what is presented in this library, but the feature set is likely to undergo some change.
Currently the only way to control the initial size is in native code (follow these issues: 1 and 2 to find out when that changes). You'd most likely want to set it in macos/Runner/MainFlutterWindow.swift.
It's not clear from your description whether you want to launch into full-screen mode, or just have a standard window the size of the client area of the screen; the code involved would be different depending on which you are trying to accomplish.
This package can help with it.
Size size = await DesktopWindow.getWindowSize();
print(size);
await DesktopWindow.setWindowSize(Size(500,500));
await DesktopWindow.setMinWindowSize(Size(400,400));
await DesktopWindow.setMaxWindowSize(Size(800,800));
await DesktopWindow.resetMaxWindowSize();
await DesktopWindow.toggleFullScreen();
bool isFullScreen = await DesktopWindow.getFullScreen();
await DesktopWindow.setFullScreen(true);
await DesktopWindow.setFullScreen(false);
I am not sure if this 100% valid, but I was looking for possibility to set window size. I have found package as #karora mentioned, but I wanted to only set window size and move on. So we can make it using xcode.
In project folder open Runner.xcodeproj:
macos -> Runner.xcodeproj
Then in Xcode project find MainMenu.xib then you can resize your flutter window.
You should be able to achieve this now within Dart code by using the window_size plugin.
The code to set initial window size could be something like:
#override
void initState() {
super.initState();
setWindowFrame(Rect.fromLTRB(1200.0, 500.0, 1800.0, 1125.0));
}
somewhere like your top-level stateful widget.
Though I should note that at the moment for me on Linux, the window frame sizing works, but positioning doesn't.
i need set default size for windows desktop app & this solution worked for me and cover the ios and linux platforms.
import 'package:desktop_window/desktop_window.dart'as window_size;
import 'dart:io';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
window_size.DesktopWindow.setMinWindowSize(Size(375, 750));
window_size.DesktopWindow.setMaxWindowSize(Size(600, 1000));
}
runApp(MyApp());
}

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