fix: 调整播放器样式,修改快速切换 AudioPlayer 无法正确释放的问题

master
Spark 7 days ago
parent bbf70c81c6
commit fc613ee961

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.5 KiB

After

Width:  |  Height:  |  Size: 299 KiB

@ -6,11 +6,11 @@ import 'package:music_player_miao/models/universal_bean.dart';
import '../models/getInfo_bean.dart'; import '../models/getInfo_bean.dart';
import '../models/login_bean.dart'; import '../models/login_bean.dart';
const String _LoginURL = 'http://flyingpig.fun:10010/users/login'; const String _LoginURL = 'http://8.210.250.29:10010/users/login';
const String _LogoutURL = 'http://flyingpig.fun:10010/users/logout'; const String _LogoutURL = 'http://8.210.250.29:10010/users/logout';
const String _SetupURL = 'http://flyingpig.fun:10010/email/register'; const String _SetupURL = 'http://8.210.250.29:10010/email/register';
const String _verificationURL = 'http://flyingpig.fun:10010/email/verificationCode'; const String _verificationURL = 'http://8.210.250.29:10010/email/verificationCode';
const String _GetInfoURL = 'http://flyingpig.fun:10010/users/info'; const String _GetInfoURL = 'http://8.210.250.29:10010/users/info';
/// ///
class LoginApiClient { class LoginApiClient {

@ -3,8 +3,8 @@ import 'package:dio/dio.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../models/universal_bean.dart'; import '../models/universal_bean.dart';
const String _changeNameURL = 'http://flyingpig.fun:10010/users/username'; const String _changeNameURL = 'http://8.210.250.29:10010/users/username';
const String _changeHeaderURL = 'http://flyingpig.fun:10010/users/avatar'; const String _changeHeaderURL = 'http://8.210.250.29:10010/users/avatar';
class ChangeApiClient { class ChangeApiClient {
final Dio dio = Dio(); final Dio dio = Dio();

@ -3,7 +3,7 @@
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:music_player_miao/models/universal_bean.dart'; import 'package:music_player_miao/models/universal_bean.dart';
const String _CollectionURL = 'http://flyingpig.fun:10010/collections'; const String _CollectionURL = 'http://8.210.250.29:10010/collections';
class CollectionApiMusic { class CollectionApiMusic {
final Dio dio = Dio(); final Dio dio = Dio();

@ -4,7 +4,7 @@
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:music_player_miao/models/universal_bean.dart'; import 'package:music_player_miao/models/universal_bean.dart';
const String _LikesURL = 'http://flyingpig.fun:10010/likes'; const String _LikesURL = 'http://8.210.250.29:10010/likes';
class LikesApiMusic { class LikesApiMusic {
final Dio dio = Dio(); final Dio dio = Dio();

@ -4,10 +4,10 @@ import '../common_widget/Song_widegt.dart';
import '../models/getMusicList_bean.dart'; import '../models/getMusicList_bean.dart';
import '../models/getRank_bean.dart'; import '../models/getRank_bean.dart';
const String _getMusic1 = 'http://flyingpig.fun:10010/musics/1'; const String _getMusic1 = 'http://8.210.250.29:10010/musics/1';
const String _getMusic2 = 'http://flyingpig.fun:10010/musics/2'; const String _getMusic2 = 'http://8.210.250.29:10010/musics/2';
const String _getMusic3 = 'http://flyingpig.fun:10010/musics/3'; const String _getMusic3 = 'http://8.210.250.29:10010/musics/3';
const String _getSongDetail = 'http://flyingpig.fun:10010/musics'; const String _getSongDetail = 'http://8.210.250.29:10010/musics';
/// ///
class GetMusic { class GetMusic {
@ -62,7 +62,7 @@ class GetMusicDetail {
Future<Song> getMusicDetail({required int songId, required String Authorization}) async { Future<Song> getMusicDetail({required int songId, required String Authorization}) async {
try { try {
// songId URL // songId URL
final String url = 'http://flyingpig.fun:10010/musics/$songId'; // API final String url = 'http://8.210.250.29:10010/musics/$songId'; // API
// GET // GET
Response response = await dio.get( Response response = await dio.get(

@ -2,7 +2,7 @@ import 'package:dio/dio.dart';
import '../models/getRank_bean.dart'; import '../models/getRank_bean.dart';
const String _getRank = 'http://flyingpig.fun:10010/musics/rank-list'; const String _getRank = 'http://8.210.250.29:10010/musics/rank-list';
/// ///
class GetRank { class GetRank {
final Dio dio = Dio(); final Dio dio = Dio();

@ -6,8 +6,8 @@ import 'package:music_player_miao/models/universal_bean.dart';
import '../models/getComment_bean.dart'; import '../models/getComment_bean.dart';
import '../models/search_bean.dart'; import '../models/search_bean.dart';
const String _SearchURL = 'http://flyingpig.fun:10010/musics/search'; const String _SearchURL = 'http://8.210.250.29:10010/musics/search';
const String _postComment = 'http://flyingpig.fun:10010/comments'; const String _postComment = 'http://8.210.250.29:10010/comments';
/// ///
class SearchMusic { class SearchMusic {
final Dio dio = Dio(); final Dio dio = Dio();

@ -7,7 +7,7 @@ import 'dart:io';
import 'package:path/path.dart'; import 'package:path/path.dart';
const String _ReleaseURL = 'http://flyingpig.fun:10010/musics'; const String _ReleaseURL = 'http://8.210.250.29:10010/musics';
/// ///
class ReleaseApi { class ReleaseApi {

@ -5,15 +5,15 @@ import 'package:music_player_miao/models/songlist_bean.dart';
import '../models/getAllSongs_bean.dart'; import '../models/getAllSongs_bean.dart';
import '../models/universal_bean.dart'; import '../models/universal_bean.dart';
import 'package:music_player_miao/models/getMyWorks.dart'; import 'package:music_player_miao/models/getMyWorks.dart';
const String _SonglistURL = 'http://flyingpig.fun:10010/songlists'; const String _SonglistURL = 'http://8.210.250.29:10010/songlists';
const String _MyWorksURL = 'http://flyingpig.fun:10010/musics/upload-music'; const String _MyWorksURL = 'http://8.210.250.29:10010/musics/upload-music';
class SonglistApi { class SonglistApi {
final Dio dio = Dio(); final Dio dio = Dio();
/// ///
Future<MyMusicListBean> getAllSongs({required int id, required String Authorization}) async { Future<MyMusicListBean> getAllSongs({required int id, required String Authorization}) async {
String urlWithId = 'http://flyingpig.fun:10010/songlist-musics/$id/music-list'; String urlWithId = 'http://8.210.250.29:10010/songlist-musics/$id/music-list';
Response response = await dio.get(urlWithId, Response response = await dio.get(urlWithId,
options: Options(headers: { options: Options(headers: {
'Authorization': Authorization, 'Authorization': Authorization,

@ -1,65 +0,0 @@
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
class PasswordManager {
static const String PREFS_KEY = 'stored_password';
late SharedPreferences _prefs;
//
static final PasswordManager _instance = PasswordManager._internal();
factory PasswordManager() {
return _instance;
}
PasswordManager._internal();
//
Future<void> init() async {
_prefs = await SharedPreferences.getInstance();
}
//
Future<void> savePassword(String username, String password) async {
final Map<String, String> credentials = {
'username': username,
'password': password,
};
await _prefs.setString(PREFS_KEY, json.encode(credentials));
}
//
Map<String, String>? getStoredCredentials() {
final String? storedData = _prefs.getString(PREFS_KEY);
if (storedData != null) {
final Map<String, String> decoded = json.decode(storedData);
return {
'username': decoded['username'] as String,
'password': decoded['password'] as String,
};
}
return null;
}
//
bool hasStoredPassword() {
return _prefs.containsKey(PREFS_KEY);
}
//
Future<void> removePassword() async {
await _prefs.remove(PREFS_KEY);
}
//
String? getStoredUsername() {
final credentials = getStoredCredentials();
return credentials?['username'];
}
//
String? getStoredPassword() {
final credentials = getStoredCredentials();
return credentials?['password'];
}
}

@ -25,7 +25,6 @@ class _BeginViewState extends State<BeginView> with TickerProviderStateMixin {
super.initState(); super.initState();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Container( return Container(
@ -39,7 +38,7 @@ class _BeginViewState extends State<BeginView> with TickerProviderStateMixin {
backgroundColor: Colors.transparent, backgroundColor: Colors.transparent,
resizeToAvoidBottomInset: false, resizeToAvoidBottomInset: false,
body: ScrollConfiguration( body: ScrollConfiguration(
behavior: ScrollBehavior().copyWith( behavior: const ScrollBehavior().copyWith(
physics: const NeverScrollableScrollPhysics(), physics: const NeverScrollableScrollPhysics(),
), ),
child: Column( child: Column(

@ -8,7 +8,6 @@ import 'package:music_player_miao/view/main_tab_view/main_tab_view.dart';
import '../../api/api_client.dart'; import '../../api/api_client.dart';
import '../../common/color_extension.dart'; import '../../common/color_extension.dart';
import '../../common/password_manager.dart';
import '../../models/getInfo_bean.dart'; import '../../models/getInfo_bean.dart';
import '../../models/login_bean.dart'; import '../../models/login_bean.dart';
import '../../widget/my_text_field.dart'; import '../../widget/my_text_field.dart';
@ -27,7 +26,6 @@ class _LoginVState extends State<LoginV> {
bool signInRequired = false; bool signInRequired = false;
IconData iconPassword = CupertinoIcons.eye_fill; IconData iconPassword = CupertinoIcons.eye_fill;
bool obscurePassword = true; bool obscurePassword = true;
final passwordManager = Get.put(PasswordManager());
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -80,13 +78,22 @@ class _LoginVState extends State<LoginV> {
child: TextButton( child: TextButton(
onPressed: () async { onPressed: () async {
try { try {
Get.dialog(
Center(
child: CircularProgressIndicator(
backgroundColor: Colors.grey[200],
),
),
barrierDismissible: false, //
);
LoginBean bean = await LoginApiClient().login( LoginBean bean = await LoginApiClient().login(
email: nameController.text, email: nameController.text,
password: passwordController.text, password: passwordController.text,
); );
if (bean.code == 200) { if (bean.code == 200) {
passwordManager.savePassword(nameController.text, passwordController.text); Get.back();
Get.to(() => const MainTabView()); Get.off(() => const MainTabView());
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: const Center( content: const Center(
@ -113,9 +120,12 @@ class _LoginVState extends State<LoginV> {
.getInfo( .getInfo(
Authorization: AppData().currentToken); Authorization: AppData().currentToken);
} else { } else {
Get.back();
throw Exception("账号或密码错误"); throw Exception("账号或密码错误");
} }
} catch (error) { } catch (error) {
Get.back();
print(error.toString());
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: Center( content: Center(

@ -91,7 +91,7 @@ class _MainTabViewState extends State<MainTabView> with SingleTickerProviderStat
width: 32, width: 32,
height: 32, height: 32,
), ),
text: "个人", text: "我的",
), ),
], ],
), ),

@ -7,10 +7,7 @@ import '../../view_model/home_view_model.dart';
import '../api/api_music_likes.dart'; import '../api/api_music_likes.dart';
import '../api/api_collection.dart'; import '../api/api_collection.dart';
import '../common/download_manager.dart'; import '../common/download_manager.dart';
import '../api/api_music_list.dart';
import '../api/api_collection.dart';
import '../common_widget/Song_widegt.dart'; import '../common_widget/Song_widegt.dart';
import '../models/getMusicList_bean.dart';
import '../view/commend_view.dart'; import '../view/commend_view.dart';
import '../models/universal_bean.dart'; import '../models/universal_bean.dart';
@ -30,7 +27,7 @@ class MusicView extends StatefulWidget {
State<MusicView> createState() => _MusicViewState(); State<MusicView> createState() => _MusicViewState();
} }
class _MusicViewState extends State<MusicView> { class _MusicViewState extends State<MusicView> with SingleTickerProviderStateMixin {
final homeVM = Get.put(HomeViewModel()); final homeVM = Get.put(HomeViewModel());
bool _isDisposed = false; bool _isDisposed = false;
AppData appData = AppData(); AppData appData = AppData();
@ -57,8 +54,42 @@ class _MusicViewState extends State<MusicView> {
List<bool> likes = []; List<bool> likes = [];
List<bool> collection = []; List<bool> collection = [];
late AnimationController _rotationController;
bool _isLoading = false; bool _isLoading = false;
@override
void initState() {
super.initState();
currentSongIndex = widget.initialSongIndex;
playerInit();
_initializeAsync();
_rotationController = AnimationController(
duration: const Duration(seconds: 20),
vsync: this,
);
// 2.
_audioPlayer.playerStateStream.listen((state) {
if (state.playing) {
//
_rotationController.repeat();
} else {
//
_rotationController.stop(canceled: false);
}
});
}
@override
void dispose() {
_isDisposed = true;
_audioPlayer.dispose();
_rotationController.dispose();
super.dispose();
}
Future<void> _fetchSonglistData() async { Future<void> _fetchSonglistData() async {
setState(() { setState(() {
for (int i = 0; i < widget.songList.length; i++) { for (int i = 0; i < widget.songList.length; i++) {
@ -86,7 +117,8 @@ class _MusicViewState extends State<MusicView> {
try { try {
// //
unawaited(_audioPlayer.stop()); await _audioPlayer.stop();
_rotationController.reset();
// Check for local file first // Check for local file first
final localSong = downloadManager.getLocalSong(currentSongIndex); final localSong = downloadManager.getLocalSong(currentSongIndex);
@ -186,8 +218,10 @@ class _MusicViewState extends State<MusicView> {
void playOrPause() async { void playOrPause() async {
if (_audioPlayer.playing) { if (_audioPlayer.playing) {
await _audioPlayer.pause(); await _audioPlayer.pause();
_rotationController.stop(canceled: false);
} else { } else {
await _audioPlayer.play(); await _audioPlayer.play();
_rotationController.repeat();
} }
setState(() {}); setState(() {});
} }
@ -222,21 +256,6 @@ class _MusicViewState extends State<MusicView> {
await _updateCurrentSong(); await _updateCurrentSong();
} }
@override
void initState() {
super.initState();
currentSongIndex = widget.initialSongIndex;
playerInit();
_initializeAsync();
}
@override
void dispose() {
_isDisposed = true;
_audioPlayer.dispose();
super.dispose();
}
void _changeCurrentSong(int index) { void _changeCurrentSong(int index) {
if (!_isDisposed) { if (!_isDisposed) {
setState(() { setState(() {
@ -245,6 +264,36 @@ class _MusicViewState extends State<MusicView> {
}); });
} }
} }
Widget _buildRotatingAlbumCover() {
return RotationTransition(
turns: _rotationController,
child: Stack(
alignment: Alignment.center,
children: [
Positioned(
child: ClipRRect(
child: Image.network(
widget.songList[currentSongIndex].artistPic,
width: 225,
height: 225,
fit: BoxFit.cover,
),
),
),
ClipRRect(
child: Image.asset(
"assets/img/music_Ellipse.png",
width: 350,
height: 350,
fit: BoxFit.cover,
),
),
],
),
);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Container( return Container(
@ -335,31 +384,7 @@ class _MusicViewState extends State<MusicView> {
height: 80, height: 80,
), ),
Center( Center(
child: Stack( child: _buildRotatingAlbumCover(),
alignment: Alignment.center,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Image.asset(
"assets/img/music_Ellipse.png",
width: 320,
height: 320,
fit: BoxFit.cover,
),
),
Positioned(
child: ClipRRect(
borderRadius: BorderRadius.circular(80),
child: Image.network(
widget.songList[currentSongIndex].artistPic,
width: 230,
height: 230,
fit: BoxFit.cover,
),
),
)
],
),
), ),
const SizedBox( const SizedBox(
height: 60, height: 60,

Loading…
Cancel
Save