下面这段代码实现了ios媒体播放的功能. [[NSBundle mainBundle]pathForResource:@"wdgsl" ofType:@"mp3"]] ,这段代码是获取要播放的音频文件的地址.[[NSBundle mainBundle] infoDictionary]是获取app的详细信息。
//播放声音,
-(void)startPlayer{
//定义URl
NSURL *audioPath = [[NSURLalloc]initFileURLWithPath:[[NSBundlemainBundle]pathForResource:@"wdgsl"ofType:@"mp3"]];
NSError *error =nil;
//播放器初始化操作
audioPlayer = [[AVAudioPlayeralloc]initWithContentsOfURL:audioPatherror:&error];
//设置代理
audioPlayer.delegate =self;
//判断是否错误,如果错误应该直接return;
if (error!=nil) {
NSLog(@"播放遇到错误了信息:%@",[error description]);
return ;
}
//开始播放
[audioPlayer play];
}
//播放完成
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
NSLog(@"播放完成!!");
}
//播放解码错误
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error{
NSLog(@"解码错误!!");
播放音效
-----------------------------------------
#import <AudioToolbox/AudioToolbox.h>
//播放声音
-(void)startPlayer{
//定义URl
NSURL *audioPath = [[NSURLalloc] initFileURLWithPath:[[NSBundlemainBundle] pathForResource:@"xp"ofType:@"wav"]];
//定义SystemSoundID
SystemSoundID soundId;
//C语言的方法调用
//注册服务
AudioServicesCreateSystemSoundID((CFURLRef)audioPath, &soundId);
//增添回调方法
AudioServicesAddSystemSoundCompletion(soundId,NULL, NULL, PlayFinishCallBack,NULL);
//开始播放
AudioServicesPlayAlertSound(soundId);
}
//声音播放完成后的回调SystemSoundID sound_id, void* user_data
void PlayFinishCallBack(){
NSLog(@"播放完成!");
}
//添加震动
-(void)startZhen{
NSLog(@"测试震动!");
//实现震动
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
}
--------------------------------------------
视频播放
#import <MediaPlayer/MediaPlayer.h>
1。添加MediaPlayer.framework
2.//定义播放器的类
//开始播放视频
-(void)startPlayer{
//获取视频文件路径
NSString *filePath = [[NSBundlemainBundle] pathForResource:@"mtv"ofType:@"3gp"];
//加载URL
NSURL *videoUrl = [NSURLfileURLWithPath:filePath];
//初始化播放器并且设定播放模式
videoPlayer = [[MPMoviePlayerViewControlleralloc] initWithContentURL:videoUrl];
//控制模式
//videoPlayer.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
//设定播放模式
videoPlayer.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
//开始播放
[[[UIApplicationsharedApplication] keyWindow]addSubview:videoPlayer.view];
}
- (void)dealloc
{
//移除通知
[[NSNotificationCenterdefaultCenter] removeObserver:self];
//释放资源
[videoPlayer release];
[superdealloc];
}
- (void)viewDidLoad
{
[superviewDidLoad];
//初始化通知
//为什么要加通知?
/*
第一个参数:通知对象
第二个参数:通知方法
第三个参数:通知发生的时间 MPMoviePlayerPlaybackDidFinishNotification播放完成通知
第四个参数:上下文参数
*/
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(playerDone)name:MPMoviePlayerPlaybackDidFinishNotificationobject:nil];
}
//视频播放完成的通知方法
-(void)playerDone{
NSLog(@"视频播放完成!");
[videoPlayer.viewremoveFromSuperview];
[videoPlayer release];
videoPlayer = nil;
}
MCSoundBoard实现了循环播放背景音乐时,还可以同时播放其他声音。背景音乐关掉时,声音是慢慢隐去直至消失,而不是突然关掉声音。
//开启背景音乐
引入框架 AudioToolbox.framework AVFoundation.framework
[MCSoundBoard addAudioAtPath:[[NSBundle mainBundle] pathForResource:@"BGM08.mp3" ofType:nil] forKey:@"BGM08"];
AVAudioPlayer *player = [MCSoundBoard audioPlayerForKey:@"BGM08"];
player.numberOfLoops = -1; // Endless
[MCSoundBoard playAudioForKey:@"BGM08" fadeInInterval:4.0];//四秒内声音渐强
//暂停背景音乐
[MCSoundBoard pauseAudioForKey:@"BGM08" fadeOutInterval:4.0];//四秒内声音渐弱