iOS 调用音乐播放以及视频播放器

时间:2021-08-24 05:16:55

音乐播放

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 =
;

player.pan =
;

;

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];