diff --git a/README.md b/README.md index 6b4dcbd..9f0b9b8 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Gallery Image Gallery Image Gallery Image +Gallery Image Gallery Image Gallery Image Gallery Image @@ -50,6 +51,7 @@ - [王叔不秃Flutter教程](https://www.bilibili.com/video/BV1dt4y117J9/?spm_id_from=333.788&vd_source=009060b038d734b4de0f2c3ccf982d98) 需要有点基础看,应该是b站最好的flutter教学。 - [Flutter GetX使用---简洁的魅力!](https://juejin.cn/post/6924104248275763208) - [Flutter之GetX依赖注入Bindings使用详解](https://juejin.cn/post/7062516045130498084) +- [后台播放音频](https://github.com/ryanheise/just_audio/blob/minor/just_audio_background/example/lib/main.dart) ### 工具 ### - [脚手架 注意阅读Readme](https://github.com/xieyezi/flutter-getx-template) - [vscode 插件](https://marketplace.visualstudio.com/items?itemName=xieyezi.monia-getx-template) 脚手架作者写的,用于快速创建页面 diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index df6d2fb..d682d10 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,15 +1,18 @@ + + + + + + + + + + + + + diff --git a/ios/Podfile b/ios/Podfile index 7cf9ec6..6355592 100644 --- a/ios/Podfile +++ b/ios/Podfile @@ -39,8 +39,14 @@ post_install do |installer| project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0' + target.build_configurations.each do |config| + config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [ + '$(inherited)', + 'AUDIO_SESSION_MICROPHONE=0' + ] end end + end end installer.pods_project.targets.each do |target| flutter_additional_ios_build_settings(target) diff --git a/ios/Podfile.lock b/ios/Podfile.lock index 900862e..1038696 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -1,4 +1,6 @@ PODS: + - audio_service (0.0.1): + - Flutter - audio_session (0.0.1): - Flutter - Flutter (1.0.0) @@ -28,6 +30,7 @@ PODS: - Flutter DEPENDENCIES: + - audio_service (from `.symlinks/plugins/audio_service/ios`) - audio_session (from `.symlinks/plugins/audio_session/ios`) - Flutter (from `Flutter`) - image_gallery_saver (from `.symlinks/plugins/image_gallery_saver/ios`) @@ -45,6 +48,8 @@ SPEC REPOS: - FMDB EXTERNAL SOURCES: + audio_service: + :path: ".symlinks/plugins/audio_service/ios" audio_session: :path: ".symlinks/plugins/audio_session/ios" Flutter: @@ -69,6 +74,7 @@ EXTERNAL SOURCES: :path: ".symlinks/plugins/webview_flutter_wkwebview/ios" SPEC CHECKSUMS: + audio_service: f509d65da41b9521a61f1c404dd58651f265a567 audio_session: 4f3e461722055d21515cf3261b64c973c062f345 Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854 FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a @@ -82,6 +88,6 @@ SPEC CHECKSUMS: vibration: 7d883d141656a1c1a6d8d238616b2042a51a1241 webview_flutter_wkwebview: b7e70ef1ddded7e69c796c7390ee74180182971f -PODFILE CHECKSUM: d39aea45b624e300ecabbcb5bf4af597e0589f7c +PODFILE CHECKSUM: 7e0f44c2569118dfe5bb4c205ce834f8885af941 COCOAPODS: 1.11.3 diff --git a/ios/Runner/Info.plist b/ios/Runner/Info.plist index d0a5a01..c8dda41 100644 --- a/ios/Runner/Info.plist +++ b/ios/Runner/Info.plist @@ -47,5 +47,9 @@ UIApplicationSupportsIndirectInputEvents + UIBackgroundModes + + audio + diff --git a/lib/main.dart b/lib/main.dart index aacb75b..dade072 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -7,8 +7,14 @@ import 'package:all_universe_flutter/pages/Index/index_binding.dart'; import 'package:all_universe_flutter/router/app_pages.dart'; import 'package:get/get.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; +import 'package:just_audio_background/just_audio_background.dart'; void main() => Global.init().then((e) async { + await JustAudioBackground.init( + androidNotificationChannelId: 'com.ryanheise.bg_demo.channel.audio', + androidNotificationChannelName: 'Audio playback', + androidNotificationOngoing: true, + ); runApp(MyApp()); }); diff --git a/lib/pages/play/play_controller.dart b/lib/pages/play/play_controller.dart index 4f31909..bd64d34 100644 --- a/lib/pages/play/play_controller.dart +++ b/lib/pages/play/play_controller.dart @@ -1,14 +1,13 @@ -import 'dart:developer'; - import 'package:all_universe_flutter/pages/play/play_state.dart'; import 'package:get/get.dart'; import 'package:just_audio/just_audio.dart'; +import 'package:just_audio_background/just_audio_background.dart'; class PlayController extends GetxController { - final PlayState state = PlayState(); - + late PlayState state; @override - void onInit() async { + void onInit() { + state = PlayState(); // await Future.delayed(Duration(seconds: 10)); /// 监听播放器播放进度 @@ -72,12 +71,28 @@ class PlayController extends GetxController { state.playViewList.indexWhere((item) => item.id == state.playData.id); if (index != -1) { state.playViewList.removeAt(index); - await state.playList.removeAt(index); } state.playViewList.insert(0, state.playData); - await state.playList - .insert(0, AudioSource.uri(Uri.parse(state.playData.url))); + List audioSources = state.playViewList + .map((item) => AudioSource.uri( + Uri.parse(item.url), + tag: MediaItem( + id: '${item.id}', + album: item.title, + title: item.title, + artUri: Uri.parse(item.cover), + ), + )) + .toList(); + + state.playList = ConcatenatingAudioSource( + // 播放下个音频之前加载 + useLazyPreparation: true, + // 定义切换算法 + shuffleOrder: DefaultShuffleOrder(), + children: audioSources, + ); await state.player.setAudioSource(state.playList, initialIndex: 0, initialPosition: Duration.zero); @@ -122,7 +137,7 @@ class PlayController extends GetxController { state.playViewList.insert(newIndex, item); /// 播放列表 - state.playList.removeAt(oldIndex); - state.playList.insert(0, AudioSource.uri(Uri.parse(item.url))); + state.playList.children.removeAt(oldIndex); + state.playList.children.insert(0, AudioSource.uri(Uri.parse(item.url))); } } diff --git a/lib/services/audio_handler.dart b/lib/services/audio_handler.dart new file mode 100644 index 0000000..8b10964 --- /dev/null +++ b/lib/services/audio_handler.dart @@ -0,0 +1,17 @@ +import 'package:audio_service/audio_service.dart'; + +Future initAudioService() async { + return await AudioService.init( + builder: () => MyAudioHandler(), + config: const AudioServiceConfig( + androidNotificationChannelId: 'com.mycompany.myapp.audio', + androidNotificationChannelName: 'Audio Service Demo', + androidNotificationOngoing: true, + androidStopForegroundOnPause: true, + ), + ); +} + +class MyAudioHandler extends BaseAudioHandler { + // TODO: Override needed methods +} diff --git a/lib/services/service_locator.dart b/lib/services/service_locator.dart new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/lib/services/service_locator.dart @@ -0,0 +1 @@ + diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 42e7956..c7529dd 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,6 +5,7 @@ import FlutterMacOS import Foundation +import audio_service import audio_session import just_audio import path_provider_foundation @@ -12,6 +13,7 @@ import shared_preferences_foundation import sqflite func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + AudioServicePlugin.register(with: registry.registrar(forPlugin: "AudioServicePlugin")) AudioSessionPlugin.register(with: registry.registrar(forPlugin: "AudioSessionPlugin")) JustAudioPlugin.register(with: registry.registrar(forPlugin: "JustAudioPlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) diff --git a/pubspec.lock b/pubspec.lock index be14375..87e1ca2 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -9,6 +9,30 @@ packages: url: "http://mirrors.cnnic.cn/dart-pub/" source: hosted version: "2.10.0" + audio_service: + dependency: transitive + description: + name: audio_service + sha256: "7e86d7ce23caad605199f7b25e548fe7b618fb0c150fa0585f47a910fe7e7a67" + url: "http://mirrors.cnnic.cn/dart-pub/" + source: hosted + version: "0.18.9" + audio_service_platform_interface: + dependency: transitive + description: + name: audio_service_platform_interface + sha256: "2c3a1d52803931e836b9693547a71c0c3585ad54219d2214219ed5cfcc3c1af4" + url: "http://mirrors.cnnic.cn/dart-pub/" + source: hosted + version: "0.1.0" + audio_service_web: + dependency: transitive + description: + name: audio_service_web + sha256: "523e64ddc914c714d53eec2da85bba1074f08cf26c786d4efb322de510815ea7" + url: "http://mirrors.cnnic.cn/dart-pub/" + source: hosted + version: "0.1.1" audio_session: dependency: transitive description: @@ -288,6 +312,14 @@ packages: url: "http://mirrors.cnnic.cn/dart-pub/" source: hosted version: "0.9.32" + just_audio_background: + dependency: "direct main" + description: + name: just_audio_background + sha256: "89498f9a53f44f50a550d778ceb3cd64cef21246685d85f427be4d97f6e2eb85" + url: "http://mirrors.cnnic.cn/dart-pub/" + source: hosted + version: "0.0.1-beta.9" just_audio_platform_interface: dependency: transitive description: @@ -372,10 +404,10 @@ packages: dependency: transitive description: name: path_provider_foundation - sha256: "12eee51abdf4d34c590f043f45073adbb45514a108bd9db4491547a2fd891059" + sha256: "818b2dc38b0f178e0ea3f7cf3b28146faab11375985d815942a68eee11c2d0f7" url: "http://mirrors.cnnic.cn/dart-pub/" source: hosted - version: "2.2.0" + version: "2.2.1" path_provider_linux: dependency: transitive description: @@ -476,58 +508,58 @@ packages: dependency: "direct main" description: name: shared_preferences - sha256: "78528fd87d0d08ffd3e69551173c026e8eacc7b7079c82eb6a77413957b7e394" + sha256: "858aaa72d8f61637d64e776aca82e1c67e6d9ee07979123c5d17115031c1b13b" url: "http://mirrors.cnnic.cn/dart-pub/" source: hosted - version: "2.0.20" + version: "2.1.0" shared_preferences_android: dependency: transitive description: name: shared_preferences_android - sha256: ad423a80fe7b4e48b50d6111b3ea1027af0e959e49d485712e134863d9c1c521 + sha256: "8304d8a1f7d21a429f91dee552792249362b68a331ac5c3c1caf370f658873f6" url: "http://mirrors.cnnic.cn/dart-pub/" source: hosted - version: "2.0.17" + version: "2.1.0" shared_preferences_foundation: dependency: transitive description: name: shared_preferences_foundation - sha256: "1e755f8583229f185cfca61b1d80fb2344c9d660e1c69ede5450d8f478fa5310" + sha256: cf2a42fb20148502022861f71698db12d937c7459345a1bdaa88fc91a91b3603 url: "http://mirrors.cnnic.cn/dart-pub/" source: hosted - version: "2.1.5" + version: "2.2.0" shared_preferences_linux: dependency: transitive description: name: shared_preferences_linux - sha256: "3a59ed10890a8409ad0faad7bb2957dab4b92b8fbe553257b05d30ed8af2c707" + sha256: "9d387433ca65717bbf1be88f4d5bb18f10508917a8fa2fb02e0fd0d7479a9afa" url: "http://mirrors.cnnic.cn/dart-pub/" source: hosted - version: "2.1.5" + version: "2.2.0" shared_preferences_platform_interface: dependency: transitive description: name: shared_preferences_platform_interface - sha256: "824bfd02713e37603b2bdade0842e47d56e7db32b1dcdd1cae533fb88e2913fc" + sha256: fb5cf25c0235df2d0640ac1b1174f6466bd311f621574997ac59018a6664548d url: "http://mirrors.cnnic.cn/dart-pub/" source: hosted - version: "2.1.1" + version: "2.2.0" shared_preferences_web: dependency: transitive description: name: shared_preferences_web - sha256: "0dc2633f215a3d4aa3184c9b2c5766f4711e4e5a6b256e62aafee41f89f1bfb8" + sha256: "74083203a8eae241e0de4a0d597dbedab3b8fef5563f33cf3c12d7e93c655ca5" url: "http://mirrors.cnnic.cn/dart-pub/" source: hosted - version: "2.0.6" + version: "2.1.0" shared_preferences_windows: dependency: transitive description: name: shared_preferences_windows - sha256: "71bcd669bb9cdb6b39f22c4a7728b6d49e934f6cba73157ffa5a54f1eed67436" + sha256: "5e588e2efef56916a3b229c3bfe81e6a525665a454519ca51dbcc4236a274173" url: "http://mirrors.cnnic.cn/dart-pub/" source: hosted - version: "2.1.5" + version: "2.2.0" sky_engine: dependency: transitive description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index 6e096de..05d5fce 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -62,8 +62,8 @@ dependencies: flutter_cache_manager: ^3.1.0 # 音频播放 - # audio_service: ^0.18.9 just_audio: ^0.9.31 + just_audio_background: ^0.0.1-beta.9 #底部弹框 modal_bottom_sheet: ^3.0.0-pre diff --git a/screenshots/sc10.png b/screenshots/sc10.png new file mode 100644 index 0000000..5b60793 Binary files /dev/null and b/screenshots/sc10.png differ