AVAudioPlayer with external URL to *.m4p

后端 未结 3 424
失恋的感觉
失恋的感觉 2020-12-03 10:52

My Problem is the following. I got this code and i guess a corrupt NSURL since the AVAudioPlayer is nil after initializing:

NSString *dummyURLString = @\"htt         


        
相关标签:
3条回答
  • 2020-12-03 11:24

    Instead of using the AVAudioPlayer you can use the AVPlayer. The AVPlayer works as well with remote URLs

    0 讨论(0)
  • 2020-12-03 11:30

    I tried this first but got error 2003334207:

    NSData *soundData = [NSData dataWithContentsOfURL:URL];
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:soundData error:&error];
    

    Seems that AVAudioPlayer really wants a file. So I put the data into a file first:

    NSURL *url = [NSURL URLWithString:@"http://a825.phobos.apple.com/us/r2000/005/Music/d8/a8/d2/mzi.jelhjoev.aac.p.m4p"]; 
    NSData *soundData = [NSData dataWithContentsOfURL:url];
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                            NSUserDomainMask, YES) objectAtIndex:0] 
                            stringByAppendingPathComponent:@"sound.caf"];
    [soundData writeToFile:filePath atomically:YES];
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
                    fileURLWithPath:filePath] error:NULL];  
    NSLog(@"error %@", error);
    
    0 讨论(0)
  • 2020-12-03 11:40

    AVAudioPlayer only works with local URL's. It must be a File URL (file://)

    See Apple's Technical Q&A QA1634

    0 讨论(0)
提交回复
热议问题