2 mapmapm MapMapM 于 2013.03.20 11:00 提问

服务器的音频文件不播放

从服务器中直接播放音频文件时它不播放,如果先播放本地文件再播放服务器的就可以。

    //NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];

NSURL *url = [NSURL URLWithString:@"http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp3"];


NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;

if (audioPlayer == nil)
    NSLog([error description]);             
else 
    [audioPlayer play];

2个回答

love_loveec
love_loveec   2013.03.20 13:45
已采纳
AVPlayer * player = [AVPlayer playerWithURL:[NSURL URLWithString:@"YOUR URL"]];
[player play];

这样看看

xinyoulingxi1_tong
xinyoulingxi1_tong   2013.03.20 13:44

这段代码应该可以帮你

NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp3]];
NSError *error;

audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];

if (audioPlayer == nil)
    NSLog([error description]); 
else
    [audioPlayer play];
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!