迅速。关于获取CMAudioFormatDescription的EXC_BREAKPOINT

时间:2014-08-09 13:56:40

标签: swift avfoundation core-media

我试图从音频文件中检索采样率和通道数。 我已经进入运行状态" EXC_BREAKPOINT(代码= 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]>

如何处理该错误?

1 个答案:

答案 0 :(得分:-1)

在Swift和xcode 6 beta中,如果使用&#34; swift优化级别&#34;编译,有时会出现这种错误。 -O(最快)。尝试将优化级别设置为-Onone in&#34; Build Settings&#34;。