我尝试播放来自服务器 url 的音频,但没有播放,但我尝试播放来自文档目录路径 url 的音频,它播放正常。
NSData *songData=[NSData dataWithContentsOfURL:[NSURL URLWithString:
[NSString stringWithFormat:@"%@",aSongURL]]];
AVAudioPlayer *abc = [[AVAudioPlayer alloc] initWithData:songData error:nil];
abc.numberOfLoops=0;
[abc prepareToPlay];
[abc play];
你可以
试试这个。希望对您有所帮助。
AVPlayer *objAVPlayer = [[AVPlayer alloc] initWithURL:url];
[objAVPlayer play];
我的答案如下
ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController
@property (nonatomic,strong)AVAudioPlayer *player;
- (IBAction)actionPlayAudio:(id)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize player;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)actionPlayAudio:(id)sender {
NSString *strAudioFileURL = @"dev.epixelsoft.co/love_app/audio/img_14957068361.caf";
NSURL *soundFileURL = [NSURL fileURLWithPath:strAudioFileURL];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
[player play];
}