Flutter provider paketi ile cihaz belleğinden MP3 verisi almak

flutter provider paketi ile cihaz belleğinden mp3 verisi almak

提问人:yusuf 提问时间:11/17/2023 更新时间:11/17/2023 访问量:14

问:

我从设备内存中获取 mp3 数据,但每次运行构建方法时数据都会一遍又一遍地出现。 我希望所有mp3文件只被获取一次,如果有更新的数据,它会反映在屏幕上,我该怎么做?

目前,代码处于此状态时,我收到此错误:

   E/MediaMetadataRetrieverJNI(21416): getEmbeddedPicture: Call to getEmbeddedPicture failed.
   I/art     (21416): Background partial concurrent mark sweep GC freed 3403(394KB) 
   AllocSpace objects, 98(50MB) LOS objects, 9% free, 38MB/42MB, paused 2.154ms total 
   105.565ms
   14
   E/MediaMetadataRetrieverJNI(21416): getEmbeddedPicture: Call to getEmbeddedPicture failed.
   W/libEGL  (21416): [ANDROID_RECORDABLE] format: 1
   I/Choreographer(21416): Skipped 65 frames!  The application may be doing too much work on 
   its main thread.
   I/flutter (21416): 333
   13
   E/MediaMetadataRetrieverJNI(21416): getEmbeddedPicture: Call to getEmbeddedPicture failed.
   Lost connection to device.
   Exited (sigterm)

import 'package:flutter/material.dart';
import 'package:music_app/theme/theme.dart';
import 'package:flutter/foundation.dart';
import 'package:on_audio_query/on_audio_query.dart';

class ThemeProvider extends ChangeNotifier{

  ThemeMode themeMode = ThemeMode.system;
  bool get isDarkMode => themeMode == ThemeMode.dark;
  void temaDegis(bool acik){
     themeMode = acik ? ThemeMode.dark : ThemeMode.light;
    notifyListeners();

  }



 late OnAudioQuery audioQuery;
 List<SongModel> musicList = [];

 Future<void> fetchMusic() async {
   audioQuery = OnAudioQuery();
   musicList = await audioQuery.querySongs(
     sortType: null,
     orderType: OrderType.ASC_OR_SMALLER,
     uriType: UriType.EXTERNAL,
     ignoreCase: true,
   );
   notifyListeners();
    } 

 }

 HomePage

  @override
   Widget build(BuildContext context) {

     final musicProvider = Provider.of<ThemeProvider>(context, listen: false);
     musicProvider.fetchMusic();
   
    

     final controller = PageController(viewportFraction: 0.8, keepPage: true);
       return  Scaffold(
        body:  ListView(
           children: [
              SizedBox(height: 2.h,),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text("En son", style: TextStyles.plyListTextStyle(context)),
                SizedBox(width: 2.w,),
                Icon(Icons.auto_awesome_motion_rounded)
              ],
            ),
            SizedBox(height: 2.h,), 
            SizedBox(
              height: 33.h,
              child: PageView(    
                
                controller: controller,
                scrollDirection: Axis.horizontal
                ,children: [
                  MusicCardWidget( image: 'images/',musicName: ''),  
                  MusicCardWidget( image: 'images/', musicName: '',) ,
                  MusicCardWidget( image: 'images/', musicName: '',)   
                ],
              )
              ),
              //SizedBox(height: 5.h,),
              MusicListCardWidget(musicList: musicProvider.musicList),
          ],
        ),
    
    
  );

}


  class _MusicListCardWidgetState extends State<MusicListCardWidget> {
     @override
    Widget build(BuildContext context) {
      return  SizedBox(
        width: double.infinity,
        child: ListView.builder(
        itemCount: widget.musicList.length,
        shrinkWrap: true,
        primary: false,
       padding: EdgeInsets.only(left: 6.w),
       itemBuilder: (context, index) {
         return ListTile(
          title: Text(widget.musicList[index].displayName.toString(), overflow: 
          TextOverflow.ellipsis,
          maxLines: 1,
           style: TextStyles.musicListCardTextStyle(context),),
          subtitle: Text(widget.musicList[index].artist.toString(), maxLines: 1, overflow: 
          TextOverflow.ellipsis, style: TextStyle(fontSize: 11),), 
          leading: ClipRRect(
            borderRadius : BorderRadius.circular(5)
            ,child: QueryArtworkWidget(id: widget.musicList[index].id, 
            type: ArtworkType.AUDIO,artworkFit: BoxFit.cover, 
            
            size: 20,
            )
            ),
          trailing: 
              Column(
                  children: [
                    IconButton(icon: Icon( Icons.play_circle_outline_outlined, size: 
                 30,color:_colorMethod(index),), onPressed: () {
                    },),
                   // Text('04.52'),
                  ],
                ),                 
        );
      },
   ),
  );
 }
  Color _colorMethod(int sayi){
    if(sayi %2==0){
    return Colors.red.shade300;
   }
   else{
    return Colors.deepPurple;
  }
 }
}
扑动 供应商 状态管理

评论


答: 暂无答案