开发者

Download a song and play it with AVFoundation

开发者 https://www.devze.com 2023-03-25 09:37 出处:网络
I\'ve been trying to get this to work all day. I\'ve tried everything I could think of to get this to work. I have a song which is an m4a located on a server. I download it and save it to the document

I've been trying to get this to work all day. I've tried everything I could think of to get this to work. I have a song which is an m4a located on a server. I download it and save it to the documents directory and then play it with AVAudioPlayer. When I tap the UIButton to play the song, it doesn't do anything. I am logging the NSError but its returning a (null). Here is my code.

        NSError *error;
        NSURL *url = [开发者_JS百科NSURL URLWithString:@"http://a2.mzstatic.com/us/r30/Music/43/49/2e/mzi.ittnbeif.aac.p.m4a"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documents = [paths objectAtIndex:0];
        NSString *file = [NSString stringWithFormat:@"%@/in-my-life.m4a", documents];

        [data writeToFile:file atomically:YES];


        AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:file] error:&error];
        [player setVolume:1.0];
        [player setDelegate:self];
        [player setMeteringEnabled:YES];
        [player play];

        NSLog(@"Play Song: %@", file);
        NSLog(@"Error: %@", error);

This is what the log shows.

2011-08-01 17:34:09.659 APP[1275:707] Play Song: /var/mobile/Applications/B1F04C1F-1541-4EDB-B898-264061292EDB/Documents/in-my-life.m4a
2011-08-01 17:34:09.661 APP[1275:707] Error: (null)

Any ideas?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号