无法让AVAudioPlayer播放

时间:2014-08-14 21:23:38

标签: ios objective-c avaudioplayer

我已经跟踪了我能找到的每一种资源,而且我不知道为什么这不起作用。我试图在模拟器上播放它。此外,我已经验证我的urlString是准确的,并且它粘贴到浏览器时播放。我错过了什么?

.h文件

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

@interface MyViewController : UIViewController <AVAudioPlayerDelegate>

@end

.m文件

@interface SessionsListViewController ()
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
@end

- (void) viewDidLoad
{
    NSString *urlString = [NSString stringWithFormat:@"%@%@", AUDIO_URL, @"test.mp3"];
    NSURL *url = [NSURL URLWithString:urlString];
    NSError *err = nil;
    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];

    [self.audioPlayer prepareToPlay];
    [self.audioPlayer setDelegate:self];
     self.audioPlayer.volume = 1.0;
    [self.audioPlayer play];


}

1 个答案:

答案 0 :(得分:0)

我找到了这个问题的完整答案。看看这个question的接受答案: