Ver Fonte

1.修改获取音频时长的方法
2.解决 cell 复用机制的漏洞

100Years há 2 semanas atrás
pai
commit
8899527beb

+ 2 - 0
AIRingtone/Business/TSAIRintoneVC/TSAIRintoneVC/TSAIRintoneVC.swift

@@ -172,7 +172,9 @@ extension TSAIRintoneVC: UICollectionViewDataSource ,UICollectionViewDelegate,UI
 //            cell.setRingBtn.addTarget(self, action: #selector(clickSetRingBtn(_ :)), for: .touchUpInside)
 //            cell.setRingBtn.addTarget(self, action: #selector(clickSetRingBtn(_ :)), for: .touchUpInside)
             if let model = itemModel as? TSActionInfoModel {
             if let model = itemModel as? TSActionInfoModel {
                 cell.model = model
                 cell.model = model
+                cell.ringModel = nil
             }else if let ringModel = itemModel as? TSRingModel {
             }else if let ringModel = itemModel as? TSRingModel {
+                cell.model = nil
                 cell.ringModel = ringModel
                 cell.ringModel = ringModel
             }
             }
         }
         }

+ 2 - 0
AIRingtone/Business/TSAIRintoneVC/TSGenerateHistoryVC/TSGenerateHistoryVC.swift

@@ -121,7 +121,9 @@ extension TSGenerateHistoryVC: UICollectionViewDataSource ,UICollectionViewDeleg
 //            cell.setRingBtn.addTarget(self, action: #selector(clickSetRingBtn(_ :)), for: .touchUpInside)
 //            cell.setRingBtn.addTarget(self, action: #selector(clickSetRingBtn(_ :)), for: .touchUpInside)
             if let model = itemModel as? TSActionInfoModel {
             if let model = itemModel as? TSActionInfoModel {
                 cell.model = model
                 cell.model = model
+                cell.ringModel = nil
             }else if let ringModel = itemModel as? TSRingModel {
             }else if let ringModel = itemModel as? TSRingModel {
+                cell.model = nil
                 cell.ringModel = ringModel
                 cell.ringModel = ringModel
             }
             }
         }
         }

+ 2 - 0
AIRingtone/Business/TSDiscoverVC/TSDiscoverListVC/TSDiscoverListVC.swift

@@ -140,7 +140,9 @@ extension TSDiscoverListVC: UICollectionViewDataSource ,UICollectionViewDelegate
 //            cell.setRingBtn.addTarget(self, action: #selector(clickSetRingBtn(_ :)), for: .touchUpInside)
 //            cell.setRingBtn.addTarget(self, action: #selector(clickSetRingBtn(_ :)), for: .touchUpInside)
             if let model = itemModel as? TSActionInfoModel {
             if let model = itemModel as? TSActionInfoModel {
                 cell.model = model
                 cell.model = model
+                cell.ringModel = nil
             }else if let ringModel = itemModel as? TSRingModel {
             }else if let ringModel = itemModel as? TSRingModel {
+                cell.model = nil
                 cell.ringModel = ringModel
                 cell.ringModel = ringModel
             }
             }
         }
         }

+ 2 - 0
AIRingtone/Business/TSDiscoverVC/TSRingDownVC/TSRingDownVC.swift

@@ -127,7 +127,9 @@ extension TSRingDownVC: UICollectionViewDataSource ,UICollectionViewDelegate,UIC
 //            cell.setRingBtn.addTarget(self, action: #selector(clickSetRingBtn(_ :)), for: .touchUpInside)
 //            cell.setRingBtn.addTarget(self, action: #selector(clickSetRingBtn(_ :)), for: .touchUpInside)
             if let model = itemModel as? TSActionInfoModel {
             if let model = itemModel as? TSActionInfoModel {
                 cell.model = model
                 cell.model = model
+                cell.ringModel = nil
             }else if let ringModel = itemModel as? TSRingModel {
             }else if let ringModel = itemModel as? TSRingModel {
+                cell.model = nil
                 cell.ringModel = ringModel
                 cell.ringModel = ringModel
             }
             }
 
 

+ 31 - 3
AIRingtone/Common/Tool/TSAudioPlayer/TSBusinessAudioPlayer.swift

@@ -293,11 +293,39 @@ extension TSBusinessAudioPlayer{
         
         
         // 4. 获取音频时长
         // 4. 获取音频时长
         let duration: Double? = {
         let duration: Double? = {
-            let asset = AVURLAsset(url: url)
-            let seconds = Double(CMTimeGetSeconds(asset.duration))
-            return seconds.isNaN ? nil : seconds
+            
+            return getAudioDurationWithAudioFile(url: url)
+//            let asset = AVURLAsset(url: url)
+//            let seconds = Double(CMTimeGetSeconds(asset.duration))
+//            return seconds.isNaN ? nil : seconds
         }()
         }()
         
         
         return AudioFileInfo(sizeInBytes: fileSize, durationInSeconds: duration)
         return AudioFileInfo(sizeInBytes: fileSize, durationInSeconds: duration)
     }
     }
+    
+    /// 同步获取音频时长(可能阻塞线程!)
+    /// 使用 AudioFile 同步获取音频时长
+    static func getAudioDurationWithAudioFile(url: URL) -> TimeInterval? {
+        var audioFile: AudioFileID?
+        let status = AudioFileOpenURL(url as CFURL, .readPermission, 0, &audioFile)
+        
+        guard status == noErr, let file = audioFile else {
+            print("⚠️ 打开音频文件失败: \(status)")
+            return nil
+        }
+        
+        // 获取音频时长(单位:秒)
+        var duration: Float64 = 0
+        var propertySize = UInt32(MemoryLayout.size(ofValue: duration))
+        let durationStatus = AudioFileGetProperty(
+            file,
+            kAudioFilePropertyEstimatedDuration,
+            &propertySize,
+            &duration
+        )
+        
+        AudioFileClose(file)
+        
+        return durationStatus == noErr ? duration : nil
+    }
 }
 }