[ios]音频(录音),视频 基础

1.音频播放

#import <AVFoundation/AVFoundation.h>

<AVAudioPlayerDelegate>

AVAudioPlayer *player;

- (IBAction)start:(id)sender {
    NSError *err=nil;
    player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"charleston1925_64kb" ofType:@"mp3"]]error:&err];
    player.delegate=self;
    if(err){
        NSLog(@"%@",[err localizedDescription]);
        [err release];
    
    }
    [player play];
    
}

- (IBAction)stop:(id)sender {
    [player stop];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    NSLog(@"%@",@"play done!");

}

/* if an error occurs while decoding it will be reported to the delegate. */
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error{
    NSLog(@"%@",[error localizedDescription]);

}
2.音频播放

#import <AudioToolbox/AudioToolbox.h>

void SoundFinishedPlayingCallback(SystemSoundID sound_id, void* user_data){

    AudioServicesDisposeSystemSoundID(sound_id);
}

 NSURL *system_sound_url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"BeepGMC500" ofType:@"wav"]];
    SystemSoundID   system_sound_id;
    AudioServicesCreateSystemSoundID((CFURLRef)system_sound_url, &system_sound_id);
    AudioServicesAddSystemSoundCompletion(system_sound_id, NULL, NULL ,SoundFinishedPlayingCallback, NULL);
    AudioServicesPlaySystemSound(system_sound_id);

让设备震动

 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

3.视频播放

#import <MediaPlayer/MediaPlayer.h>

 

 NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp4"];  

   NSURL *url = [NSURL fileURLWithPath:path];  

    MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL:url];  

    movie.controlStyle = MPMovieControlStyleFullscreen;  

    [movie.view setFrame:self.view.bounds];  

    movie.initialPlaybackTime = -1;  

    [self.view addSubview:movie.view];  

 

 [[NSNotificationCenter defaultCenter] addObserver:self  

                                             selector:@selector(myMovieFinishedCallback:)  

                                                 name:MPMoviePlayerPlaybackDidFinishNotification  

                                               object:movie];  

    [movie play];

-(void)myMovieFinishedCallback:(NSNotification*)notify  

{  

    

    MPMoviePlayerController* theMovie = [notify object];  

   [[NSNotificationCenter defaultCenter] removeObserver:self  

                                                    name:MPMoviePlayerPlaybackDidFinishNotification  

                                                  object:theMovie];  

    [theMovie.view removeFromSuperview]; 

    [theMovie release];  


   

    [[NSNotificationCenter defaultCenter] addObserver:self  

                                             selector:@selector(myMovieFinishedCallback:)  

                                                 name:MPMoviePlayerPlaybackDidFinishNotification  

                                               object:movie];  

    [movie play];

4.录音,播放

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface RecorderViewController : UIViewController
{
    AVAudioRecorder * recoider;
    AVAudioPlayer * player;

}
@property (retain, nonatomic) IBOutlet AVAudioRecorder * recoider;
@property (retain, nonatomic) IBOutlet AVAudioPlayer * player;

m文件

-(NSString *)  documentsDirectory{

    NSArray * paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [paths  objectAtIndex:0];



}

//开始录音
- (IBAction)Rocord:(id)sender {
    
     self.myLabel.text=@"Recode.....";
    if([recoider isRecording])
    {
        return;
    
    }
    if([player isPlaying]){
        return;

    }
    NSError *error=nil;
    [[AVAudioSession sharedInstance]  setCategory:AVAudioSessionCategoryRecord error:&error];   
    [[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.recoider=[[AVAudioRecorder alloc] initWithURL:fileurl settings:settings error:&error];
    [self.recoider record];

    
}
//停止录制
- (IBAction)stop:(id)sender {
    self.myLabel.text=@"stoping...";
    if([recoider isRecording])
    {
        [recoider stop];
        
    }
    if([player isPlaying]){
        [player stop];
        
    }
}
//播放录制的音频
- (IBAction)play:(id)sender {
    self.myLabel.text=@"playing...";
    if([recoider isRecording])
    {
        return;
        
    }
    if([player isPlaying]){
        return;
        
    }
    NSError * errl=nil;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&errl];
    [[AVAudioSession sharedInstance] setActive:YES error:&errl];
    
    NSString * filePath=[NSString stringWithFormat:@"%@/rec_audio.caf",[self documentsDirectory]];
    NSURL * fileurl=[NSURL fileURLWithPath:filePath];

    self.player=[[AVAudioPlayer alloc] initWithContentsOfURL:fileurl error:&errl];
    [self.player play];
    
}

整理了一些多媒体应用中会用到的基础的东西

posted @ 2013-04-01 00:13  金建彤  阅读(386)  评论(0编辑  收藏  举报