Trouble using the audioplayers package in my flutter app - android

I started with flutter last week and I got this problem with the audioplayer package. I am doing a Flutter Development Bootcamp with Dart at Udemy. I followed almost everything at the video, but I recived a huge error message that I can't solve.
This is my code:
import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
void main() => runApp(XylophoneApp());
class XylophoneApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.lightBlue,
body: SafeArea(
child: Container(
child: Center(
child: TextButton(
onPressed: (){
final player = AudioCache();
player.play('assets/note1');
},
child: Text(
'click me'
),
),
),
),
),
),
);
}
}
This is the error message that i get:
Running Gradle task 'assembleDebug'...
e: Incompatible classes were found in dependencies. Remove them from the classpath or use '-Xskip-metadata-version-check' to suppress errors
e: C:/Users/victo/.gradle/caches/transforms-2/files-2.1/24fa3aa8d2270e5eb067bbe36e9b7563/jetified-kotlin-stdlib-1.5.10.jar!/META-INF/kotlin-stdlib.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15... (The message is much more larger than this)
What went wrong:
Execution failed for task ':audioplayers:compileDebugKotlin'.
Compilation error. See log for more details
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Get more help at https://help.gradle.org
BUILD FAILED in 21s
Exception: Gradle task assembleDebug failed with exit code 1
I've seen this message The binary version of its metadata is 1.5.1, expected version is 1.1.15, but I dont know what to do with it or how I can solve it.
This is my pubspec.yaml:
name: xylophone
description: A new Flutter application.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
audioplayers: ^0.19.1
cupertino_icons: ^0.1.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- assets/

In gradle files, go to build.gradle (Project: YourApp). Then, change the following code (in buildscript):
from
ext.kotlin_version = '1.3.50'
to
ext.kotlin_version = '1.4.32'
or what ever the latest version of Kotlin available and make sure to update Kotlin version on Android Studio as well
After following the instructions, your error will be resolved.

You can look into error log Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15 but try to 1.3.50, i have app with audioplayer i am using this version if it doesn't work use 1.1.15 or the suggested version after changing version uninstall app and run flutter clean and run again
Try downgrade to a lower kotlin version in build.gradle
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
}
Also refer this answers also if doesn't work in case

Migration Guide
dependencies:
audioplayers: ^0.x.x
to
dependencies:
audioplayers: ^1.0.1
https://github.com/bluefireteam/audioplayers/blob/main/migration_guide.md
try with this
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.

If you are using the latest version of audioplayers in my case ^1.0.1 then your code should look like this instead
onPressed: () async {
final player = AudioPlayer();
await player.play(AssetSource('note1.wav'));
},
Wishing you all the best in your learning

Related

Flutter Error: No named parameter with the name 'displayLarge'

I am working on an app, I posted last update on git 2 days ago and haven't touched the code since. Now, I opened android studio and it was giving me this error when I tried to run it... I tried deleting repository from my computer and then cloning it again but it didn't work. I have also tried searching for similar problems, and found out there was a similar problem with speed-dial package and one comment said something about broken package versions. Anyways, this is the error I am getting after I try running my app:
These are first few lines of the error it's giving me, but there is more than a thousand lines following these.
I also tried running flutter doctor and it gave me the following:
google_fonts: ^2.3.1 has also solved the problems left in google_fonts: 2.3.0
Well! This issue will be raised in all projects using the google_fonts with a caret sign (^).
The caret sign (^) is used for pub dependencies in Dart to indicate a range of version numbers that are allowed. Specifically, any version from the specified version up to (but not including) the next non-breaking version is ok.
So google_fonts: ^2.2.0 is the same as '>=2.2.0 <3.0.0', It's shorthand for the longer form. The ^ is saying, I want to automatically use the most up-to-date package from Pub as long as that update won't break anything in my app. The google_fonts: 2.3.0 has some issues with this as of now.
Solution Steps :
Remove (^) from your current google_fonts. max can be set as google_fonts: 2.2.0.
To make sure, this does not conflict with any other dependency, for once do 'pub get'.
Try to invalidate caches and restart. This should clean error, else do flutter clean to remove old updated google_fonts and pub get.
Done! You are good to go. Happy Codding!!!
Please follow the below steps to solve the issue.
Open pubspec.yaml file.
Change google_fonts: ^2.2.0 to google_fonts: 2.2.0
Save.
Well, I don't know if you have tried this, but this problem is described here: issue 219
In summary, you have to use google_fonts v2.3 for Flutter v2.1 or greater.
set the package version to google_fonts: 2.2.0 and it should work properly
as they said, just remove the ^ from the google fonts on the dependencies on the pubspec.yaml
(google_fonts: ^2.3.1 to google_fonts: 2.3.1)
then flutter clean && pub get.
I had the same problem but it was not an issue with dependencies as for others. I stopped getting this error when I put "shape" in the scope of styles.
Example with my problem:
ElevatedButton(
child: Text('Sign in with Google'),
onPressed: _signInWithGoogle,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white,
foregroundColor: Colors.black,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0),
),
),
Example when "shape" stopped giving an error:
ElevatedButton(
child: Text('Sign in with Google'),
onPressed: _signInWithGoogle,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white,
foregroundColor: Colors.black,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0),
),
),
),

