본문 바로가기
ios 뽀개기/objective-c

UIView 커스터마이징 & 오디오 비디오 사진 뷰어

by 인생여희 2018. 12. 28.
반응형

uiview 커스터마이징 & 오디오 비디오 사진 뷰어


.h


#import <MCore/MCore.h>

#import <MediaPlayer/MediaPlayer.h>

#import <AVKit/AVKit.h>

@interface MediaViewActivity : UIViewController <AVAudioPlayerDelegate>


@end


.m


#import "MediaViewActivity.h"


@interface MediaViewActivity ()

@property (strong, nonatomic) MPMoviePlayerController *moviePlayer;

@property (strong, nonatomic)  AVAudioPlayer *audioPlayer;

@property (nonatomic, assign) BOOL isPlaying;

@property(strong,nonatomic)  NSTimer *TimeOfActiveUser;

@property (nonatomic, assign) NSInteger second;                                                     //타임 인터발

@property(strong,nonatomic) UILabel *time;

@property (strong, nonatomic)  UISlider *currentTimeSlider;

- (void)currentTimeSliderValueChanged:(id)sender;

- (void)currentTimeSliderTouchUpInside:(id)sender;

@end


@implementation MediaViewActivity


- (void)viewDidLoad {

    [super viewDidLoad];

   

    //document 경로

    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(

                                                                       NSDocumentDirectory,

                                                                       NSUserDomainMask,

                                                                       YES) objectAtIndex:0];

    

   

    //album.js에서 오디오,사진,영상 중에 클릭했을때 파일이 저장된 로컬 url을 넘겨준다.

    NSString* reletivePath =  [_parameters objectForKey:@"url" encoding:NSUTF8StringEncoding];

   

    NSString* mediaUrlString = [documentDirectory stringByAppendingFormat:@"%@" , reletivePath];

    

    //확장자명을 통해서 분기. 미디어 타입별로 다른 Viewer 열기.

    NSRange strRange;

    

    strRange = [mediaUrlString rangeOfString:@"photo"];

    if (strRange.location != NSNotFound) {

        

        NSLog(@"사진 파일");

        NSLog(@"mediaUrlString : %@" , mediaUrlString);

        UIImage *image = [UIImage imageWithContentsOfFile:mediaUrlString];

        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        imageView.frame = CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height - 140);

        [self.view addSubview:imageView];

        

        //닫기 버튼 custom

        UIButton *closeButton = [[UIButton alloc]init];

        closeButton.frame=CGRectMake(320, 15, 40, 40);

        NSString *thePath = [[NSBundle mainBundle] pathForResource:@"camera_ic10" ofType:@"png"];

        UIImage *closeImg = [UIImage imageWithContentsOfFile:thePath];

        [closeButton setImage:closeImg forState:UIControlStateNormal];

        [closeButton addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:closeButton];

        

    }

    

    strRange = [mediaUrlString rangeOfString:@"video"];

    if (strRange.location != NSNotFound) {


        //닫기 버튼 custom

        UIButton *closeButton = [[UIButton alloc]init];

        closeButton.frame=CGRectMake(320, 15, 40, 40);

        [closeButton setTitle:@"video" forState:UIControlStateNormal];

        [closeButton setTitleColor:UIColor.clearColor forState:UIControlStateNormal];

        NSString *thePath = [[NSBundle mainBundle] pathForResource:@"camera_ic10" ofType:@"png"];

        UIImage *closeImg = [UIImage imageWithContentsOfFile:thePath];

        [closeButton setImage:closeImg forState:UIControlStateNormal];

        [closeButton addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];

        

         //비디오 플레이어

        _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaUrlString]];


        self.moviePlayer.view.frame = CGRectMake (0, 0, self.view.frame.size.width, self.view.frame.size.height-20);

        self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

       

        [self.view addSubview:self.moviePlayer.view];

        [self.moviePlayer.view addSubview:closeButton];

        

        [self.moviePlayer play];


    }

    

    strRange = [mediaUrlString rangeOfString:@"audio"];

    if (strRange.location != NSNotFound) {

        

        //백그라운드 그림 바꾸기

        NSString *backgroundImagePath = [[NSBundle mainBundle] pathForResource:@"camera_ic02" ofType:@"png"];

        UIImage *backgroundImg = [UIImage imageWithContentsOfFile:backgroundImagePath];

        

        

        UIImageView *backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50, self.view.frame.size.height / 2 -73, 100, 100)];

        [backImageView setImage:backgroundImg];

        

        

        //닫기 버튼 custom

        UIButton *closeButton = [[UIButton alloc]init];

        closeButton.frame=CGRectMake(320, 15, 40, 40);

        [closeButton setTitle:@"audio" forState:UIControlStateNormal];

        [closeButton setTitleColor:UIColor.clearColor forState:UIControlStateNormal];

        NSString *thePath = [[NSBundle mainBundle] pathForResource:@"camera_ic10" ofType:@"png"];

        UIImage *closeImg = [UIImage imageWithContentsOfFile:thePath];

        [closeButton setImage:closeImg forState:UIControlStateNormal];

        [closeButton addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];

        

        


        

        //오디오 플레이어

        _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaUrlString]];

        

        self.moviePlayer.view.frame = CGRectMake (0, self.view.frame.size.height - 65

                                                  , self.view.frame.size.width, 55);

        self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

        

        [self.view addSubview:backImageView];

        [self.view addSubview:self.moviePlayer.view];

        [self.view addSubview:closeButton];

        

        [self.moviePlayer play];

