AVAudioPlayer:使用简单,但是只能播放本地音频文件,不可以播放流媒体文件
注意:加载本地音频文件的路径的时候,不可以 使用 NSURL *url = [NSURL URLWithString:musicPath];这句话是将网络的路径转换成url;加载本地需要用 NSURL *url = [NSURL fileURLWithPath:musicPath];
例如:
//_player是AVAudioPlayer对象,播放音视频文件,创建的播放器要放在全局,否则,不能播放 NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]; //转换网络链接 // NSURL *url = [NSURL URLWithString:musicPath]; //本地路径 NSURL *url = [NSURL fileURLWithPath:musicPath]; _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; _player.delegate = self; [_player prepareToPlay];
AVPlayer:既可以播放本地音频,也可以播放流媒体
//_player1是AVPlayer对象,可以播放流媒体音频 NSString *str = @"http://www.soge8.com/1424215157/e4eaa401acb097ad2745efe7f8213352.mp3"; _player1 = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:str]];
注册系统的声音:
//取得文件路径 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"44th Street Medium.caf" ofType:nil]; NSURL *url = [NSURL fileURLWithPath:filePath]; UInt32 soundID = 0; //注册系统声音 AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID); //播放系统声音 AudioServicesPlaySystemSound(soundID); //手机震动 //可以使用 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
时间: 2024-11-04 23:46:03