美文网首页
音视频播放总结

音视频播放总结

作者: 航行_iOS | 来源:发表于2018-04-08 16:01 被阅读0次

    音视频播放是iOS开发常用的功能,在这里做一个简单的总结:
    音频简单来分为短音频(音效)、长音频(音乐)

    (1)简单音效的播放:<AVFoundation>

    // 1.获得音效文件的路径
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];
    // 2.加载音效文件,得到对应的音效ID
    SystemSoundID soundID = 0;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
    // 3.播放音效 不带震动
    AudioServicesPlaySystemSound(soundID);
    //播放时带有震动
    //AudioServicesPlayAlertSound(soundID)
    

    (2)音乐播放:AVAudioPlayer(只能用来播放本地音乐)< AVFoundation>

    - (IBAction)startMusic:(id)sender {
    [self.player play];
    //    [self playMusicWithFileName:@"music_a.mp3"];
    }
    - (IBAction)suspendMusic:(id)sender {
    [self.player pause];
    //    [self pauseMusicWithFileName:@"music_a.mp3"];
    }
    - (IBAction)stopMusic:(id)sender {
    [self.player stop];
    self.player  = nil;
    //    [self stopMusicWithFileName:@"music_a.mp3"];
    
    }
    #pragma mark - 懒加载
    - (AVAudioPlayer *)player
    {
    if (_player == nil) {
        // 1.创建音乐文件
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"music_a.mp3" withExtension:nil];
        // 2.创建播放器
        _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        // 3.准备播放
        [_player prepareToPlay];
    }
    return _player;
    }
    

    (3)播放本地音乐、网络音乐、本地视频、网络视频AVPlayer < AVFoundation>

    - (AVPlayer *)player
    {
    if (_player == nil) {
     //方法一
     // 1.加载视频资源
     //本地
    //NSURL *localUrl = [[NSBundle mainBundle]     URLForResource:@"music_a.mp3" withExtension:nil]; 
     NSURL *localUrl = [[NSBundle mainBundle]     URLForResource:@"testVideo.mp4" withExtension:nil];
     //网络
    NSURL *networkUrl = [NSURL URLWithString:@""];
      // 2.创建播放器
     AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:localUrl];
      _player = [AVPlayer playerWithPlayerItem:playerItem];
    // 3.创建AVPlayerLayer  音频不需要  视频需要
    AVPlayerLayer *playerLayer = [AVPlayerLayer     playerLayerWithPlayer:_player];
    
    playerLayer.frame = CGRectMake(0, 100, self.view.bounds.size.width,   self.view.bounds.size.width * 9 / 16);
     [self.view.layer addSublayer:playerLayer];
    }
        return _player;
    }
    
    //调用播放方法
    [self.player play];
    

    (4).视频播放还有其他方法

    (1).MPMoviePlayerController<MediaPlayer/MediaPlayer.h> (可以调节全屏状态和非全屏状态) (iOS9.0后废弃)

    - (void)mpMoviePlayerControllerTest
    {
    NSURL*url=[NSURL URLWithString:@"http://v4ttyey-10001453.video.myqcloud.com/Microblog/288-4-1452304375video1466172731.mp4"];
    NSURL *localUrl = [[NSBundle mainBundle] URLForResource:@"testVideo.mp4" withExtension:nil];
    MPMoviePlayerController *mpMoviePlayerVC = [[MPMoviePlayerController alloc]initWithContentURL:url];
    mpMoviePlayerVC.view.frame = CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.width * 9 / 16);
    [self.view addSubview:mpMoviePlayerVC.view];
    }
    

    (2) MPMoviePlayerViewController<MediaPlayer/MediaPlayer.h> (以modal的样式弹出来,只能全屏显示) (iOS9.0后废弃)

    - (void)mpMoviePlayerViewControllerTest
    {
    NSURL*url=[NSURL URLWithString:@"http://v4ttyey-10001453.video.myqcloud.com/Microblog/288-4-1452304375video1466172731.mp4"];
    //NSURL *localUrl = [[NSBundle mainBundle] URLForResource:@"testVideo.mp4" withExtension:nil];
     MPMoviePlayerViewController *playerViewVC =       [[MPMoviePlayerViewController alloc]initWithContentURL:url];
       [self presentViewController:playerViewVC animated:YES completion:nil];
    }
    

    (3)AVPlayerViewController(以modal的样式弹出来,只能全屏显示)<AVKit>

    //此处传入的player就是AVPlayer格式的
     AVPlayerViewController *avplayerVC = [[AVPlayerViewController alloc]init];
        avplayerVC.player = _player;
        [self presentViewController:avplayerVC animated:YES completion:nil];
    

    所有格式都支持的框架VLC 、ffmpeg 、kxMovie

    上面的demo地址 https://github.com/hangxing0215/SHAVDemo

    相关文章

      网友评论

          本文标题:音视频播放总结

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