可可在播放mp3

Mar*_*ius 13 macos cocoa mp3 objective-c playback

有没有一种从cocoa加载,播放和控制mp3文件的简单方法?尝试谷歌搜索它,但是,因为所有东西苹果,我得到凌乱的结果,不知道从哪里开始.据我所知,有和NSSound,但它有很多限制,然后有CoreAudio,但它是非常困难的.那么有人能指出我正确的方向吗?谢谢.

Maz*_*yod 40

进展中的复活:

为什么要复活这个问题?因为谷歌搜索"可可玩MP3"带我到这里,并没有真棒的2012年答案.所以,这是真棒2012年答案;)

使用AVFoundation!根据苹果的说法,它已经与Mac OS X Lion集成(我认为),所以..这里是如何轻松地播放mp3:

1-链接AVFoundation框架.

2-将它导入任何你想要播放你真棒mp3的地方

#import <AVFoundation/AVFoundation.h>
Run Code Online (Sandbox Code Playgroud)

3-添加一个audioPlayer实例变量来播放声音(至少我喜欢这样做)

@interface WCMainWindow : ... {
    ...
    AVAudioPlayer* audioPlayer;
}
Run Code Online (Sandbox Code Playgroud)

4-在初始化中,确保初始化audioPlayer:

NSString* path = [[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"mp3"];
NSURL* file = [NSURL fileURLWithPath:path];
// thanks @gebirgsbaerbel

audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[audioPlayer prepareToPlay];
Run Code Online (Sandbox Code Playgroud)

5-玩你真棒的Mp3 !!

if ([audioPlayer isPlaying]) {
    [audioPlayer pause];
} else {
    [audioPlayer play];
}
Run Code Online (Sandbox Code Playgroud)

最后,归功于这个家伙.

  • 方法URLWithString总是为我返回nil.使用`[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myFile"ofType:@"mp3"]];`而不是修复问题. (2认同)

sbo*_*oth 5

我用C++编写了一个可能有用的框架:http://github.com/sbooth/SFBAudioEngine

它支持多种音频格式,并且具有相当良好的API,并附带Cocoa示例.

如果您对第三方框架不感兴趣,那么您的赌注可能是使用AudioQueue来处理播放.为此,您可能需要使用AudioFile解码MP3和AudioQueue以进行播放.Apple在http://developer.apple.com/mac/library/samplecode/AudioQueueTools/Introduction/Intro.html上有一个例子