day7---视频、音频

一、音频播放器的创建
#import 导入头文件
1)创建音频播放器
_player = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:_musicArr[_index] ofType:@"mp3"]] error:nil];
2)开启播放器
[_player play];

//播放歌曲
-(void)playWithIndex:(NSInteger)index
{
UIButton *bt = (UIButton *)[self.view viewWithTag:104];

if (_player)
{
    //如果正在播放
    if ([_player isPlaying]) {          【控制播放停止的方法】
        [_player pause];
        [bt setBackgroundImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
    }else
    {
        [_player play];
        [bt setBackgroundImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateNormal];
    }
    
}else
{
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:_musicArr[_index] ofType:@"mp3"]];
    _player = [[AVAudioPlayer alloc] initWithData:data error:nil];
    _player.delegate = self;
    //播放
    [self getAsset];
    [_player play];
    
    [bt setBackgroundImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateNormal];
}  

}

二、根据歌曲的进度,用计数器控制进度条的进度
-(void)changeVale
{
if (_player) {
if (_slider.touchInside == YES) {
return;
}
//当前播放的时长与总时长的比例 【当前播放的进度和歌曲总时长】
_slider.value = _player.currentTime/_player.duration;
}
}

pragma mark --滑动slider响应的方法.

-(void)changeValue
{
if (_player) { 【用进度条的滑动控制歌曲进度】

    _player.currentTime = _player.duration*_slider.value;   
}  

}

三、音频自带的本地申请数据的方法:

-(void)getAsset
{
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:_musicArr[_index] ofType:@"mp3"]] options:nil];

//数据里面存储的是 AVMetadataItem * 类型的数据
NSArray *commonData = [asset commonMetadata];
for (AVMetadataItem *item in commonData) {
    
    if ([item.commonKey isEqualToString:@"albumName"]) {
        NSLog(@"%@",item.value);
    }else if ([item.commonKey isEqualToString:@"title"])
    {
        _nameLabel.text = (NSString *)item.value;
    }else if ([item.commonKey isEqualToString:@"artist"])
    {
        NSLog(@"演唱:%@",item.value);
    }else if ([item.commonKey isEqualToString:@"artwork"])
    {
        _musicImageV.image = [UIImage imageWithData:(NSData *)item.value];
    }
    
    NSLog(@"%@",item);

// item.value
}
}

四、代理方法,音频播放结束时调用一下方法:
    AVAudioPlayerDelegate       【代理方法】

//播放结束的代理

pragma mark -AVAudioPlayerDelegate

  • (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
    {
    //第一种情况 如果单曲循环则
    // _index不变 _player.currentTime = 0;

    //第二种播放下一首
    if (_index == _musicArr.count - 1 ) {
    _index = 0;
    }else
    {
    _index ++;
    }
    [self creatNewPlay];

}

视频
#import 【导入视频头文件】
一、创建视频播放器
//1.创建播放器对象
_play = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"" ofType:@"mov"]]];

//2.设置播放器视图的大小
_play.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300);

//播放器的相关属性
//慢速,默认是1;
_play.currentPlaybackRate = 0.5;
//设置播放器的格式  选择0表示无格式
_play.controlStyle = MPMovieControlStyleFullscreen;

//3.启动播放器
[_play play];
[self.view addSubview:_play.view];




//横竖屏是通过状态栏来控制的,所以用以下方法控制横竖屏:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenFull) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenExit) name:MPMoviePlayerWillExitFullscreenNotification object:nil];

//AVplayer  自定义的播放器

-(void)screenFull
{
//控制状态栏向右旋转
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;

}

-(void)screenExit
{
//控制状态栏竖屏显示
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;

}

//允许横竖屏的方法,这个方法必须在根视图控制器添加这个方法;
-(BOOL)shouldAutorotate
{
return NO;
}

你可能感兴趣的:(day7---视频、音频)