iOS开发之多媒体API(2)

视频格式可以分为适合本地播放的本地影像视频和适合在网络中播放的网络流媒体影像视频两大类。尽管后者在播放的稳定性和播放画面质量上可能没有前者 优秀,但网络流媒体影像视频的广泛传播性使之正被广泛应用于视频点播、网络演示、远程教育、网络视频广告等等互联网信息服务领域。

AD:

实例

wps_clip_image-14723

SystemSoundServices

添加AudioToolbox.framework框架

SystemSoundServicesViewController.h文件

 
   
  1. #import <UIKit/UIKit.h> 
  2. #include <AudioToolbox/AudioToolbox.h> 
  3.  
  4. @interface SystemSoundServicesViewController : UIViewController; 
  5.  
  6. - (IBAction) playSystemSound; 
  7. - (IBAction) vibrate; 
  8.  
  9. @end 

播放事件

   
  1. - (IBAction) playSystemSound{ 
  2.  
  3.     NSURL* system_sound_url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"BeepGMC500" ofType:@"wav"]]; 
  4.     SystemSoundID system_sound_id; 
  5.  
  6.     AudioServicesCreateSystemSoundID( 
  7.         (CFURLRef)system_sound_url, 
  8.         &system_sound_id 
  9.     ); 
  10.      
  11.     // Register the sound completion callback. 
  12.     AudioServicesAddSystemSoundCompletion( 
  13.         system_sound_id, 
  14.         NULL, // uses the main run loop 
  15.         NULL, // uses kCFRunLoopDefaultMode 
  16.         MySoundFinishedPlayingCallback, // the name of our custom callback function 
  17.         NULL // for user data, but we don't need to do that in this case, so we just pass NULL 
  18.     ); 
  19.  
  20.     // Play the System Sound 
  21.     AudioServicesPlaySystemSound(system_sound_id); 

AudioServicesAddSystemSoundCompletion方法5个参数,第一参数SystemSoundID,第二参数是是否使用循环,第三个参数是循环模式,第四个参数是回调函数,就是当播放完成时候回调的方法,第五个参数是为回调函数提供参数。

这里回调的方法是C语言风格的函数:MySoundFinishedPlayingCallback。