How to resolve #bazel_tools//tools/android:databinding_exec when switching from bazel 3.6.0 to 3.7.0

I’ve been using bazel to build an android project, initially starting with a 0.24 version, then using the 2.0 version for a longer time, and now I am trying to switch to the latest 3.7.0 / 3.7.1 versions.
Currently I am using the following important .bazelrc settings for android:
build:androidbuild --crosstool_top=#androidndk//:default_crosstool
build:androidbuild --host_crosstool_top=#bazel_tools//tools/cpp:toolchain
build:android-32 --config=androidbuild --cpu=x86 --fat_apk_cpu=x86
All cc_binary / cc_library targets can still be built the same way we used to, but the android_binary targets no longer builds and I am getting this error:
ERROR: While resolving toolchains for target
#bazel_tools//tools/android:databinding_exec: No matching toolchains
found for types #bazel_tools//tools/cpp:toolchain_type. Maybe
--incompatible_use_cc_configure_from_rules_cc has been flipped and there is no default C++ toolchain added in the WORKSPACE file? See
https://github.com/bazelbuild/bazel/issues/10134 for details and
migration instructions.
I tried to find which was the bazel version that started to throw this error, and I found that I can still build my project with 3.6.0, but I start getting the above error if I switch to 3.7.0 or 3.7.1.
Looking at the 3.6.0 vs 3.7.0 changes, I am wondering if the new create_dummy_sdk_toolchain call in tools/android/android_sdk_repository_template.bzl would cause this failure or there is something else I am missing (like changes in the cc_flags_supplier.bzl / compiler_flag.bzl sources to set incompatible_use_toolchain_transition to True).
I tried the approach of using --platforms instead, but I am still getting errors even in that case:
build:androidbuild --extra_toolchains=#androidndk//:all
build:android-32 --config=androidbuild --cpu=x86 --platforms=//config:android_x86
With:
platform(
name = "android_x86",
constraint_values = [
"#bazel_tools//platforms:x86_32",
"#bazel_tools//platforms:android",
],
cpu_constraints = [
],
os_constraints = [
],
target_platform = True,
)
Error:
ERROR: While resolving toolchains for target
#bazel_tools//tools/android:instrumentation_test_check: No matching
toolchains found for types #bazel_tools//tools/cpp:toolchain_type.
Maybe --incompatible_use_cc_configure_from_rules_cc has been flipped
and there is no default C++ toolchain added in the WORKSPACE file? See
https://github.com/bazelbuild/bazel/issues/10134 for details and
migration instructions.
Can you please advise me on what is missing from the project that is required to solve the problem caused by the 3.6.0 vs 3.7.0 diffs, do we now need to define a proper toolchain for Android (can't we rely anymore on bazel to figure out how to build android targets)?
UPDATE Dec/16/2020
Looks like the problem was caused by this code:
WORKSPACE:
register_execution_platforms(
"//tools/config:host_platform",
)
In the BUILD file:
platform(
name = "host_platform",
constraint_values = [
],
cpu_constraints = [
"#bazel_tools//platforms:x86_32",
"#bazel_tools//platforms:x86_64",
],
host_platform = True,
os_constraints = [
"#bazel_tools//platforms:windows",
],
)
For some reason this works in bazel 3.6.0 even when the host is Linux, but it's no longer working in 3.7.0.
The problem was caused by this code:
WORKSPACE:
register_execution_platforms(
"//tools/config:host_platform",
)
In the BUILD file:
platform(
name = "host_platform",
constraint_values = [
],
cpu_constraints = [
"#bazel_tools//platforms:x86_32",
"#bazel_tools//platforms:x86_64",
],
host_platform = True,
os_constraints = [
"#bazel_tools//platforms:windows",
],
)
For some reason this works in bazel 3.6.0 even when the host is Linux, but it's no longer working in 3.7.0.
when I do bazel build --platforms=//:linux_arm64 it gave me the same error:
hile resolving toolchains for target #io_bazel_rules_go//:cgo_context_data: No matching toolchains found for types #bazel_tools//tools/cpp:toolchain_type. Maybe --incompatible_use_cc_configure_from_rules_cc has been flipped and there is no default C++ toolchain added in the WORKSPACE file? See https://github.com/bazelbuild/bazel/issues/10134 for details and migration instructions.
The follow command works
bazel build --platforms=#io_bazel_rules_go//go/toolchain:linux_arm64

Url Launcher does not work with MissingPluginException Flutter

I am trying to launch a url in my flutter application. What i'm trying to do is very simple and it works in all other projects except for this one! The browser should be launched on an inkwell onTap event. I tried the exact same code in other projects and worked. I also tried to create a new flutter project and the code worked.
The app does not crash and i don't get any error but on Debug i get a missing plugin exception.
I tried flutter clean and flutter run but didn't work! I tried invalidating cache and restart but also didn't work! I tried removing and re installing the plugin but also didn't work!
Here's the code:
_launchMapsUrl() async {
final url = 'https://www.google.com';
if (await canLaunch(url)) {
await launch(url);
} else {
print('Could not launch $url');
}
}
the onTap:
onTap: () {
_launchMapsUrl();
},
The Compiled and Target SDK versions are 29 and the version of the launcher in my pubspec.yaml is url_launcher: ^5.7.10
For the record the other projects that the code worked in are of the same versions
I think it has something to do with caching issue i'm not really sure, i'm very new to flutter.
Can you please recommend a solution.

How to get rid of: "FileSystemException: Cannot open file, path = '...' (OS Error: Permission denied, errno = 13)"?

I'm trying to display an image from my phone in my Flutter app and getting following error:
Another exception was thrown: FileSystemException: Cannot open file, path = '/storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/1570277797774' (OS Error: Permission denied, errno = 13)
I don't understand the Permission denied part, because I added...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
...to my android/app/src/mainAndroidManifest.xml file, and when running the app it shows that it really does have the permissions to read files:
Also the file I'm trying to open exists and seems fine - I can open it with other apps as well:
Here's my Code:
import 'dart:io';
import 'package:flutter/material.dart';
void main() => runApp(FileOps());
class FileOps extends StatefulWidget {
_FileOpsState createState() => _FileOpsState();
}
class _FileOpsState extends State<FileOps> {
File localFile;
String widgetTitle = 'Show Image';
#override
void initState() {
super.initState();
localfile.then((File _localFile) {
setState(() {
localFile = _localFile;
});
});
}
#override
Widget build(BuildContext context) {
return MaterialApp(
title: widgetTitle,
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Text(widgetTitle),
),
body: Column(
children: <Widget>[
Image.file(localFile),
],
),
),
);
}
Future<File> get localfile async {
String imgPath =
'/storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/1570277797774';
return File(imgPath);
}
}
My pubspec.yaml:
name: template_app
description: A template Flutter app for testing and minimal examples
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons:
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
Answers to questions in comments:
"com.android.providers.media" is not the name of my app, idk what it is - but the image I want to open is there
My Android Version is 8.0.0, my phone is also running EMUI 8.0.0 (idk what it is exactly, some Huawei stuff)
I can open the file with the standard app that came with the phone called "Gallery"
What Worked for me is
First in android/app/build.gradle
set your compileSdkVersion to 29 (current highest)
set your targetSdkVersion to 29 (current highest)
Secondly in android/app/src/main/AndroidManifest.xml
add android:requestLegacyExternalStorage="true to the tag
Then rebuild and see if it works for you
Attention: My answer is a bit old now, I heard that a newer version of permission_handler works a bit differently now - so just google how it works nowadays, I guess.
I got it - <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> seemed to only define what permissions were needed, but not actually requesting them :D
So I manually requested the permissions and then everything worked:
I followed this tutorial.
I added permission_handler: to pubspec.yaml
Changed my get localfile get method to:
Future<File> get localfile async {
final PermissionHandler _permissionHandler = PermissionHandler();
var result = await _permissionHandler.requestPermissions([PermissionGroup.storage]);
if (result[PermissionGroup.storage] == PermissionStatus.granted) {
// permission was granted
String imgPath = '/storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/1570277797774';
return File(imgPath);
}
}
i got rid of this error by executing
flutter clean && flutter pub get in the terminal
You can try restarting the code editor and the emulator if the external files changes may be slow message is displayed on your android studio. And then run flutter clean followed by pub get.
Doing this worked for me.
What i did when i faced the problem is i closed the editor and re-opened it after that i run the following commands sequentially before build in android studio.
1: flutter clean
2: flutter pub get

