Jos*_*ane 11 iphone audio objective-c
我已经将音频工具箱和avfoundation导入到我的班级并将框架添加到我的项目中并使用此代码播放声音:
- (void)playSound:(NSString *)name withExtension:(NSString *)extension
{
NSURL* soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:name ofType:extension]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
audioPlayer.delegate = self;
audioPlayer.volume = 1.0;
[audioPlayer play];
}
Run Code Online (Sandbox Code Playgroud)
我称之为:
[self playSound:@"wrong" withExtension:@"wav"];
Run Code Online (Sandbox Code Playgroud)
零声音.非常感谢任何帮助,谢谢.
ach*_*chi 25
更新的答案:
我有这个问题.它与ARC有关,与prepareForPlay无关.只需对它进行强有力的引用即可.
如下用户所述:Kinderchocolate:)
在代码中:
.h
~
@property (nonatomic, strong) AVAudioPlayer *player;
.m
~
@implementation
@synthesize _player = player;
Run Code Online (Sandbox Code Playgroud)
h3r*_*r3b 18
是的,ARC也是我的问题.我解决了只是添加:
在.h
@property (strong, nonatomic) AVAudioPlayer *player;
Run Code Online (Sandbox Code Playgroud)
在.m
@synthesize player;
-(void)startMusic{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Riddls" ofType:@"m4a"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //infinite
[player play];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8714 次 |
| 最近记录: |