视频格式可以分为适合本地播放的本地影像视频和适合在网络中播放的网络流媒体影像视频两大类。尽管后者在播放的稳定性和播放画面质量上可能没有前者 优秀,但网络流媒体影像视频的广泛传播性使之正被广泛应用于视频点播、网络演示、远程教育、网络视频广告等等互联网信息服务领域。
AD:
实例
SystemSoundServices
添加AudioToolbox.framework框架
SystemSoundServicesViewController.h文件
- #import <UIKit/UIKit.h>
- #include <AudioToolbox/AudioToolbox.h>
- @interface SystemSoundServicesViewController : UIViewController;
- - (IBAction) playSystemSound;
- - (IBAction) vibrate;
- @end
播放事件
- - (IBAction) playSystemSound{
- NSURL* system_sound_url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"BeepGMC500" ofType:@"wav"]];
- SystemSoundID system_sound_id;
- AudioServicesCreateSystemSoundID(
- (CFURLRef)system_sound_url,
- &system_sound_id
- );
- // Register the sound completion callback.
- AudioServicesAddSystemSoundCompletion(
- system_sound_id,
- NULL, // uses the main run loop
- NULL, // uses kCFRunLoopDefaultMode
- MySoundFinishedPlayingCallback, // the name of our custom callback function
- NULL // for user data, but we don't need to do that in this case, so we just pass NULL
- );
- // Play the System Sound
- AudioServicesPlaySystemSound(system_sound_id);
- }
AudioServicesAddSystemSoundCompletion方法5个参数,第一参数SystemSoundID,第二参数是是否使用循环,第三个参数是循环模式,第四个参数是回调函数,就是当播放完成时候回调的方法,第五个参数是为回调函数提供参数。
这里回调的方法是C语言风格的函数:MySoundFinishedPlayingCallback。
回调函数
- void MySoundFinishedPlayingCallback(SystemSoundID sound_id, void* user_data){
- AudioServicesDisposeSystemSoundID(sound_id);
- }
震动方法调用
- // Vibrate on action
- - (IBAction) vibrate{
- AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
- }
12.3 播放和录制音频
AVFoundation控件可以实现一般音频播放和录制。
AVAudioPlayer音频播放类,用于播放大于5秒钟声音,可以播放本地声音,但是不能播放网络媒体文件。能够播放、 暂停、循环和跳过等操作。
AVAudioRecorder音频录制类。
实例AVAudioPlayer
添加AVFoundation.framework框架
AvplayerViewController.h文件
- #import <UIKit/UIKit.h>
- #import <AVFoundation/AVFoundation.h>
- @interface AvplayerViewController : UIViewController <AVAudioPlayerDelegate> {
- AVAudioPlayer * player;
- }
- - (IBAction) stopSong: (id) sender;
- - (IBAction) playSong: (id) sender;
- @end
AvplayerViewController.m
- #import "AvplayerViewController.h"
- @implementation AvplayerViewController
- - (IBAction) playSong: (id) sender {
- NSError *error = nil;
- player = [[AVAudioPlayer alloc] initWithContentsOfURL:
- [NSURL fileURLWithPath:[[NSBundle mainBundle]
- pathForResource:@"charleston1925_64kb" ofType:@"mp3"]] error:&error];
- player.delegate = self;
- if(error) {
- NSLog(@"%@",[error description]);
- [error release];
- }
- [player play];
- }
- - (IBAction) stopSong: (id) sender {
- [player stop];
- }
- - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
- NSLog(@"播放完成。");
- }
- - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error {
- NSLog(@"播放错误发生: %@", [error localizedDescription]);
- }
- - (void)dealloc {
- [player release];
- [super dealloc];
- }
- @end
AVAudioPlayer委托
AVAudioPlayerDelegate委托对象提供了两个主要方法:
audioPlayerDidFinishPlaying:successfully:
audioPlayerDecodeErrorDidOccur:error:
AVAudioRecorder
新建实例:Recorder
RecorderViewController.h文件
- #import <UIKit/UIKit.h>
- #import <AVFoundation/AVFoundation.h>
- @interface RecorderViewController : UIViewController
- {
- AVAudioRecorder *recorder;
- AVAudioPlayer *player;
- UILabel *label;
- }
- @property (retain, nonatomic) AVAudioRecorder * recorder;
- @property (retain, nonatomic) AVAudioPlayer * player;
- @property (retain, nonatomic) IBOutlet UILabel *label;
- -(IBAction)recordPushed:(id)sender;
- -(IBAction)playPushed:(id)sender;
- -(IBAction)stopPushed:(id)sender;
- @end
音频录制方法
- -(IBAction)recordPushed:(id)sender
- {
- label.text = @"recode...";
- if([recorder isRecording])
- return;
- if([player isPlaying])
- [player stop];
- NSError *error = nil;
- [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord
- error:&error];
- [[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];
音频录制方法
- NSMutableDictionary *settings = [NSMutableDictionary dictionary];
- [settings setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM]
- forKey:AVFormatIDKey];
- [settings setValue:[NSNumber numberWithFloat:44100.0]
- forKey:AVSampleRateKey]; //采样率
- [settings setValue:[NSNumber numberWithInt:1]
- forKey:AVNumberOfChannelsKey];//通道的数目
- [settings setValue:[NSNumber numberWithInt:16]
- forKey:AVLinearPCMBitDepthKey];//采样位数 默认 16
- [settings setValue:[NSNumber numberWithBool:NO]
- forKey:AVLinearPCMIsBigEndianKey];//大端还是小端 是内存的组织方式
- [settings setValue:[NSNumber numberWithBool:NO]
- forKey:AVLinearPCMIsFloatKey];//采样信号是整数还是浮点数
- NSString *filePath =
- [NSString stringWithFormat:@"%@/rec_audio.caf", [self documentsDirectory]];
- NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
- //[self setRecorder:nil];
- recorder = [[AVAudioRecorder alloc]
- initWithURL:fileUrl
- settings:settings
- error:&error];
- // [recorder setMeteringEnabled:YES];
- [recorder record];
- }
-(NSString *)documentsDirectory{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); return [paths objectAtIndex:0]; }
音频播放方法
- -(IBAction)playPushed:(id)sender{
- label.text = @"play...";
- if([recorder isRecording])
- [recorder stop];
- if([player isPlaying])
- [player stop];
- NSString *filePath =
- [NSString stringWithFormat:@"%@/rec_audio.caf", [self documentsDirectory]];
- NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
- NSError *error = nil;
- // [self setPlayer:nil];
- [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
- error:&error];
- [[AVAudioSession sharedInstance] setActive:YES error:&error];
- player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:&error];
- // [player setMeteringEnabled:YES];
- [player play];
- }
音频停止方法
- -(IBAction)stopPushed:(id)sender{
- label.text = @"stop...";
- if([recorder isRecording])
- [recorder stop];
- if([player isPlaying])
- [player stop];
- }