Skip to content

Commit

Permalink
feat: ✨后台播放
Browse files Browse the repository at this point in the history
  • Loading branch information
xuergo committed Apr 4, 2023
1 parent 28fe5ac commit ffbb15f
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 29 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<img src="screenshots/sc4.png" alt="Gallery Image" width="30%" />
<img src="screenshots/sc5.png" alt="Gallery Image" width="30%" />
<img src="screenshots/sc6.png" alt="Gallery Image" width="30%" />
<img src="screenshots/sc10.png" alt="Gallery Image" width="30%" />
<img src="screenshots/sc7.png" alt="Gallery Image" width="30%" />
<img src="screenshots/sc8.png" alt="Gallery Image" width="30%" />
<img src="screenshots/sc9.png" alt="Gallery Image" width="30%" />
Expand All @@ -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) 脚手架作者写的,用于快速创建页面
Expand Down
23 changes: 22 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.all_univers">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application
android:label="大宇宙"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:name="com.ryanheise.audioservice.AudioServiceActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
Expand All @@ -34,5 +37,23 @@
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<service
android:name="com.ryanheise.audioservice.AudioService"
android:foregroundServiceType="mediaPlayback"
android:exported="true"
tools:ignore="Instantiatable">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>

<receiver
android:name="com.ryanheise.audioservice.MediaButtonReceiver"
android:exported="true"
tools:ignore="Instantiatable">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
</application>
</manifest>
6 changes: 6 additions & 0 deletions ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
PODS:
- audio_service (0.0.1):
- Flutter
- audio_session (0.0.1):
- Flutter
- Flutter (1.0.0)
Expand Down Expand Up @@ -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`)
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -82,6 +88,6 @@ SPEC CHECKSUMS:
vibration: 7d883d141656a1c1a6d8d238616b2042a51a1241
webview_flutter_wkwebview: b7e70ef1ddded7e69c796c7390ee74180182971f

PODFILE CHECKSUM: d39aea45b624e300ecabbcb5bf4af597e0589f7c
PODFILE CHECKSUM: 7e0f44c2569118dfe5bb4c205ce834f8885af941

COCOAPODS: 1.11.3
4 changes: 4 additions & 0 deletions ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,9 @@
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
</dict>
</plist>
6 changes: 6 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});

Expand Down
35 changes: 25 additions & 10 deletions lib/pages/play/play_controller.dart
Original file line number Diff line number Diff line change
@@ -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));

/// 监听播放器播放进度
Expand Down Expand Up @@ -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<AudioSource> 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);
Expand Down Expand Up @@ -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)));
}
}
17 changes: 17 additions & 0 deletions lib/services/audio_handler.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:audio_service/audio_service.dart';

Future<AudioHandler> 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
}
1 change: 1 addition & 0 deletions lib/services/service_locator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import FlutterMacOS
import Foundation

import audio_service
import audio_session
import just_audio
import path_provider_foundation
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"))
Expand Down
64 changes: 48 additions & 16 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added screenshots/sc10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ffbb15f

Please sign in to comment.