2009-08-18 82 views

回答

1
+2

鏈接#1是作弊 - 我實際上並不需要一個界面,只是一個聲音。鏈接#2在Mac上播放,而不是iPhone。 – AngryHacker 2009-08-18 22:07:15

+0

嗨AngryHacker:是的,我認爲第一個鏈接不是你要去的 - 但是我把它包括在內以防你實際上在尋找那個。至於第二個鏈接,那裏有一個到「AudioStreamer」項目的鏈接,其中包括iPhone和Mac版本的源代碼。 – PF1 2009-08-18 22:14:38

2

在終端中使用afconvert將您的MP3轉換爲.caf。男人afconvert會告訴你更多。

然後

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: name ofType: @"caf"]; 

NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; 

AVAudioPlayer *player = [super initWithContentsOfURL: fileURL error: nil]; 
[fileURL release]; 
[player play]; 
+0

太好了。在項目中的哪個位置放置.caf文件?進入資源文件夾? – AngryHacker 2009-08-19 16:02:48

+0

是的。資源文件夾是好的。如果您有多個.cafs文件並且想將它們保留在自己的文件夾中,您可以擁有子文件夾。 – mahboudz 2009-08-20 04:53:28

+2

還有一件事要知道的是,如果你想毫不拖延地播放聲音,你可以調用[player prepareToPlay]來預載它。然而,調用[玩家停止]可能會卸載它,迫使玩家重新加載它。 - 暫停,停止播放,並且不卸載。 (您可以手動設置播放器在暫停後從頭開始播放) – mahboudz 2009-08-20 05:02:02

15

下面是我在做什麼在Xcode 4.2播放MP3:

1)進口AVFoundation.framework框架到您的項目

2)補充說:#import "AVFoundation/AVAudioPlayer.h"到項目ViewController.h文件:

#import <UIKit/UIKit.h> 
#import "AVFoundation/AVAudioPlayer.h" 

@interface ViewController : UIViewController 

@end 

3)拖放你的MP3文件yoursoundfile.mp3到根項目瀏覽器

4)ViewController.m修改-(void)viewDidLoad

- (void)viewDidLoad{ 
    [super viewDidLoad]; 
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
             pathForResource:@"yoursoundfile" 
             ofType:@"mp3"]]; 
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] 
            initWithContentsOfURL:url 
            error:nil]; 
    [audioPlayer play]; 
} 

一旦你開始你的程序,它會播放聲音。你可以通過改變音量,或者什麼時候播放,停止,以適應你的應用...

相關問題