回调函数

   
  1. void MySoundFinishedPlayingCallback(SystemSoundID sound_id, void* user_data){ 
  2.     AudioServicesDisposeSystemSoundID(sound_id); 

震动方法调用

   
  1. // Vibrate on action 
  2. - (IBAction) vibrate{ 
  3.     AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 

12.3 播放和录制音频

AVFoundation控件可以实现一般音频播放和录制。

AVAudioPlayer音频播放类,用于播放大于5秒钟声音,可以播放本地声音,但是不能播放网络媒体文件。能够播放、 暂停、循环和跳过等操作。

AVAudioRecorder音频录制类。

实例AVAudioPlayer

wps_clip_image-30534

添加AVFoundation.framework框架

AvplayerViewController.h文件

 
   
  1. #import <UIKit/UIKit.h> 
  2. #import <AVFoundation/AVFoundation.h> 
  3.  
  4. @interface AvplayerViewController : UIViewController <AVAudioPlayerDelegate> { 
  5.     AVAudioPlayer * player; 
  6.  
  7.  
  8. - (IBAction) stopSong: (id) sender; 
  9. - (IBAction) playSong: (id) sender; 
  10.  
  11.  
  12. @end 

AvplayerViewController.m

   
  1. #import "AvplayerViewController.h" 
  2.  
  3. @implementation AvplayerViewController 
  4.  
  5. - (IBAction) playSong: (id) sender { 
  6.     NSError *error = nil; 
  7.     player = [[AVAudioPlayer alloc] initWithContentsOfURL:  
  8.                  [NSURL fileURLWithPath:[[NSBundle mainBundle]  
  9.                     pathForResource:@"charleston1925_64kb" ofType:@"mp3"]] error:&error]; 
  10.     player.delegate = self; 
  11.     if(error) { 
  12.         NSLog(@"%@",[error description]);  
  13.         [error release]; 
  14.     } 
  15.     [player play]; 
  16.  
  17. - (IBAction) stopSong: (id) sender { 
  18.     [player stop]; 
  19.  
  20. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { 
  21.     NSLog(@"播放完成。");     
  22.  
  23. - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error { 
  24.     NSLog(@"播放错误发生: %@", [error localizedDescription]);     
  25.  
  26.  
  27. - (void)dealloc { 
  28.     [player release]; 
  29.     [super dealloc]; 
  30.  
  31. @end 

AVAudioPlayer委托

AVAudioPlayerDelegate委托对象提供了两个主要方法:

audioPlayerDidFinishPlaying:successfully:

audioPlayerDecodeErrorDidOccur:error:

AVAudioRecorder

新建实例:Recorder

wps_clip_image-7998

RecorderViewController.h文件

 
   
  1. #import <UIKit/UIKit.h> 
  2. #import <AVFoundation/AVFoundation.h> 
  3.  
  4. @interface RecorderViewController : UIViewController 
  5.     AVAudioRecorder *recorder; 
  6.     AVAudioPlayer *player; 
  7.     UILabel *label; 
  8. @property (retain, nonatomic) AVAudioRecorder * recorder; 
  9. @property (retain, nonatomic) AVAudioPlayer * player; 
  10. @property (retain, nonatomic) IBOutlet UILabel *label; 
  11. -(IBAction)recordPushed:(id)sender; 
  12. -(IBAction)playPushed:(id)sender; 
  13. -(IBAction)stopPushed:(id)sender; 
  14. @end 

音频录制方法

 
   
  1. -(IBAction)recordPushed:(id)sender  
  2.     label.text = @"recode..."
  3.     if([recorder isRecording]) 
  4.         return
  5.      
  6.     if([player isPlaying]) 
  7.         [player stop]; 
  8.     NSError *error = nil; 
  9.     [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord 
  10.                                            error:&error]; 
  11.     [[AVAudioSession sharedInstance] setActive:YES error:&error]; 

AVAudioSession 是iOS提供音频会话类,音频会话是指定应用程序与音频系统如何交互。AVAudioSession 通过指定一个音频类别(Category)实现的,音频类别(Category)描述了应用程序使用音频的方式。下面是语句是设定音频会话类别:

[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryRecord error:&error];

AVAudioSessionCategoryRecord代表只能输入音频,即录制音频了。其效果是停止其它音频播放。

使用类别后,音频会话要设置为“活跃的”Active,这会把后台的任何系统声音关闭。

[[AVAudioSession sharedInstance]  setActive:YES error:&error];   

音频录制方法

 
   
  1. NSMutableDictionary *settings = [NSMutableDictionary dictionary]; 
  2.     [settings setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM]  
  3.                 forKey:AVFormatIDKey]; 
  4.     [settings setValue:[NSNumber numberWithFloat:44100.0]  
  5.                 forKey:AVSampleRateKey]; //采样率 
  6.     [settings setValue:[NSNumber numberWithInt:1]  
  7.                 forKey:AVNumberOfChannelsKey];//通道的数目 
  8.     [settings setValue:[NSNumber numberWithInt:16]  
  9.                 forKey:AVLinearPCMBitDepthKey];//采样位数  默认 16 
  10.     [settings setValue:[NSNumber numberWithBool:NO]  
  11.                 forKey:AVLinearPCMIsBigEndianKey];//大端还是小端 是内存的组织方式 
  12.     [settings setValue:[NSNumber numberWithBool:NO]  
  13.                 forKey:AVLinearPCMIsFloatKey];//采样信号是整数还是浮点数 
  14.      
  15.     NSString *filePath =  
  16.     [NSString stringWithFormat:@"%@/rec_audio.caf", [self documentsDirectory]]; 
  17.     NSURL *fileUrl = [NSURL fileURLWithPath:filePath]; 
  18.      
  19.     //[self setRecorder:nil]; 
  20.     recorder = [[AVAudioRecorder alloc]  
  21.                                     initWithURL:fileUrl  
  22.                                     settings:settings  
  23.                                     error:&error]; 
  24.      
  25.    // [recorder setMeteringEnabled:YES]; 
  26.     [recorder record]; 
-(NSString *)documentsDirectory{     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,                                                           NSUserDomainMask, YES);      return [paths objectAtIndex:0]; }

音频播放方法

   
  1. -(IBAction)playPushed:(id)sender{ 
  2.     label.text = @"play..."
  3.      
  4.     if([recorder isRecording]) 
  5.         [recorder stop]; 
  6.     if([player isPlaying]) 
  7.         [player stop]; 
  8.      
  9.     NSString *filePath =  
  10.     [NSString stringWithFormat:@"%@/rec_audio.caf", [self documentsDirectory]]; 
  11.     NSURL *fileUrl = [NSURL fileURLWithPath:filePath]; 
  12.     NSError *error = nil; 
  13.    // [self setPlayer:nil]; 
  14.     [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback 
  15.                                            error:&error]; 
  16.     [[AVAudioSession sharedInstance] setActive:YES error:&error]; 
  17.     player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:&error]; 
  18.    // [player setMeteringEnabled:YES]; 
  19.     [player play]; 

音频停止方法

   
  1. -(IBAction)stopPushed:(id)sender{ 
  2.     label.text = @"stop..."
  3.     if([recorder isRecording]) 
  4.         [recorder stop]; 
  5.     if([player isPlaying]) 
  6.         [player stop]; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值