音视频播放

作者: 码农小白 | 来源:发表于2016-07-05 22:58 被阅读98次

    一、系统声音播放AudioToolBox(无界面)

    引入@improt AudioToolbox;
    1.可以播放系统的声音,如铃声等。
    2.可以使手机震动。
    3.只能播放时间长度在30s以内的声音。
    4.不能够控制播放进度。
    5.没有循环播放和立体声功能。
    6.调用后立即执行。
    7.支持caf/aif/wav/mp3格式。
    8.最大的好处:属于底层,节省资源。如搜狗输入法的按键声音等。

    - (IBAction)playMusic:(id)sender {
        // 播放系统声音(真机)
        AudioServicesPlaySystemSound(1200);// 1000~2000
        // 震动(静音+真机)
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        // 1.获取音频地址.
        NSString *path = [[NSBundle mainBundle] pathForResource:@"videoRing" ofType:@"mp3"];
        // 1.1 获取URL路径.
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"videoRing" withExtension:@"mp3"];
        // 系统声音把1000~2000的数字都占用了,其他随便用.
        SystemSoundID soundId = 11;
        // 绑定声音和数字
        AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundId);
        // 2.播放声音.
        AudioServicesPlaySystemSound(soundId);
    }
    

    二、AudioPlayer用于播放本地音频,没有网络功能,在iOS8之后被AVPlayer取代(没有界面)

    引入@import AVFoundation;
    为了防止疯狂点击疯狂重复播放,所以需要全局定义AVAudioPlayer:
    @property (nonatomic) AVAudioPlayer *audioPlayer;
    以下是示例:

    - (IBAction)playMusic:(id)sender {
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"周杰伦 - 告白气球" withExtension:@"mp3"];
        NSError *error = nil;
        _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        // 播放的速度
        // 如果是slider的话,要先暂停,表示打开波形的监听
    //    _audioPlayer.enableRate = YES;
    //    _audioPlayer.rate = 1;
        // 监听播放的音频音线波形的大小
        _audioPlayer.meteringEnabled = YES;
        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(readMeter:) userInfo:nil repeats:YES];
        // 循环播放次数
        _audioPlayer.numberOfLoops = 2;
        // 设置起始播放时间
        _audioPlayer.currentTime = 30;
        // 获取总时长,可以使用滑动条,通过半分法
        NSTimeInterval total = _audioPlayer.duration;
        _audioPlayer.delegate = self;// 设置代理,监听.
        [_audioPlayer play];
    }
    

    三、AVPlayer强大的播放类,可以播放视频、音频、远程和本地资源(没有界面)

    引入@impoert AVFoundation;
    由于该类是没有界面的,所以播放视频的时候需要一个用来显示视频的界面,即AVPlayerLayer创建的对象。

    @interface ViewController ()
    @property (nonatomic) AVPlayer *audioPlayer;
    @property (nonatomic) AVPlayer *videoPlayer;
    @property (nonatomic) AVPlayerLayer *videoLayer;// 视频需要一个显示画面的
    @end
    @implementation ViewController
    - (IBAction)playMusic:(id)sender {
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"张宇 - 趁早" withExtension:@"mp3"];
        _audioPlayer = [AVPlayer playerWithURL:url];
        [_audioPlayer play];
    }
    - (IBAction)playMovie:(id)sender {
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"lllll" withExtension:@"mov"];
        _videoPlayer = [AVPlayer playerWithURL:url];
        // AVPlayerLayer 是用来显示视频的
        _videoLayer = [AVPlayerLayer playerLayerWithPlayer:_videoPlayer];
        _videoLayer.frame = self.view.bounds;
        [self.view.layer addSublayer:_videoLayer];
        [_videoPlayer play];
        
    }
    

    四、MediaPlayer早期的播放视频音频的控制器,在iOS8之后被AVKit所取代(自带播放页面和控制按钮)

    引入@improt MediaPlayer;

    - (IBAction)playAudio:(id)sender {
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"戴荃 - 悟空" withExtension:@"mp3"];
        MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
        [self presentViewController:vc animated:YES completion:nil];
    }
    - (IBAction)playVideo:(id)sender {
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"lllll" withExtension:@"mov"];
        MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
        [self presentViewController:vc animated:YES completion:nil];
    }
    

    五、AVKit是iOS8之后的,取代MediaPlayer(自带界面和控制按钮)

    引入@import AVFoundation;@import AVKit;

    - (IBAction)playAudio:(id)sender {
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"戴荃 - 悟空" withExtension:@"mp3"];
        AVPlayerViewController *playerVC = [[AVPlayerViewController alloc] init];
        playerVC.player = [AVPlayer playerWithURL:url];
        [playerVC.player play];
        [self presentViewController:playerVC animated:YES completion:nil];
    }
    - (IBAction)playVideo:(id)sender {
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"lllll" withExtension:@"mov"];
        AVPlayerViewController *playerVC = [[AVPlayerViewController alloc] init];
        playerVC.player = [AVPlayer playerWithURL:url];
        [playerVC.player play];
        [self presentViewController:playerVC animated:YES completion:nil];
    }
    

    系统文字转语音

    引入@import AVFoundation;

    - (IBAction)sayWords:(id)sender {
        // 必须有网络才能进行文字转语音
        // 1.设置要说的话
        NSString *words = @"你好!";
        // 打印出所支持的语言
        NSLog(@"%@",[AVSpeechSynthesisVoice speechVoices]);
        // 2.设置说话时的语言
        AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-HK"];
        AVSpeechUtterance *utt = [AVSpeechUtterance speechUtteranceWithString:words];
        // 默认是使用系统语言
        utt.voice = voice;
        // 3.开始说
        AVSpeechSynthesizer *syn = [[AVSpeechSynthesizer alloc] init];
        [syn speakUtterance:utt];
    }
    

    关于后台播放(可以在程序任何位置设置)

    • 首先需要设置当前的会话模式
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    • 然后激活会话模式
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    
    • 最后需要在info.plist文件中添加Required background modes并将其item0改为App plays audio or streams audio/video using AurPlay

    相关文章

      网友评论

      • 4d4aa2a610a2:你好,为什么我用avplayer播放本地mov格式视频,播放不出来呢,mp4格式的可以播放

      本文标题:音视频播放

      本文链接:https://www.haomeiwen.com/subject/texkjttx.html