AVAudioPlayer不能正常播放声音如何解决?

在项目中不能正常播放声音,在网上搜了下,主要遇到两个问题:

1.AVAudioPlayer,没有强引用,被释放掉了。

参考此处:http://stackoverflow.com/questions/19032254/avaudioplayer-not-working-on-ios7

@property (strong, nonatomic)  AVAudioPlayer * newAudio;
如上,声明为强引用,如果在一个方法里直接使用AVAudioPlayer,方法结束后,就可能没有足够的时间来播放声音。

2.声音文件名称中有空格,如"By The Seaside.m4r",就不能正常的播放。

参考此处:http://stackoverflow.com/questions/12550773/avaudioplayer-cannot-play-from-url

    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"m4r"];
    NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSError *error;
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    self.player.numberOfLoops = -1;
    [self.player prepareToPlay];
    [self.player play];

你可能感兴趣的:(iOS,调试)