Compare commits
5 Commits
Author | SHA1 | Date |
---|---|---|
Spark | 353fe0175a | 23 hours ago |
Spark | afd2adff5a | 24 hours ago |
Spark | 04e09c2507 | 1 week ago |
Spark | e908f62d86 | 1 week ago |
Spark | 38921f848f | 1 week ago |
After Width: | Height: | Size: 1.5 KiB |
After Width: | Height: | Size: 1.2 KiB |
@ -0,0 +1,201 @@
|
||||
// audio_player_controller.dart
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:just_audio/just_audio.dart';
|
||||
import '../common_widget/Song_widegt.dart';
|
||||
import '../models/getMusicList_bean.dart';
|
||||
import '../common/download_manager.dart';
|
||||
import '../common_widget/app_data.dart';
|
||||
import '../api/api_music_list.dart';
|
||||
|
||||
class AudioPlayerController extends GetxController {
|
||||
final audioPlayer = AudioPlayer();
|
||||
final downloadManager = Get.find<DownloadManager>();
|
||||
final appData = AppData();
|
||||
|
||||
// Observable values
|
||||
final currentSongIndex = 0.obs;
|
||||
final duration = Duration.zero.obs;
|
||||
final position = Duration.zero.obs;
|
||||
final isPlaying = false.obs;
|
||||
final isLoading = false.obs;
|
||||
final isRotating = false.obs;
|
||||
final isDisposed = false.obs;
|
||||
|
||||
// Current song info
|
||||
final artistName = ''.obs;
|
||||
final musicName = ''.obs;
|
||||
final likesStatus = false.obs;
|
||||
final collectionsStatus = false.obs;
|
||||
|
||||
// Song lists
|
||||
final songList = <Song>[].obs;
|
||||
final ids = <int>[].obs;
|
||||
final songUrls = <String>[].obs;
|
||||
final artists = <String>[].obs;
|
||||
final musicNames = <String>[].obs;
|
||||
final likes = <bool>[].obs;
|
||||
final collections = <bool>[].obs;
|
||||
|
||||
StreamSubscription? _positionSubscription;
|
||||
StreamSubscription? _durationSubscription;
|
||||
StreamSubscription? _playerStateSubscription;
|
||||
|
||||
void initWithSongs(List<Song> songs, int initialIndex) {
|
||||
songList.value = songs;
|
||||
currentSongIndex.value = initialIndex;
|
||||
_initializeSongLists();
|
||||
_initializePlayer();
|
||||
}
|
||||
|
||||
void _initializeSongLists() {
|
||||
for (int i = 0; i < songList.length; i++) {
|
||||
ids.add(songList[i].id);
|
||||
songUrls.add(songList[i].musicurl ?? '');
|
||||
artists.add(songList[i].artist);
|
||||
musicNames.add(songList[i].title);
|
||||
likes.add(songList[i].likes ?? false);
|
||||
collections.add(songList[i].collection ?? false);
|
||||
}
|
||||
_updateCurrentSongInfo();
|
||||
}
|
||||
|
||||
void _initializePlayer() {
|
||||
// Position updates
|
||||
_positionSubscription = audioPlayer.positionStream.listen((pos) {
|
||||
position.value = pos;
|
||||
});
|
||||
|
||||
// Duration updates
|
||||
_durationSubscription = audioPlayer.durationStream.listen((dur) {
|
||||
duration.value = dur ?? Duration.zero;
|
||||
});
|
||||
|
||||
// Player state updates
|
||||
_playerStateSubscription = audioPlayer.playerStateStream.listen((state) {
|
||||
// isPlaying.value = state.playing;
|
||||
if (state.processingState == ProcessingState.completed) {
|
||||
playNext();
|
||||
}
|
||||
});
|
||||
|
||||
// Initial load
|
||||
_loadAndPlayCurrentSong();
|
||||
}
|
||||
|
||||
void _updateCurrentSongInfo() {
|
||||
artistName.value = artists[currentSongIndex.value];
|
||||
musicName.value = musicNames[currentSongIndex.value];
|
||||
likesStatus.value = likes[currentSongIndex.value];
|
||||
collectionsStatus.value = collections[currentSongIndex.value];
|
||||
}
|
||||
|
||||
Future<void> toggleLike() async {
|
||||
final currentIndex = currentSongIndex.value;
|
||||
likesStatus.value = !likesStatus.value;
|
||||
likes[currentIndex] = likesStatus.value;
|
||||
}
|
||||
|
||||
Future<void> toggleCollection() async {
|
||||
final currentIndex = currentSongIndex.value;
|
||||
collectionsStatus.value = !collectionsStatus.value;
|
||||
collections[currentIndex] = collectionsStatus.value;
|
||||
}
|
||||
|
||||
Future<void> _loadAndPlayCurrentSong() async {
|
||||
isLoading.value = true;
|
||||
position.value = Duration.zero;
|
||||
duration.value = Duration.zero;
|
||||
_updateCurrentSongInfo();
|
||||
|
||||
await _checkAndUpdateSongStatus(currentSongIndex.value);
|
||||
|
||||
try {
|
||||
await audioPlayer.stop();
|
||||
|
||||
final localSong = downloadManager.getLocalSong(currentSongIndex.value);
|
||||
final audioSource = localSong != null
|
||||
? AudioSource.file(localSong.musicurl!)
|
||||
: AudioSource.uri(Uri.parse(songUrls[currentSongIndex.value]));
|
||||
|
||||
await audioPlayer.setAudioSource(audioSource, preload: true);
|
||||
duration.value = await audioPlayer.duration ?? Duration.zero;
|
||||
await audioPlayer.play();
|
||||
} catch (e) {
|
||||
print('Error loading audio source: $e');
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _checkAndUpdateSongStatus(int index) async {
|
||||
if (songList[index].likes == null || songList[index].collection == null) {
|
||||
try {
|
||||
MusicListBean musicListBean = await GetMusic().getMusicById(
|
||||
id: ids[index],
|
||||
Authorization: appData.currentToken,
|
||||
);
|
||||
|
||||
if (musicListBean.code == 200) {
|
||||
likes[index] = musicListBean.likeOrNot!;
|
||||
collections[index] = musicListBean.collectOrNot!;
|
||||
|
||||
if (index == currentSongIndex.value) {
|
||||
likesStatus.value = musicListBean.likeOrNot!;
|
||||
collectionsStatus.value = musicListBean.collectOrNot!;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error fetching song status: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void playOrPause() async {
|
||||
if (audioPlayer.playing) {
|
||||
isPlaying.value = false;
|
||||
await audioPlayer.pause();
|
||||
} else {
|
||||
await audioPlayer.play();
|
||||
isPlaying.value = true;
|
||||
}
|
||||
}
|
||||
|
||||
void playNext() {
|
||||
if (currentSongIndex.value < songList.length - 1) {
|
||||
currentSongIndex.value++;
|
||||
} else {
|
||||
currentSongIndex.value = 0;
|
||||
}
|
||||
_loadAndPlayCurrentSong();
|
||||
}
|
||||
|
||||
void playPrevious() {
|
||||
if (currentSongIndex.value > 0) {
|
||||
currentSongIndex.value--;
|
||||
} else {
|
||||
currentSongIndex.value = songList.length - 1;
|
||||
}
|
||||
_loadAndPlayCurrentSong();
|
||||
}
|
||||
|
||||
void seekTo(Duration position) async {
|
||||
await audioPlayer.seek(position);
|
||||
}
|
||||
|
||||
void changeSong(int index) {
|
||||
currentSongIndex.value = index;
|
||||
_loadAndPlayCurrentSong();
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
isDisposed.value = true;
|
||||
_positionSubscription?.cancel();
|
||||
_durationSubscription?.cancel();
|
||||
_playerStateSubscription?.cancel();
|
||||
audioPlayer.dispose();
|
||||
super.onClose();
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
class MusicsListBean {
|
||||
int? code;
|
||||
String? msg;
|
||||
List<MusicItem>? data;
|
||||
|
||||
MusicsListBean.fromMap(Map map) {
|
||||
code = map['code'];
|
||||
msg = map['msg'];
|
||||
if (map['data'] != null && map['data'] is List) {
|
||||
data = (map['data'] as List).map((item) => MusicItem.fromMap(item)).toList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MusicItem {
|
||||
int? id;
|
||||
String? name;
|
||||
String? coverPath;
|
||||
String? musicPath;
|
||||
String? singerName;
|
||||
String? uploadUserName;
|
||||
bool? likeOrNot;
|
||||
bool? collectOrNot;
|
||||
|
||||
MusicItem.fromMap(Map map) {
|
||||
id = map['id'];
|
||||
name = map['name'];
|
||||
coverPath = map['coverPath'];
|
||||
musicPath = map['musicPath'];
|
||||
singerName = map['singerName'];
|
||||
uploadUserName = map['uploadUserName'];
|
||||
likeOrNot = map['likeOrNot'];
|
||||
collectOrNot = map['collectOrNot'];
|
||||
}
|
||||
}
|
@ -1,329 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:music_player_miao/api/api_music_return.dart';
|
||||
import 'package:music_player_miao/common_widget/app_data.dart';
|
||||
import 'package:music_player_miao/models/getComment_bean.dart';
|
||||
import 'package:music_player_miao/models/universal_bean.dart';
|
||||
import 'package:music_player_miao/widget/text_field.dart';
|
||||
|
||||
|
||||
class CommentView extends StatefulWidget {
|
||||
|
||||
|
||||
@override
|
||||
_CommentViewState createState() => _CommentViewState();
|
||||
|
||||
CommentView({super.key, required this.initialSongIndex});
|
||||
|
||||
late final int initialSongIndex;
|
||||
}
|
||||
|
||||
class _CommentViewState extends State<CommentView> {
|
||||
List comments = [];
|
||||
|
||||
TextEditingController commentController = TextEditingController();
|
||||
FocusNode commentFocusNode = FocusNode();
|
||||
List commentTimes = [];
|
||||
List commentHeader = [];
|
||||
List commentName = [];
|
||||
bool ascendingOrder = true;
|
||||
int playlistCount = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_fetchSonglistData();
|
||||
}
|
||||
|
||||
Future<void> _fetchSonglistData() async {
|
||||
try {
|
||||
GetCommentBean bean1 = await getCommentApi().getComment(
|
||||
musicId: '1',
|
||||
pageNo: '0',
|
||||
pageSize: '10',
|
||||
Authorization: AppData().currentToken,
|
||||
);
|
||||
|
||||
// 检查 rows 是否为空
|
||||
if (bean1.rows == null) {
|
||||
print('No comments found');
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
comments = bean1.rows!.map((rows) => rows.content ?? 'No content').toList();
|
||||
commentTimes = bean1.rows!.map((rows) => rows.time ?? 'Unknown time').toList();
|
||||
commentHeader = bean1.rows!.map((rows) => rows.avatar ?? 'Default avatar').toList();
|
||||
commentName = bean1.rows!.map((rows) => rows.username ?? 'Anonymous').toList();
|
||||
playlistCount = comments.length;
|
||||
});
|
||||
} catch (error) {
|
||||
print('Error fetching songlist data: $error');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
// backgroundColor: Colors.transparent,
|
||||
appBar: AppBar(
|
||||
centerTitle: true,
|
||||
backgroundColor: const Color(0xffF6FFD1),
|
||||
title: const Text(
|
||||
'评论(200)',
|
||||
style: TextStyle(
|
||||
color: Colors.black,
|
||||
fontSize: 22
|
||||
),
|
||||
),
|
||||
elevation: 0,
|
||||
leading: IconButton(
|
||||
onPressed: () {
|
||||
Get.back();
|
||||
},
|
||||
icon: Image.asset(
|
||||
"assets/img/back.png",
|
||||
width: 25,
|
||||
height: 25,
|
||||
fit: BoxFit.contain,
|
||||
),
|
||||
),
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
Container(
|
||||
height: 80,
|
||||
padding: const EdgeInsets.only(left: 20, right: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xffF9F2AF),
|
||||
borderRadius: BorderRadius.circular(20)
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: Image.asset(
|
||||
"assets/img/artist_pic.png",
|
||||
width: 60,
|
||||
height: 60,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 20,),
|
||||
const Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
"背对背拥抱",
|
||||
maxLines: 1,
|
||||
style: TextStyle(
|
||||
color: Colors.black,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w400),
|
||||
),
|
||||
Text(
|
||||
"林俊杰",
|
||||
maxLines: 1,
|
||||
style: TextStyle(color: Colors.black, fontSize: 14),
|
||||
)
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
IconButton(
|
||||
onPressed: () {},
|
||||
icon: Image.asset(
|
||||
"assets/img/music_pause.png",
|
||||
width: 25,
|
||||
height: 25,
|
||||
fit: BoxFit.contain,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(10.0),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text(
|
||||
"评论区",
|
||||
style: TextStyle(
|
||||
fontSize: 18
|
||||
),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
const Text(
|
||||
"时间",
|
||||
style: TextStyle(
|
||||
fontSize: 16
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
ascendingOrder = !ascendingOrder;
|
||||
|
||||
// 使用时间排序索引列表
|
||||
List<int> sortedIndexes = List<int>.generate(comments.length, (i) => i);
|
||||
sortedIndexes.sort((a, b) {
|
||||
DateTime timeA = DateTime.parse(commentTimes[a]);
|
||||
DateTime timeB = DateTime.parse(commentTimes[b]);
|
||||
return ascendingOrder ? timeA.compareTo(timeB) : timeB.compareTo(timeA);
|
||||
});
|
||||
|
||||
comments = [for (var i in sortedIndexes) comments[i]];
|
||||
commentTimes = [for (var i in sortedIndexes) commentTimes[i]];
|
||||
commentHeader = [for (var i in sortedIndexes) commentHeader[i]];
|
||||
commentName = [for (var i in sortedIndexes) commentName[i]];
|
||||
});
|
||||
},
|
||||
icon: Image.asset(
|
||||
ascendingOrder
|
||||
? "assets/img/commend_up.png"
|
||||
: "assets/img/commend_down.png",
|
||||
fit: BoxFit.contain,
|
||||
),
|
||||
),
|
||||
|
||||
],
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
/// 显示评论的区域
|
||||
Expanded(
|
||||
child:
|
||||
ListView.builder(
|
||||
itemCount: comments.length,
|
||||
itemBuilder: (context, index) {
|
||||
return ListTile(
|
||||
title: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
CircleAvatar(
|
||||
backgroundImage: NetworkImage(commentHeader[index])
|
||||
),
|
||||
const SizedBox(width: 10,),
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(commentName[index],
|
||||
style: const TextStyle(fontSize: 18)),
|
||||
const SizedBox(width: 8),
|
||||
// Adjust the spacing between elements
|
||||
Text(commentTimes[index],
|
||||
style: const TextStyle(fontSize: 14),),
|
||||
// Add the timestamp
|
||||
],
|
||||
),
|
||||
],
|
||||
), // Ad
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 50, top: 10, bottom: 20),
|
||||
child: Text(
|
||||
comments[index],
|
||||
style: const TextStyle(
|
||||
fontSize: 18), // Customize the font size if needed
|
||||
),
|
||||
),
|
||||
Container(
|
||||
width: 560,
|
||||
height: 2,
|
||||
color: const Color(0xffE3F0ED),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
///输入框和提交按钮
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextFieldColor(
|
||||
controller: commentController,
|
||||
hintText: '来发表你的评论吧!',
|
||||
)
|
||||
),
|
||||
const SizedBox(width: 8.0),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
submitComment();
|
||||
UniversalBean bean = await commentMusic().comment(
|
||||
musicId: widget.initialSongIndex,
|
||||
content: commentController.text,
|
||||
Authorization: AppData().currentToken);
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: const Color(0xff429482),
|
||||
// Change Colors.blue to your desired background color
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(
|
||||
10), // Adjust the radius as needed
|
||||
),
|
||||
minimumSize: const Size(30, 44),
|
||||
),
|
||||
child: const Text(
|
||||
'提交',
|
||||
style: TextStyle(
|
||||
fontSize: 16
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void submitComment() async {
|
||||
String comment = commentController.text.trim();
|
||||
if (comment.isEmpty) {
|
||||
print('Comment cannot be empty');
|
||||
Get.snackbar('错误', '评论不能为空');
|
||||
return;
|
||||
}
|
||||
|
||||
print('Submitting comment with content: $comment');
|
||||
|
||||
try {
|
||||
UniversalBean bean = await commentMusic().comment(
|
||||
musicId: widget.initialSongIndex,
|
||||
content: comment,
|
||||
Authorization: AppData().currentToken,
|
||||
);
|
||||
|
||||
// 处理响应结果
|
||||
if (bean.code == 200) {
|
||||
print('Comment submitted successfully');
|
||||
commentController.clear();
|
||||
_fetchSonglistData(); // 刷新评论列表
|
||||
} else {
|
||||
print('Failed to submit comment: ${bean.msg}');
|
||||
Get.snackbar('错误', bean.msg ?? '评论提交失败');
|
||||
}
|
||||
} catch (error) {
|
||||
print('Error submitting comment: $error');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue