如何在ios中从服务器URL播放.caf音频文件

时间:2017-05-25 11:47:11

标签: ios objective-c caf

我尝试从服务器网址播放音频但没有播放,但我尝试播放文档目录路径网址中的音频并且播放正常。

NSData *songData=[NSData dataWithContentsOfURL:[NSURL URLWithString:
[NSString stringWithFormat:@"%@",aSongURL]]];
AVAudioPlayer *abc = [[AVAudioPlayer alloc] initWithData:songData error:nil];
abc.numberOfLoops=0;
[abc prepareToPlay];
[abc play];

2 个答案:

答案 0 :(得分:1)

你可以试试这个。希望它有所帮助。

AVPlayer *objAVPlayer = [[AVPlayer alloc] initWithURL:url];
[objAVPlayer play];

答案 1 :(得分:0)

我的回答在下面

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