iOS 多媒体开发

来源:互联网 发布:人工智能的前景 编辑:程序博客网 时间:2024/05/22 03:32
#多媒体  
##systemSound   
1.只能播放本地的.wav,.caf等文件;   


2.需要包含<AudioToolbox/AudioToolbox.h>头文件   
3.对象的初始化:   
*用于表示系统音频文件对象 SystemSoundID soundFileObject;  
*可以理解为音频文件的位置和目录 CFURLRef soudnFileURLRef;   
4.步骤:
   
```   
1>//先得到音频文件的URL
    NSURL *soundFileURL = [[NSBundle mainBundle] URLForResource:@"Two" withExtension:@"caf"];
2>//    创建一个表示系统音频文件对象的变量
    self.soudnFileURLRef = (CFURLRef)[soundFileURL retain];
3> //  根据音频文件的URL使用Api接品来创建音频文件对象
  OSStatus ret = AudioServicesCreateSystemSoundID(self.soudnFileURLRef, &_soundFileObject);
 4> //播放各种声音如下:
  
  AudioServicesPlaySystemSound(self.soundFileObject);//系统声音
   AudioServicesPlayAlertSound(self.soundFileObject);//警告音
   
   AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//震动声音   


```
5.细节:这个系统的多媒体对象不能调节自己的音量,播放文件格式有要求   




 
##AVAudioPlayer
1.播放本地音乐或者缓存的文件,不能播放流媒体   
2.步骤:   
   
``` 
1>AVAudioPlayer *audioPlayer;
//    创建表示文件路径的对象
    NSURL *soundFileURL = [[NSBundle mainBundle] URLForResource:@"倩女幽魂" withExtension:@"mp3"];
//    实例化audioPlayer对象
    NSError *error = nil;
    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];   
  2>属性设置   
   self.audioPlayer.numberOfLoops = 2;
//    准备播放audiPlayer
    [self.audioPlayer prepareToPlay];
self.audioPlayer.duration;//音乐的时长


3>播放,暂停,停止
[self.audioPlayer play];
[self.audioPlayer pause];
[self.audioPlayer stop];
**注意:停止与pause没有区分开,停止后要手动把self.audioPlayer.currentTime设置为0,并且调用prepareplay方法否则会是暂停的效果
 
```   


##AVPlyer
1.播放流媒体音乐,支持后台播放


2.基本步骤:
   
```   
1> NSString *strURL = @"http://music.baidu.com/data/music/file?link=http://yinyueshiting.baidu.com/data2/music/123297915/1201250291413777661128.mp3?xcode=f17c3fd6b099333c1b467e4607d610d5a6a03c85c06f4409&song_id=120125029";
    
 2> self.player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:strURL]];
    [self setAudio2SupportBackgroundPlay];
//    设置开始接受远程事件(ios8以后的方法)
//    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];


3>设备是否支持后台播放(方法)   
- (void)setAudio2SupportBackgroundPlay
{
    UIDevice *device = [UIDevice currentDevice];
    if ([device respondsToSelector:@selector(isMultitaskingSupported)]) {
        if (device.multitaskingSupported) {
            AVAudioSession *audioSession = [AVAudioSession sharedInstance];
            NSError *error = nil;
            [audioSession setCategory:AVAudioSessionCategoryPlayback error:&error];
            if (error != nil) {
                NSLog(@"setCategory error.%@",error);
            }
            error = nil;
            [audioSession setActive:YES error:&error];
            if (error != nil) {
                NSLog(@"active category error.%@",error);
            }
        }
    }else
    {
        NSLog(@"unsupored background");
    }
}






```   


3.设备支持远程控制事件 
    
```   
*必须调用这个方法,才会响应远程事件*    
- (BOOL)becomeFirstResponder
{
    return YES;
}


*设置远程的动作,与远程设备有关*
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    if (event.type == UIEventTypeRemoteControl) {
        switch (event.subtype) {
            case UIEventSubtypeRemoteControlPause:
                //调用音频文件的暂停操作
            {
                [self pauseMusic:nil];
            }
                break;
            case UIEventSubtypeRemoteControlPlay:
                //调用播放器的播放动作
            {
                [self playMusic];
            }
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:
            {
                NSString *str = @"http://music.baidu.com/data/music/file?link=http://yinyueshiting.baidu.com/data2/music/122674129/1226741191413784861128.mp3?xcode=43808e599aa38593c3216f27fd3ae77fe70c8ba5d04fc31d&song_id=122674119";
                self.player = [AVPlayer playerWithURL:[NSURL URLWithString:str]];
                [self playMusic];
                //播放上一首音频数据
            }
                break;
                case UIEventSubtypeRemoteControlNextTrack:
            {
                //播放下一首音频文件
                NSString *str = @"http://music.baidu.com/data/music/file?link=http://yinyueshiting.baidu.com/data2/music/121949522/12152327672000128.mp3?xcode=86085ebefeaf5f8876bed49d3c85b6a51185dbd0cbb5163e&song_id=121523276";//没有什么不同-曲婉婷
                self.player = [AVPlayer playerWithURL:[NSURL URLWithString:str]];
                [self playMusic];
            }
                break;
            default:
                break;
        }
    }
    
}   


*界面将要呈现的时候, ,设置开始接受远程事件。前且将当前ViewController变成第一响应者*
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}


*界面将要消失的时候, 设置结束按收远程事件,并且将当前ViewController取消第一响应者, 以达到不接收远程事件的目的。*


- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
}




```
##MPMoviePlayerController&MPMoviePlayerViewController


1.MPMoviePlayerViewController比MPMoviePlayerView封装的更好,前者可以播放流媒体,(.m3u8的格式)
2.步骤 


```
*MPMoviePlayerViewController*
MPMoviePlayerViewController *videoPlayer;
   //如何实现播话流媒体文件
    NSURL *url = [NSURL URLWithString:@"http://live.3gv.ifeng.com/live/hongkong.m3u8"];
    
   MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    
   player.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    
   [self presentViewController:player animated:YES completion:nil];


```   


```
*MPMoviePlayerController*
MPMoviePlayerController *videoPlyer;
 NSURL *url = [[NSBundle mainBundle] URLForResource:@"S6T9U6014" withExtension:@"mp4"];
    self.videoPlyer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    
   [self.view addSubview:self.videoPlyer.view];
   [self.videoPlyer.view setFrame:CGRectMake(20, 20, 200, 200)];
   [self.videoPlyer prepareToPlay];
   [self.videoPlyer play];




```




##录音


1. AVAudioRecorder *audioReconder;
   AVAudioPlayer *audioPlayer;
   
2.步骤:


```
1>//设置文件路径
self.soundFilePath = [NSURL fileURLWithPath: [NSString stringWithFormat:@"%@/ recordTest.caf",[[NSBundle mainBundle] resourcePath]]];
    
   NSError *error = nil;
2> //设置录音的属性
   NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10];
    NSNumber *formatObject = [NSNumber numberWithInt:kAudioFormatAppleIMA4];
    [recordSettings setObject:formatObject forKey:AVFormatIDKey];
    [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
    [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; //1.单声道 2.立体声
    [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; //采样位
    [recordSettings setObject:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
    
   self.audioReconder = [[AVAudioRecorder alloc] initWithURL:self.soundFilePath settings:recordSettings  error:&error];
    if (error != nil) {
        NSLog(@"instance audioReconder error.%@",error);
    }
    
 3>   [self.audioReconder prepareToRecord];


```
0 0
原创粉丝点击