首页 > 代码库 > iOS 调用音乐播放以及视频播放器
iOS 调用音乐播放以及视频播放器
音乐播放
NSString *path = [[NSBundlemainBundle] pathForResource:@"预谋"ofType:@"mp3"];
if (path) {
NSURL *url = [NSURLfileURLWithPath:path];
player= [[AVAudioPlayeralloc]initWithContentsOfURL:urlerror:nil];
[playersetDelegate:self];
player.volume =1;
player.pan =1;
player.numberOfLoops = -1;
if ([playerprepareToPlay]) {
NSLog(@"prepareToPlay:%@",path);
[playerplay];
} else {
NSLog(@"no prepareToPlay");
}
//[player release];
}
一定要注意player的引用,如果直接在函数内部申请一个临时变量,那么这个player会在函数执行完成,执行release 会被释放掉,如果需要在函数内部使用,那么请sleep下,不过这样会堵塞主线程的运作
视频播放器
NSURL * movieurl = [NSURLURLWithString:@"http://v.youku.com/player/getRealM3U8/vid/XNDUwNjc4MzA4/type/video.m3u8"];
MPMoviePlayerViewController *player = [[MPMoviePlayerViewControlleralloc] initWithContentURL:movieurl];
[selfpresentMoviePlayerViewControllerAnimated:player];
iOS 调用音乐播放以及视频播放器