//        //닫기 버튼 custom

//        UIButton *closeButton = [[UIButton alloc]init];

//        closeButton.frame=CGRectMake(320, 15, 40, 40);

//        NSString *thePath = [[NSBundle mainBundle] pathForResource:@"camera_ic10" ofType:@"png"];

//        UIImage *closeImg = [UIImage imageWithContentsOfFile:thePath];

//        [closeButton setImage:closeImg forState:UIControlStateNormal];

//        [closeButton addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];

//

//

//        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(137, 56, 343, 17)];

//        [title setText:@"제목"];

//

//

//        _time = [[UILabel alloc]initWithFrame:CGRectMake(137, 119, 101, 36)];

//        _time.text = @"00:00";

//

//        //시작 멈춤 버튼

//        UIButton *startBtn = [[UIButton alloc]initWithFrame:CGRectMake(16, 220, 119, 102)];

//        [startBtn setTitle:@"시작/멈춤" forState:UIControlStateNormal];

//        [startBtn addTarget:self action:@selector(startAudioAction:) forControlEvents:UIControlEventTouchUpInside];

//

//        //정지버튼

//         UIButton *stopBtn = [[UIButton alloc]initWithFrame:CGRectMake(258, 220, 101, 102)];

//         [stopBtn setTitle:@"정지" forState:UIControlStateNormal];

//        [stopBtn addTarget:self action:@selector(stopAudioAction:) forControlEvents:UIControlEventTouchUpInside];

//

//

//        //오디오 플레이어 준비

//

//        _audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:

//                        [NSURL fileURLWithPath:mediaUrlString] error:NULL];

//        _audioPlayer.delegate = self;

//        [_audioPlayer prepareToPlay];

//        [self setIsPlaying:NO];

//

//

//        //슬라이더

//        _currentTimeSlider = [[UISlider alloc]initWithFrame:CGRectMake(26, 500, 160, 60)];

//        _currentTimeSlider.minimumValue = 0.0f;

//        _currentTimeSlider.maximumValue = self.audioPlayer.duration;

//        _currentTimeSlider.continuous = YES;

//        [_currentTimeSlider addTarget:self action:@selector(currentTimeSliderValueChanged:) forControlEvents:UIControlEventValueChanged];

//        [_currentTimeSlider addTarget:self action:@selector(currentTimeSliderTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];

//

//

//        //view에 넣어주기

//        [self.view setBackgroundColor:UIColor.greenColor];

//          [self.view addSubview:_currentTimeSlider];

//        [self.view addSubview:closeButton];