Error: Not found: 'dart:js' import 'dart:js';

I am creating a PopupMenuButton() in the AppBar section.
This is my file:
import 'dart:js';
import 'package:bfdi_app/settings.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
class ProfilePage extends StatefulWidget {
#override
_ProfilePageState createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'App_Name',
),
actions: <Widget>[
PopupMenuButton(
icon: Icon(Icons.settings),
onSelected:(value){
if(value==0){
Navigator.push(
context,
MaterialPageRoute(builder: (context)=>SettingPage())
);
}
},
itemBuilder: (context) => [
PopupMenuItem(
child: Text("Settings"),
value:0,
],
),
],
),
}
Now I am facing an error in Console Log:
Compiler message:
lib/Pages/addPost.dart:1:8: Error: Not found: 'dart:js'
import 'dart:js';
^
lib/Profile/profile.dart:1:8: Error: Not found: 'dart:js'
import 'dart:js';
^
I have already added the dependency dart:js, but still getting the same error.
Error:
Go to your flutter installed directory/.pub-cache/hosted/pub.dartlang.org/js-0.6.3-nullsafety.1/lib/js.dart
Delete or comment this line:
export 'dart:js' show allowInterop, allowInteropCaptureThis;
Then try again
I just del import 'package:js'. it works for me.
Sounds like an import issue as soon as you are trying to compile for a native application.
Try searching for and delte the incorrect dart:js import (import 'dart:js';).
Have a look at this GitHub issue #70373
EDIT: I have switched to the master channel and all working fine. Although I don't see any requirement of dart.js in your current code. so you can remove that too
ORIGINAL ANSWER:
I believe you are using stable channel so try to switch to beta channel.
Open your terminal and run,
$ flutter channel beta
Quoting pub.dev:
Depend on it
Add this to your package's pubspec.yaml file:
dependencies:
js: ^0.6.2
Install it
You can install packages from the command line:with pub:
$ pub get
with flutter:
$ flutter pub get
Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.
Import it
Now in your Dart code, you can use:
import 'package:js/js.dart';
in my case just flutter clean solved the issue
Try using the js package: https://pub.dev/packages/js#-installing-tab-
You should try to use package:js instead of dart:js
Actually mine was solved with just a system restart and a "flutter clean" command. (yes I am using beta channel) But weird thing was the files were existed already but the dependencies somehow couldn't read from the dart packages.
You may not running the app on supported platform by library. In my case I was running on Android which library just supported Web as output. No need to clear cache or comment some code.
This solution allowed me to run tests using dart:js & without modifying my local flutter installation.
check if you are not using that package in your project just remove unused packages.
Step -1
Just remove dart:js
Step -2
Run on terminal "flutter clean"
Step -3
Run on terminal "flutter pub get"
Step -4
Run your project

Categories

Resources