ios编程之媒体播放功能
2013-08-23 19:40
218 查看
下面这段代码实现了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];//四秒内声音渐弱
,这段代码是获取要播放的音频文件的地址.[[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];//四秒内声音渐弱
相关文章推荐
- GStreamer播放媒体,实现Video Mute功能
- 媒体播放互不干涉 让XP用户羡慕的Vista功能
- win7/8/xp系统关闭自动播放功能禁止音频媒体自动播放
- 使用HTML5中的Blob对象实现媒体播放功能
- 一个多种功能于一身的专业媒体播放软件。
- 使用HTML5中的Blob对象实现媒体播放功能
- 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能
- Android学习:媒体播放功能
- HTML5 Blob对象实现媒体播放功能
- 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能
- <Win32_17>集音频和视频播放功能于一身的简易播放器
- 苹果平台上的媒体流播放技术HLS
- 集成Vitamio,实现媒体播放(附电视播放Demo)
- 灯鹭推出“社会化评论功能”,评论可同步到8大社会化媒体
- Android调用手机系统自带录音功能实现语音录制与播放
- C#中windows程序使用播放功能
- 如何关闭CD自动播放功能
- Java实现视频网站的视频上传、视频转码、视频关键帧抽图, 及视频播放功能
- QML带进度条的视频播放功能
- qt5.2音乐播放器的播放功能