0

When I tap on an image view, I am trying to play music. When I run the following code, the music doesn't play. The music is an .m4a:

.h

#import <AVFoundation/AVFoundation.h>
@interface ChooseViewController : UIViewController {
    AVAudioPlayer *audioPlayer;
}
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

.m

- (void)imageTaped:(UIGestureRecognizer *)gestureRecognizer {
    // 
    self.songURL = [NSURL URLWithString:@"http://a1745.phobos.apple.com/us/r1000/011/Music6/v4/91/d0/45/91d0451d-c2cd-911a-491e-4f51c02e6b38/mzaf_4642375274280516284.plus.aac.p.m4a"];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.songURL
                                                                       error:nil];
    [audioPlayer play];
}
smecperson
  • 301
  • 1
  • 4
  • 15
  • possible duplicate of [AVAudioPlayer not playing any sound](http://stackoverflow.com/questions/8016765/avaudioplayer-not-playing-any-sound) – Yuyutsu May 02 '15 at 04:30
  • As you can see by my code, I tried that solution, but it didn't work. – smecperson May 02 '15 at 04:31

2 Answers2

0

you Try this code

NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *path = [NSString stringWithFormat:@"%@/sound.mp3", resourcePath];

NSURL *url = [NSURL fileURLWithPath:path];
NSError *error = nil;

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url
                                                                    error:&error];
[audioPlayer play];
Vijay yadav
  • 1,220
  • 8
  • 17
0

I fixed it by adding this line right after setting the URL:

NSData *data = [NSData dataWithContentsOfURL:mp3URL];
smecperson
  • 301
  • 1
  • 4
  • 15