AVPlayer可以使用.m4a的文件扩展名工作,但不能使用.aac扩展名,使用AVAssetExportSession时也是如此。
创始人
2024-11-13 03:00:11
0

AVPlayer可以使用.m4a的文件扩展名工作,但不能使用.aac扩展名,使用AVAssetExportSession时也是如此。下面是一种解决方法的代码示例:

import UIKit
import AVFoundation

class ViewController: UIViewController {

    var player: AVPlayer?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 播放.m4a文件
        guard let m4aFilePath = Bundle.main.path(forResource: "audio", ofType: "m4a") else {
            return
        }
        
        let m4aFileURL = URL(fileURLWithPath: m4aFilePath)
        let m4aAsset = AVAsset(url: m4aFileURL)
        let m4aPlayerItem = AVPlayerItem(asset: m4aAsset)
        
        player = AVPlayer(playerItem: m4aPlayerItem)
        player?.play()
        
        
        // 将.aac文件转换为.m4a文件
        guard let aacFilePath = Bundle.main.path(forResource: "audio", ofType: "aac") else {
            return
        }
        
        let aacFileURL = URL(fileURLWithPath: aacFilePath)
        let aacAsset = AVAsset(url: aacFileURL)
        
        let exportSession = AVAssetExportSession(asset: aacAsset, presetName: AVAssetExportPresetAppleM4A)
        exportSession?.outputFileType = .m4a
        
        guard let m4aOutputFilePath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("output.m4a") else {
            return
        }
        
        exportSession?.outputURL = m4aOutputFilePath
        
        exportSession?.exportAsynchronously(completionHandler: {
            if exportSession?.status == .completed {
                // 转换成功,可以使用AVPlayer播放.m4a文件
                let m4aAsset = AVAsset(url: m4aOutputFilePath)
                let m4aPlayerItem = AVPlayerItem(asset: m4aAsset)
                
                DispatchQueue.main.async {
                    self.player = AVPlayer(playerItem: m4aPlayerItem)
                    self.player?.play()
                }
            } else {
                // 转换失败
                print("转换失败:\(exportSession?.error?.localizedDescription ?? "")")
            }
        })
    }

}

上述代码首先使用AVPlayer播放名为"audio.m4a"的文件。然后,它将名为"audio.aac"的文件转换为.m4a格式,并将输出文件保存在应用程序的文档目录中。转换完成后,它使用AVPlayer播放转换后的.m4a文件。

请注意,上述代码中的文件名和路径是示例,你需要根据你的实际文件名和路径进行调整。

相关内容

热门资讯

科普分享“Wepoker透视免... 您好:Wepoker透视免费版的软件,软件加客服确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
每日必备“WePoKer辅助挂... 无需打开直接搜索;操作使用教程:每日必备“WePoKer辅助挂怎么下载”开挂辅助脚本(透视)科技教程...
科普分享“wePoKer有透视... 科普分享“wePoKer有透视软件吗”开挂辅助挂(透视)安装教程是一款可以让一直输的玩家,快速成为一...
最新技巧“wepoker是不是... 最新技巧“wepoker是不是有透视软件”开挂辅助平台(透视)曝光教程是一款可以让一直输的玩家,快速...
重要通知“有wepoker透视... 无需打开直接搜索加微信客服(136704302)咨询了解有wepoker透视吗辅助器目前(微信:13...
科普常识“wepoker透视辅... 科普常识“wepoker透视辅助挂的功能介绍”开挂辅助安装(透视)靠谱教程是一款可以让一直输的玩家,...
玩家必用“wepoker辅助软... 玩家必用“wepoker辅助软件安全吗”开挂辅助插件(透视)实用技巧是一款可以让一直输的玩家,快速成...
我来教教你“微乐扑克记牌器怎么... 您好:微乐扑克记牌器怎么用这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户...
玩家必知教程“wepoker冤... 您好:wepoker冤家牌规律这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
开挂辅助软件"wep... 开挂辅助软件"wepoker透视脚本免费下载pc"开挂(透视)辅助脚本(有挂分析)【无需打开直接搜索...