音樂播放
NSString *path = [[NSBundle mainBundle] pathForResource:@"預謀" ofType:@"mp3"];
if (path) {
NSURL *url = [NSURL fileURLWithPath:path];
player= [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
[player setDelegate:self];
player.volume = 1;
player.pan = 1;
player.numberOfLoops = -1;
if ([player prepareToPlay]) {
NSLog(@"prepareToPlay:%@",path);
[player play];
} else {
NSLog(@"no prepareToPlay");
}
//[player release];
}
一定要注意player的引用,如果直接在函數內部申請一個臨時變量,那麼這個player會在函數執行完成,執行release 會被釋放掉,如果需要在函數內部使用,那麼請sleep下,不過這樣會堵塞主線程的運作
視頻播放器
NSURL * movieurl = [NSURL URLWithString:@"http://v.youku.com/player/getRealM3U8/vid/XNDUwNjc4MzA4/type/video.m3u8"];
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:movieurl];
[self presentMoviePlayerViewControllerAnimated:player];