Is there an easy way to load, play and control an mp3 file from cocoa? Tried googling it, but, as all things apple, i get messy results and have no idea where to start. As i understand, there's and NSSound, but it has lots of limitations and then there's CoreAudio, but it's very difficult. So can someone point me in a right direction with this? Thanks.
-
Sorry, are you developing for iOS or Mac? – shosti Jul 18 '10 at 18:01
-
1Yes, sorry, forgot to clarify: AVAudioPlayer won't work: i'm developing for OSX – Marius Jul 18 '10 at 18:23
-
1As of Mac OS X 10.7 Lion (possibly even 10.6.8?) AVFoundation works! Hence, AVAudioPlayer.. See answer below.. – Mazyod Apr 12 '12 at 14:10
6 Answers
Use AVFoundation! According to apple, it has been integrated with Mac OS X Lion (I think), so.. Here is how to play mp3 painlessly:
1- link the AVFoundation Framework.
2- import it wherever you want to play your awesome mp3
#import <AVFoundation/AVFoundation.h>
3- Add an audioPlayer instance variable to play the sound (That's how I like to do it, at least)
@interface WCMainWindow : ... {
...
AVAudioPlayer* audioPlayer;
}
4- In you init, make sure you initialize your 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];
5- Play your awesome Mp3!!
if ([audioPlayer isPlaying]) {
[audioPlayer pause];
} else {
[audioPlayer play];
}
Finally, credit goes to this guy.

- 22,319
- 10
- 92
- 157
-
-
What do you mean? Go through the steps explained, and make sure you add an MP3 file to your resources. Then, add a button (action) that will call `[audioPlayer play]` – Mazyod Jul 09 '12 at 19:27
-
2The method URLWithString always returned nil for me. Using `[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"mp3"]];` instead fixed the problem. – gebirgsbärbel Jun 14 '13 at 11:58
I've written a framework in C++ that might help: http://github.com/sbooth/SFBAudioEngine
It supports multiple audio formats and has a fairly benign API and comes with a Cocoa sample.
If you're not interested in third-party frameworks, your bet would probably be to use an AudioQueue to take care of the playback. To do this, you'd probably use AudioFile to decode the MP3 and AudioQueue for the playback. Apple has an example at http://developer.apple.com/mac/library/samplecode/AudioQueueTools/Introduction/Intro.html

- 16,646
- 2
- 55
- 81
#import <Cocoa/Cocoa.h>
#import <QTKit/QTKit.h>
@interface SoundPlayer : NSObject {
NSSound *sound;
IBOutlet NSWindow *window;
IBOutlet NSSlider *progress;
BOOL loop;
}
- (IBAction)open: (id)sender;
- (IBAction)setLoop: (id)sender;
- (IBAction)play: (id)sender;
- (IBAction)stop: (id)sender;
- (IBAction)takeCurrentTimeFrom: (id)sender;
@end
#import "SoundPlayer.h"
@implementation SoundPlayer
- (IBAction)open: (id)sender
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel runModalForTypes: [NSArray arrayWithObjects: @"aiff", @"mp3", @"m4a", nil]];
[sound release];
sound = [[QTMovie movieWithFile: [openPanel filename]
error: NULL] retain];
[sound setAttribute: [NSNumber numberWithBool: loop]
forKey: QTMovieLoopsAttribute];
[window setTitle: [[openPanel filename] lastPathComponent]];
}
- (IBAction)setLoop: (id)sender
{
loop = ([sender state] == NSOnState);
[sound setAttribute: [NSNumber numberWithBool: loop]
forKey: QTMovieLoopsAttribute];
}
- (IBAction)play: (id)sender
{
NSTimeInterval duration;
QTGetTimeInterval([sound duration], &duration);
[progress setMaxValue: duration];
[NSTimer scheduledTimerWithTimeInterval: 0.1
target: self
selector: @selector(updateIndicator:)
userInfo: sound
repeats: YES];
[sound play];
}
- (void)updateIndicator: (NSTimer*)aTimer
{
QTMovie *playingSound = [aTimer userInfo];
if (!(sound == playingSound && ([sound rate] != 0)))
{
[aTimer invalidate];
return;
}
NSTimeInterval currentTime;
QTGetTimeInterval([sound currentTime], ¤tTime);
[progress setDoubleValue: currentTime];
}
- (IBAction)stop: (id)sender
{
[sound stop];
}
- (IBAction)takeCurrentTimeFrom: (id)sender
{
[sound setCurrentTime: QTMakeTimeWithTimeInterval([sender doubleValue])];
}
@end

- 7,266
- 4
- 35
- 36
Use NSSound.
You didn't specify what you mean by “lots of limitations”, so I don't know why this won't work for you. Note that it has a lot fewer limitations since Leopard; you can now play to any device, for example.

- 95,783
- 15
- 211
- 370
Besides NSSound
, you could consider QTMovieView
. It sounds odd, but you can have a hidden QTMovieView
in your window and use it to play an MP3.
Added: QTMovieView
is deprecated as of OS 10.9. So unless you need to support OS versions before 10.7 (when AVFoundation came to the Mac), you should probably not use this.

- 22,385
- 6
- 55
- 76
-
I've also seen examples with QTKit without any views, but i'm not sure if this is a good way to do it, since i haven't found how to seek a file or maybe even add some effects later. – Marius Jul 18 '10 at 18:54
-
-
Well, in simpler terms, go to some specific position and continue playing from there. – Marius Jul 18 '10 at 19:54
-
Oh, OK... when using QTMovieView, you can use QTMovie methods such as `setCurrentTime:` on the associated QTMovie. – JWWalker Jul 18 '10 at 21:30
-
Thanks. Would it be possible to add some audio effects to QTMovie later? Like reverb, equalizer etc.? Or is it only possible for CoreAudio? – Marius Jul 19 '10 at 11:01
-
I can't swear that it can't be done, but I don't recall seeing anything about using audio effects on a QTMovie. – JWWalker Jul 19 '10 at 16:17
-
@JWWalker, I know that you wrote your answer back in 2010, but now, in 2016, I believe tht QTMoviewView is deprecated. – Kaydell Aug 29 '16 at 17:54
import Cocoa
import AVFoundation
class ViewController: NSViewController {
var audio = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
let play = Bundle.main.path(forResource: "A" , ofType: "mb3")
do{
audio = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: play!))
}
catch{
print(error)
}
}
@IBAction func button(_ sender: Any)
{
audio.play()
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}

- 1,015
- 8
- 26