I am designing the music player application as android.
I added some parts of the app and moved on to adding permissions.
after placing app permissions my android app started crashing.
home.dart:
import 'package:beats/consts/colors.dart';
import 'package:beats/consts/text_style.dart';
import 'package:beats/controllers/player_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:on_audio_query/on_audio_query.dart';
class Home extends StatelessWidget {
const Home({super.key});
@override
Widget build(BuildContext context) {
var controller = Get.put(PlayerController());
return Scaffold(
backgroundColor: bgDarkColor,
appBar: AppBar(
backgroundColor: bgDarkColor,
actions: [
IconButton(
onPressed: () {},
icon: const Icon(Icons.search, color: whiteColor))
],
leading: const Icon(Icons.sort_rounded, color: whiteColor),
title: Text("Beats", style: ourStyle(family: bold, size: 18)),
),
body: FutureBuilder<List<SongModel>>(
future: controller.audioQuery.querySongs(
ignoreCase: true,
orderType: OrderType.ASC_OR_SMALLER,
sortType: null,
uriType: UriType.EXTERNAL),
builder: (BuildContext context, snapshot) {
if (snapshot.data == null) {
return const Center(
child: CircularProgressIndicator(),
);
} else if (snapshot.data!.isEmpty) {
return Center(
child: Text(
"not found",
style: ourStyle(),
),
);
}
else{
return Padding(
padding: const EdgeInsets.all(5.0),
child: ListView.builder(
physics: const BouncingScrollPhysics(),
itemCount: 100,
itemBuilder: (BuildContext context, int index) {
return Container(
child: ListTile(
tileColor: bgColor,
title: Text(
snapshot.data![index].displayNameWOExt,
style: ourStyle(family: bold, size: 15),
),
subtitle: Text(
"${snapshot.data![index].artist}",
style: ourStyle(family: regular, size: 12),
),
leading: const Icon(
Icons.music_note,
color: whiteColor,
size: 32,
),
trailing: const Icon(
Icons.play_arrow,
color: whiteColor,
size: 26,
),
),
);
},
),
);
}
}
));
}
}
player_controller.dart:
import 'package:get/get.dart';
import 'package:on_audio_query/on_audio_query.dart';
import 'package:permission_handler/permission_handler.dart';
class PlayerController extends GetxController {
final audioQuery = OnAudioQuery();
@override
void onInit() {
super.onInit();
checkPermission();
}
checkPermission() async {
var perm = await Permission.storage.request();
if (perm.isGranted) {
} else {
checkPermission();
}
}
}
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- To access storage -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:label="beats"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:taskAffinity=""
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
<!-- Required to query activities that can process text, see:
https://developer.android.com/training/package-visibility and
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
<queries>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT"/>
<data android:mimeType="text/plain"/>
</intent>
</queries>
</manifest>
build.gradle:
plugins {
id "com.android.application"
id "kotlin-android"
// The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
id "dev.flutter.flutter-gradle-plugin"
}
def localProperties = new Properties()
def localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader("UTF-8") { reader ->
localProperties.load(reader)
}
}
def flutterVersionCode = localProperties.getProperty("flutter.versionCode")
if (flutterVersionCode == null) {
flutterVersionCode = "1"
}
def flutterVersionName = localProperties.getProperty("flutter.versionName")
if (flutterVersionName == null) {
flutterVersionName = "1.0"
}
android {
namespace = "com.example.beats"
compileSdk = 34
ndkVersion = flutter.ndkVersion
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId = "com.example.beats"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdk = 21
targetSdk = 34
versionCode = flutterVersionCode.toInteger()
versionName = flutterVersionName
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig = signingConfigs.debug
}
}
}
flutter {
source = "../.."
}
the error is shows:
Launching libmain.dart on sdk gphone64 x86 64 in debug mode...
√ Built buildappoutputsflutter-apkapp-debug.apk
W/HWUI ( 3399): Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
W/HWUI ( 3399): Failed to initialize 101010-2 format, error = EGL_SUCCESS
I/Gralloc4( 3399): mapper 4.x is not supported
I/m.example.beats( 3399): Compiler allocated 4968KB to compile void android.view.ViewRootImpl.performTraversals()
I/m.example.beats( 3399): Compiler allocated 5155KB to compile void android.view.ViewRootImpl.performTraversals()
Connecting to VM Service at ws://127.0.0.1:50941/sEATj_-yDJ0=/ws
[GETX] Instance "PlayerController" has been created
[GETX] Instance "PlayerController" has been initialized
W/OnAudioQueryPlugin( 3399): The application doesn't have access to the library
E/AndroidRuntime( 3399): FATAL EXCEPTION: main
E/AndroidRuntime( 3399): Process: com.example.beats, PID: 3399
E/AndroidRuntime( 3399): java.lang.IllegalStateException: Reply already submitted
E/AndroidRuntime( 3399): at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:431)
E/AndroidRuntime( 3399): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:272)
E/AndroidRuntime( 3399): at com.lucasjosino.on_audio_query.queries.AudioQuery$querySongs$1.invokeSuspend(AudioQuery.kt:67)
E/AndroidRuntime( 3399): at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E/AndroidRuntime( 3399): at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
E/AndroidRuntime( 3399): at android.os.Handler.handleCallback(Handler.java:959)
E/AndroidRuntime( 3399): at android.os.Handler.dispatchMessage(Handler.java:100)
E/AndroidRuntime( 3399): at android.os.Looper.loopOnce(Looper.java:232)
E/AndroidRuntime( 3399): at android.os.Looper.loop(Looper.java:317)
E/AndroidRuntime( 3399): at android.app.ActivityThread.main(ActivityThread.java:8699)
E/AndroidRuntime( 3399): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 3399): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
E/AndroidRuntime( 3399): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
E/AndroidRuntime( 3399): Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@6c3570f, Dispatchers.Main.immediate]
I/Process ( 3399): Sending signal. PID: 3399 SIG: 9
Lost connection to device.
Exited.
I converted the app to .apk and tried it on my phone but the app crashed.
When I tried it on a lower level phone, the app started working.
the name of my virtual android machine:
Pixel 3a API 35 mobile
here is the full project:
Github music player