//        [self.view addSubview:title];

//         [self.view addSubview:_time];

//         [self.view addSubview:startBtn];

//         [self.view addSubview:stopBtn];

//

//

//        //오디오 플레이어 시작

//        [self startAudioAction:startBtn];

    }

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


////오디오 정지

//- (void)stopAudioAction:(UIButton *)button {

//

//    [_audioPlayer stop];

//    [self setIsPlaying:NO];

//

//    //타이머 정지

//    [_TimeOfActiveUser invalidate];

//    _TimeOfActiveUser = nil;

//

//    //타이머 라벨 및 변수 초기화

//    _time.text = [NSString stringWithFormat:@"%0.2d:%0.2d", 0, 0];

//    _second = 0;

//   _audioPlayer.currentTime = 0;

//    [_audioPlayer prepareToPlay];

//}

//

//

////오디오 시작

//- (void)startAudioAction:(UIButton *)button {

//

//    //재생중

//    if (_isPlaying) {

//        [_audioPlayer pause];

//        [self setIsPlaying:NO];

//

//        //타이머 정지

//        [_TimeOfActiveUser invalidate];

//        _TimeOfActiveUser = nil;

//

//    }else{

//

//        [_audioPlayer play];

//        [self setIsPlaying:YES];

//

//        //타이머 시작

//        _TimeOfActiveUser =  [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {

//

//            [self updateDisplay];

//

//        }];

//

//    }

//

//}

//

//#pragma mark - AVAudioPlayerDelegate

//

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

//

//    NSLog(@"audioPlayerDidFinishPlaying");

//

//    //타이머 정지

//    [_TimeOfActiveUser invalidate];

//    _TimeOfActiveUser = nil;

//      [self setIsPlaying:NO];

//

//    //타이머 라벨 및 변수 초기화

//    [self updateDisplay];

//

//}

//

//- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error{

//    NSLog(@"audioPlayerDecodeErrorDidOccur");

//    //타이머 정지

//    [_TimeOfActiveUser invalidate];

//    _TimeOfActiveUser = nil;

//      [self setIsPlaying:NO];

//    //타이머 라벨 및 변수 초기화

//     [self updateDisplay];

//}

//


//

//- (void)currentTimeSliderValueChanged:(id)sender{

//

//}

//- (void)currentTimeSliderTouchUpInside:(id)sender{

//

//    [self.audioPlayer stop];

//    self.audioPlayer.currentTime = self.currentTimeSlider.value;

//    [self.audioPlayer prepareToPlay];

//    [self setIsPlaying:NO];

//    [self startAudioAction:nil];

//}

//

//

//- (void)updateDisplay

//{

//

//    NSTimeInterval currentTime = self.audioPlayer.currentTime;

//

//    long seconds = (int)currentTime % 60;

//    long minutes = ((int)currentTime / 60) % 60;

//

//    _time.text = [NSString stringWithFormat:@"%0.2ld:%0.2ld", minutes, seconds];

//

//    self.currentTimeSlider.value = currentTime;

//}

//닫기 버튼

- (void)closeAction:(UIButton *)button {

    

    //버튼에 따라서 분기해주기

    //오디오 재생중에 종료했을때 ..stop 처리 해주기

    

    if ([button.titleLabel.text isEqualToString:@"audio"]) {

        

         [self.moviePlayer stop];

    }else if([button.titleLabel.text isEqualToString:@"video"]){

         [self.moviePlayer stop];

    }


    [self historyBack:nil];

}

@end



오디오 참고 파일

iPhoneStreamingPlayer.zip

IDZAudioPlayerTutorial1.zip

AudioStreamer-master (2).zip





반응형

'ios 뽀개기 > objective-c' 카테고리의 다른 글

암호화 복호화  (0) 2019.01.02
Obejctive c 클래스 변수 & 배열을 json 변환  (0) 2019.01.02
ios 저장 용량 계산  (0) 2018.12.27
ios 오디오 + 비디오 권한 체크 함수  (0) 2018.12.20
FileManager  (0) 2018.12.20

댓글