avplay循环播放 ios
2017-02-27 10:54:00 weixin_33922670 阅读数 7

直接上代码,有注释

//
//  ViewController.m
//  SlipPicture
//
//  Created by chenqianfeng on 15/12/7.
//  Copyright © 2015年 chenqianfeng. All rights reserved.
//
#import "ViewController.h"
@interface ViewController (){
    UIScrollView *_sView;
    UIPageControl *_pgControler;
    NSArray *_imageArr;
}
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //创建数据
    [self createImageArr];
    //创建ScrollView
    [self creatScrollView];
    //创建Pagecontroller
    [self createPagecontroller];
}
- (void)createImageArr
{
    _imageArr = [[NSArray alloc]initWithObjects:@"3",@"0",@"1",@"2",@"3",@"0", nil];
}
- (void)creatScrollView
{
    _sView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100,[UIScreen mainScreen].bounds.size.width, 400)];
    //必须设置contentSize,要不然滚动不了(scrollEnabled默认为YES,可以滚动)
    _sView.contentSize = CGSizeMake(_sView.frame.size.width * _imageArr.count, 0);
    //设置翻页效果
    _sView.pagingEnabled = YES;
    //启动时候设置偏移量显示数组中的第二张图
    _sView.contentOffset = CGPointMake(_sView.frame.size.width, 0);
    //设置代理
    _sView.delegate = self;
    [self.view addSubview:_sView];
    //往ScrollView中加图片、(循环播放,)
    for (int i = 0; i<_imageArr.count; i++) {
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * i, 0, [UIScreen mainScreen].bounds.size.width, 400)];
        imgView.image = [UIImage imageNamed:_imageArr[i]];
        [_sView addSubview:imgView];
    }
}
- (void)createPagecontroller
{
    //Pagecontroller
    _pgControler = [[UIPageControl alloc]initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 100)/2, 100+200, 100, 40)];
    _pgControler.numberOfPages = 4;//设置Pagecontroller的页数
    _pgControler.currentPage = 0;//设置Pagecontroller的当前页
    _pgControler.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_pgControler];
}
#pragma  mark UIScrollViewDelegate
// scrollview 减速停止
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == 0) {
        //从第一张图向左滑(左边提前放了一张最后一张图,障眼法),设置偏移量使得瞬间跳到最后一张图
        scrollView.contentOffset = CGPointMake(self.view.frame.size.width * (_imageArr.count - 2), 0);
    } else if (scrollView.contentOffset.x == (_imageArr.count - 1)*self.view.frame.size.width){
        //从最后一张图向右滑(右边提前放了一张第一张图,障眼法),设置偏移量使得瞬间跳到第一张图
        scrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
    }else{
    }
    //pageController的currentPage
    _pgControler.currentPage = scrollView.contentOffset.x / self.view.frame.size.width - 1;
}
@end
2012-07-09 13:08:27 linux_zkf 阅读数 2390

作者:朱克锋

邮箱:zhukefeng@iboxpay.com

转载请注明出处:http://blog.csdn.net/linux_zkf


#define PATHSTRING @"http://。。。"


-(void)myMovieFinishedCallback:(NSNotification*)aNotification

{

    MPMoviePlayerController* theMovie=[aNotification object];

[theMovie play];

}


- (void) play: (UIBarButtonItem *) barBtnItem

{

MPMoviePlayerController* theMovie=[[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:PATHSTRING]];


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];

[theMovie play];

}

2017-09-01 09:18:37 Fantasy_Jun 阅读数 3027
/**
 *  添加播放器通知,通过AVPlayerItemDidPlayToEndTimeNotification字段判断播放器播放情况
 */
-(void)addNotification{
    //给AVPlayerItem添加播放完成通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
}

-(void)removeNotification{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/**
 *  播放完成通知
 *
 *  @param notification 通知对象
 */
-(void)playbackFinished:(NSNotification *)notification{
    NSLog(@"视频播放完成.");
    // 播放完成后重复播放
    // 跳到最新的时间点开始播放
    [_player seekToTime:CMTimeMake(0, 1)];
    [_player play];
}
2013-08-27 16:41:18 NNI2567 阅读数 338

//引入

#import <AVFoundation/AVFoundation.h>

//并在借口文件中添加要实现的代理

@interface ViewController : UIViewController<AVAudioPlayerDelegate>


//建全局变量myplayer

AVAudioPlayer *myplayer;


//获取mp3路径,音乐的名字为1.mp3

NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp3"];

//转为url

NSURL *url = [[NSURL alloc] initFileURLWithPath:path];


//初始化myplayer

myplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

//设置代理

myplayer.delegate = self;

//实现代理的循环播放函数

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

{

    [myplayer play];

    

}


2016-04-11 13:15:00 weixin_34220179 阅读数 27

在类中什么两个属性

@property (nonatomic,assign) BOOL isDragging;               //是否正在拖动
@property (nonatomic,strong)NSTimer *timer;                 //设置动画

用作判断是否在拖地和添加动画

先创建scrollview

UIView  *view4Header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, 100)];

UIScrollView * _scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWIDTH, 100)];
    _scrollview.contentSize=CGSizeMake(kWIDTH*6, 100);
//    _scrollview.contentOffset = CGPointMake(0, 0);
    _scrollview.pagingEnabled=YES;
    _scrollview.delegate=self;
    _scrollview.showsHorizontalScrollIndicator = NO;
    [view4Header addSubview:_scrollview];

    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((kWIDTH - 80)/2, view4Header.frame.size.height - 20, 80, 10)];
    _pageControl.numberOfPages = 4;
    _pageControl.currentPage = 0;
    [view4Header addSubview:_pageControl];
    
    //贴图
    for (int i=0; i<6; i++) {

        int y=i + 5;
        if (i == 5) {
            y = 6;
        }else if (i == 0){
            y = 9;
        }

        UIImageView*imageView=[[UIImageView alloc]initWithFrame:CGRectMake(i*kWIDTH, 0, kWIDTH, 100)];
        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"pic_n000%d",y]];
        [_scrollview addSubview:imageView];
    }

//  设置时钟动画 定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.5f target:self selector:@selector(update:) userInfo:nil repeats:YES];
    //  将定时器添加到主线程
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

实现定时移动效果

- (void)update:(NSTimer *)timer{
    //定时移动
    
    CGPoint offSet = _scrollview.contentOffset;
    
    if (_isDragging == YES) {
        return ;
    }
    
    offSet.x = offSet.x + kWIDTH;
   
    [_scrollview setContentOffset:offSet animated:YES];
    
    if (offSet.x / kWIDTH == 5) {
        [_scrollview setContentOffset:CGPointMake(0, 0)];
    }
}

最后添加上ScrollView的代理方法,使其可以滑动拖动

#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _isDragging = YES;
}
//  停止滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    float offset = _scrollview.contentOffset.x;
    
    if (offset > kWIDTH*4) {
        [_scrollview setContentOffset:CGPointMake(kWIDTH, 0)];
    }

    if (offset < kWIDTH) {
        [_scrollview setContentOffset:CGPointMake(4*kWIDTH, 0)];
    }
    _isDragging = NO;
}
没有更多推荐了,返回首页