使用 Objective C 在设备上播放声音
Playing a sound in on device with Objective C
我已经在此处查看了很多关于此问题的答案,但无法解决任何问题。
这是我的问题,我试图在按下按钮时播放声音,这里是文件所在的位置
这是我的代码
- (void)playSoundWithOfThisFile:(NSString*)fileName {
NSString *soundFilePath = [NSString stringWithFormat:fileName,
[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
player.numberOfLoops = 1;
[player play];
}
当我运行通过代码得到的值如下
fileName = Mummy.mp3
`soundFilePath = Mummy.mp3` (I've had it Sounds/Mummy.mp3 and this doesn't work either)
soundFileURL = Mummy.mp3
-- 文件:///
player = nil
如有任何帮助,我们将不胜感激。如果我发现它的错误只是一个没有信息的一般错误
试试这个。
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Mummy" ofType:@"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:filePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
player.numberOfLoops = 1;
[player play];
上的文档
尝试使用下面的代码
SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle]
pathForResource:@"test" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)
[NSURL fileURLWithPath:soundFile], & soundID);
AudioServicesPlaySystemSound(soundID);
别忘了导入
#import <AudioToolbox/AudioToolbox.h>
NSString *path = [[NSBundle mainBundle] pathForResource: @"how_to_play" ofType: @"mp3"];
if(!self.theAudio)
{
self.theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.theAudio.delegate = self;
[self.theAudio play];
}
输入viewcontroller.h
< AVAudioPlayerDelegate>
@property(nonatomic,strong) AVAudioPlayer *theAudio;
我已经在此处查看了很多关于此问题的答案,但无法解决任何问题。
这是我的问题,我试图在按下按钮时播放声音,这里是文件所在的位置
这是我的代码
- (void)playSoundWithOfThisFile:(NSString*)fileName {
NSString *soundFilePath = [NSString stringWithFormat:fileName,
[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
player.numberOfLoops = 1;
[player play];
}
当我运行通过代码得到的值如下
fileName = Mummy.mp3
`soundFilePath = Mummy.mp3` (I've had it Sounds/Mummy.mp3 and this doesn't work either)
soundFileURL = Mummy.mp3
-- 文件:///
player = nil
如有任何帮助,我们将不胜感激。如果我发现它的错误只是一个没有信息的一般错误
试试这个。
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Mummy" ofType:@"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:filePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
player.numberOfLoops = 1;
[player play];
上的文档
尝试使用下面的代码
SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle]
pathForResource:@"test" ofType:@"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)
[NSURL fileURLWithPath:soundFile], & soundID);
AudioServicesPlaySystemSound(soundID);
别忘了导入
#import <AudioToolbox/AudioToolbox.h>
NSString *path = [[NSBundle mainBundle] pathForResource: @"how_to_play" ofType: @"mp3"];
if(!self.theAudio)
{
self.theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.theAudio.delegate = self;
[self.theAudio play];
}
输入viewcontroller.h
< AVAudioPlayerDelegate>
@property(nonatomic,strong) AVAudioPlayer *theAudio;