How to play a music file using URL link in iPhone?

后端 未结 5 732
无人共我
无人共我 2020-12-13 05:12

I want to play a music file using URL Link in the iPhone. But when I use the below code I am getting error I am not getting where I am going wrong. Can anyone Correct me?

5条回答
  •  粉色の甜心
    2020-12-13 05:42

    Try the following:

    -(void)viewDidLoad 
    
    {
    
    [super viewDidLoad];
    NSString* resourcePath = @"your url"; //your url
    
    NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
    NSError *error;
    
    
    audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
    audioPlayer.numberOfLoops = 1;
    
    if (audioPlayer == nil)
        NSLog([error description]);             
    
    else 
        [audioPlayer play];
    

提交回复
热议问题