swift 。获取 CMAudioFormatDescription 时的 EXC_BREAKPOINT

标签 swift avfoundation core-media

我正在尝试从音频文件中检索采样率和 channel 数。 我在此行遇到运行时“EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT)”错误:

let audioFormstDesc = descriptions[0] as CMAudioFormatDescription

这是我使用的最简单的代码版本:

import AVFoundation
import CoreMedia
import MediaPlayer

class ViewController: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()

        let query = MPMediaQuery.songsQuery()
        let song = query.items[0] as MPMediaItem
        let url = song.valueForProperty(MPMediaItemPropertyAssetURL) as NSURL
        let songAsset = AVURLAsset.URLAssetWithURL(url, options: nil)
        let trackAsset = songAsset.tracks[0] as AVAssetTrack
        if let descriptions = trackAsset.formatDescriptions
        {
            let audioFormstDesc = descriptions[0] as CMAudioFormatDescription
            let streamBasicDescription = CMAudioFormatDescriptionGetStreamBasicDescription(audioFormstDesc)
        }
    }
}

在 Objective-C 中,这段代码工作得很好:

MPMediaQuery *query = [MPMediaQuery songsQuery];
MPMediaItem *song = [[query items] objectAtIndex:0];
NSURL *url = [song valueForProperty: MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVAssetTrack *trackAsset = songAsset.tracks[0];

UInt32 sampleRate = 0, channelCount = 0;
NSArray* descriptions = trackAsset.formatDescriptions;
CMAudioFormatDescriptionRef audioFormstDesc = (__bridge CMAudioFormatDescriptionRef)descriptions[0];
const AudioStreamBasicDescription* audioStreamDesc = CMAudioFormatDescriptionGetStreamBasicDescription(audioFormstDesc);
if(audioStreamDesc)
{
    sampleRate = audioStreamDesc->mSampleRate;
    channelCount = audioStreamDesc->mChannelsPerFrame;
}

println(descriptions[0]) 打印到控制台:

<CMAudioFormatDescription 0x15d3b690 [0x387e5ad0]>

如何处理该错误?

最佳答案

在 Swift 和 xcode 6 beta 中,如果您使用“swift 优化级别”-O(最快)进行编译,有时会出现这种错误。尝试在“build设置”中将优化级别设置为 -Onone。

关于 swift 。获取 CMAudioFormatDescription 时的 EXC_BREAKPOINT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25219306/

相关文章:

ios - 在 Swift 2.0 中创建 CMSampleBuffer 的副本

swift - NSFetchRequest - 实体 - 解包可选值时意外发现 nil

ios - Swift - 捕获闭包 - Apple 的快速编程指南片段

ios - UITableView 中的多个自定义单元格与 Firebase 数据库

iphone - 如何使用 AVFoundation 为逐帧生成的视频设置方向?

ios - 如何在 Swift 中的相机层顶部添加文本框?

objective-c - CMSampleBufferGetImageBuffer CVImageBufferGetEncodedSize 制作问题

macos - MacOS 虚拟网络摄像头本质上是否与 10.14 的强化运行时库验证不兼容?

ios - 为什么 NSKeyedArchiver 和 NSKeyedUnarchiver 在这里没有按预期工作?

ios - 尝试从 PHAsset 获取 AVAssetExportSession 的输出时得到空结果