播放audioiOS Objective-C

我试图让我的iOS应用程序中播放audio文件。 这是我现在的代码

NSString *soundFilePath = [NSString stringWithFormat:@"%@/test.m4a", [[NSBundle mainBundle] resourcePath]]; NSLog(@"%@",soundFilePath); NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [audioPlayer setVolume:1.0]; audioPlayer.delegate = self; [audioPlayer stop]; [audioPlayer setCurrentTime:0]; [audioPlayer play]; 

我正在检查一堆其他职位,但他们通常都做同样的事情。 我没有收到错误,但是我听不到在模拟器或设备上播放的任何audio。

这是我在模拟器上获得的audio文件的path

 /Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/long-numbered-thing/BookAdventure.app/test.m4a 

任何帮助表示赞赏!

也许你应该尝试使用NSBundle方法pathForResource:ofType:方法来创build文件的path。

下面的代码应该可以成功播放一个audio文件。 我只用它与mp3 ,但我想它应该也适用于m4a 。 如果此代码不起作用,您可以尝试更改audio文件的格式。 对于此代码,audio文件位于主项目目录中。

 /* Use this code to play an audio file */ NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"m4a"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil]; player.numberOfLoops = -1; //Infinite [player play]; 

编辑

好吧,试试这个:

 NSString *soundFilePath = [NSString stringWithFormat:@"%@/test.m4a",[[NSBundle mainBundle] resourcePath]]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil]; player.numberOfLoops = -1; //Infinite [player play]; 

另外,确保你做适当的import:

 #import <AudioToolbox/AudioToolbox.h> #import <AVFoundation/AVFoundation.h> 

您需要存储对“AVAudioPlayer”的引用

 @property (strong) AVAudioPlayer *audioPlayer; 

如果你确定你的audio文件在下面的Bundle Resources中,它应该播放。

项目>构build阶段>复制

播放扩展名为.caf,.m4a,.mp4,.mp3,.wav,.aif的文件


从GitHub下载以下两个文件

 SoundManager.h SoundManager.m 

将这些文件添加到您的项目

还要将audio文件添加到资源文件夹( 示例文件 )

 mysound.mp3, song.mp3 

导入所需文件中的头文件

 #import "SoundManager.h" 

– (void)viewDidLoad中添加以下两行

 [SoundManager sharedManager].allowsBackgroundMusic = YES; [[SoundManager sharedManager] prepareToPlay]; 

使用下面的行播放声音(mysound.mp3)

 [[SoundManager sharedManager] playSound:@"mysound" looping:NO]; 

使用下面的行来停止声音(mysound.mp3)

 [[SoundManager sharedManager] stopSound:@"mysound"]; 

你也可以播放音乐(song.mp3)

 [[SoundManager sharedManager] playMusic:@"song" looping:YES]; 

并可以阻止音乐

 [[SoundManager sharedManager] stopMusic]; 

完整的文档在GitHub上

首先将这个框架导入到你的文件中

 #import <AVFoundation/AVFoundation.h> 

然后声明一个AVAudioPlayer的实例。

 AVAudioPlayer *audioPlayer; NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Name of your audio file" ofType:@"type of your audio file example: mp3"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil]; audioPlayer.numberOfLoops = -1; [audioPlayer play]; 

如果您生成系统声音ID,请使用以下代码播放捆绑audio文件

 for(int i = 0 ;i< 5;i++) { SystemSoundID soundFileObject; NSString *audioFileName = [NSString stringWithFormat:@"Alarm%d",i+1]; NSURL *tapSound = [[NSBundle mainBundle] URLForResource: audioFileName withExtension: @"caf"]; // Store the URL as a CFURLRef instance CFURLRef soundFileURLRef = (__bridge CFURLRef) tapSound; // Create a system sound object representing the sound file. AudioServicesCreateSystemSoundID ( soundFileURLRef, &soundFileObject ); [alarmToneIdList addObject:[NSNumber numberWithUnsignedLong:soundFileObject]]; } 

您可以使用下面的代码播放声音

 AudioServicesPlaySystemSound([[alarmToneIdList objectAtIndex:row]unsignedLongValue]); 

为了实现这个框架,需要在你的Xcode中添加

AudioToolbox

最后下面的头文件需要在控制器中导入

 #import AudioToolbox/AudioToolbox.h #import AudioToolbox/AudioServices.h