zoukankan      html  css  js  c++  java
  • [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];
        
    }

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

  • 相关阅读:
    《大话数据结构》第1章 数据结构绪论 1.2 你数据结构怎么学的?
    伍迷七八月新浪微博集锦
    《大话数据结构》第9章 排序 9.7 堆排序(下)
    《大话数据结构》第3章 线性表 3.8.2 单链表的删除
    《大话数据结构》第9章 排序 9.5 直接插入排序
    《大话数据结构》第9章 排序 9.8 归并排序(上)
    《大话数据结构》第2章 算法基础 2.9 算法的时间复杂度
    《大话数据结构》第1章 数据结构绪论 1.1 开场白
    《大话数据结构》第9章 排序 9.1 开场白
    [AWS] Assign a public IP address to an EC2 instance after launched
  • 原文地址:https://www.cnblogs.com/jinjiantong/p/2992610.html
Copyright © 2011-2022 走看看