[英]cocoa playing an mp3
有沒有一種簡單的方法可以從可可加載、播放和控制 mp3 文件? 嘗試使用谷歌搜索它,但是,就像蘋果一樣,我得到的結果很混亂,不知道從哪里開始。 據我所知,有和 NSSound,但它有很多限制,然后有 CoreAudio,但它非常困難。 那么有人可以指出我正確的方向嗎? 謝謝。
正在進行中的復活:
為什么要復活這個問題? 因為谷歌搜索“可可播放 mp3”將我帶到這里,並且沒有很棒的 2012 答案。 所以,這是 2012 年很棒的答案;)
使用 AVFoundation! 據蘋果稱,它已與 Mac OS X Lion 集成(我認為),所以.. 以下是如何輕松播放 mp3:
1- 鏈接 AVFoundation 框架。
2- 將其導入到任何您想播放精彩 mp3 的地方
#import <AVFoundation/AVFoundation.h>
3- 添加一個 audioPlayer 實例變量來播放聲音(至少我喜歡這樣做)
@interface WCMainWindow : ... {
...
AVAudioPlayer* audioPlayer;
}
4- 在初始化時,確保初始化音頻播放器:
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- 播放您的精彩 Mp3!
if ([audioPlayer isPlaying]) {
[audioPlayer pause];
} else {
[audioPlayer play];
}
最后,功勞歸於這個人。
我用 C++ 編寫了一個可能有幫助的框架: http : //github.com/sbooth/SFBAudioEngine
它支持多種音頻格式,並具有相當良性的 API 並帶有 Cocoa 示例。
如果您對第三方框架不感興趣,您可能會選擇使用 AudioQueue 來處理播放。 為此,您可能會使用 AudioFile 來解碼 MP3 和 AudioQueue 以進行播放。 Apple 在http://developer.apple.com/mac/library/samplecode/AudioQueueTools/Introduction/Intro.html有一個例子
#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
使用NSSound 。
您沒有具體說明“很多限制”是什么意思,所以我不知道為什么這對您不起作用。 請注意,自 Leopard 以來,它的限制要少得多; 例如,您現在可以在任何設備上播放。
除了NSSound
,你可以考慮QTMovieView
。 這聽起來很奇怪,但是您可以在窗口中隱藏QTMovieView
並用它來播放 MP3。
補充: QTMovieView
從 OS 10.9 開始被棄用。 因此,除非您需要支持 10.7 之前的操作系統版本(當 AVFoundation 出現在 Mac 上時),您可能不應該使用它。
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.
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.