2009-08-18 78 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]; 
} 

一旦你开始你的程序,它会播放声音。你可以通过改变音量,或者什么时候播放,停止,以适应你的应用...

相关问题