如何在ios上播放音频文件

如何在ios上播放音频文件,ios,xcode,avfoundation,avaudioplayer,Ios,Xcode,Avfoundation,Avaudioplayer,我试图播放音频文件,但我可以让它工作。 我导入了AVFoundation框架 代码如下: NSString *fileName = [[NSBundle mainBundle] pathForResource:@"Alarm" ofType:@"caf"]; NSURL *url = [[NSURL alloc] initFileURLWithPath:fileName]; NSLog(@"Test: %@ ", url); AVAudioPlayer *audioFi

我试图播放音频文件,但我可以让它工作。 我导入了
AVFoundation
框架

代码如下:

NSString *fileName = [[NSBundle mainBundle] pathForResource:@"Alarm" ofType:@"caf"];
    NSURL *url = [[NSURL alloc] initFileURLWithPath:fileName];
    NSLog(@"Test: %@ ", url);

    AVAudioPlayer *audioFile = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
    audioFile.delegate = self;
    audioFile.volume = 1;

    [audioFile play];
我收到一个错误
nil字符串参数

我将文件复制到支持文件文件夹中,因此文件就在那里

你们能帮帮我吗


谢谢

当文件位于特定文件夹中时,我会使用类似的方式:

// Build URL to the sound file we are going to play
NSURL *sound_file = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:sound_file_name ofType:@"mp3" inDirectory:directory]];   

// Play it
audio_player = [[AVAudioPlayer alloc] initWithContentsOfURL:sound_file error:nil];
audio_player.delegate = self;
[audio_player play];
[sound_file release];
sound\u file\u name
是不带扩展名的文件名:
@“success”


目录
类似于:
@“媒体/声音效果”

添加此文件,然后查看程序是否识别该文件存在

NSLog(@“文件存在:%d”,[[NSFileManager defaultManager]fileExistsAtPath:fileName])


如果存在,它将打印1。如果没有,请确保文件名正确(大小写很重要)。

只需执行两个简单步骤即可

步骤1:

UIViewController.h

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

@property(nonatomic, retain) AVAudioPlayer *player;

只需确保AVAudioPlayer属性必须是非原子的并保留即可。

确保导入audiotoolbox框架,然后编写以下内容。这是我用来播放简单声音的

#import <AudioToolbox/AudioToolbox.h>

 CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Your sound here", CFSTR ("mp3"), NULL);

UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
#导入
CFBundleRef mainBundle=CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef=CbundlecopyresourceUrl(mainBundle,(CFStringRef)@“您的声音在这里”,CFSTR(“mp3”),NULL);
UInt32声音ID;
AudioServicesCreateSystemSoundID(soundFileURLRef和soundID);
AudioServicesPlaySystemSound(soundID);

如果您对此有任何疑问,请告诉我

确保在Xcode中选择该文件时,设置了其“目标成员资格”部分(在属性窗口最左侧的选项卡中),之后我没有收到错误,但声音不会播放。给我的道路是'file://localhost/var/mobile/Applications/FAA680BA-162E-4E58-A22B-3E2F139A4368/RemindMe.app/Alarm.caf我敢打赌,它之所以没有发挥作用,是因为它超出了范围,在有机会之前就被解约了。让它成为一个实例变量,这样它就会一直存在。答案是。。。你是对的:)我创建了一个属性,现在它正在工作。你知道为什么会发生这种情况吗?@Camus请标记一个答案作为解决方案。尝试
NSError*error;AVAudioPlayer*音频文件=[[AVAudioPlayer alloc]initWithContentsOfURL:url错误:&error];NSLog(@“错误%@,[错误描述])
#import <AudioToolbox/AudioToolbox.h>

 CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Your sound here", CFSTR ("mp3"), NULL);

UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);