0

I've followed every resource that I can find, and I don't know why this isn't working. I'm trying to play it on the simulator. Also, I have verified that my urlString is accurate and that it plays when pasted into a browser. What am I missing?

.h file

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

@interface MyViewController : UIViewController <AVAudioPlayerDelegate>

@end

.m file

@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];


}
7
  • What does a log of url show?
    – rdelmar
    Aug 14, 2014 at 22:30
  • Don't you need a "www" and/or an "http://" at the beginning of that? Browsers automatically add that for you, I don't think that's true for initWithContentsOfURL:.
    – rdelmar
    Aug 14, 2014 at 22:38
  • Also, I think this is probably a typo, but there should be an @end after your property declaration in the class extension.
    – rdelmar
    Aug 14, 2014 at 22:39
  • Yes. it's a typo. I've edited the post.
    – AndroidDev
    Aug 14, 2014 at 22:41
  • That's a SO formatting thing. They took my text (which included www) and displayed it as a hyperlink without that part. Not sure why or how to correct it, but that full URL is in the string.
    – AndroidDev
    Aug 14, 2014 at 22:42

0

Browse other questions tagged or ask your own question.