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];
}
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];
我通过在设置网址后立即添加以下行来解决此问题:
NSData *data = [NSData dataWithContentsOfURL:mp3URL];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.