182 lines
4.2 KiB
Objective-C
182 lines
4.2 KiB
Objective-C
//
|
|
// AudioPlayerView.m
|
|
// BlueCucumber-AudioPlayer
|
|
//
|
|
// Created by Thong Nguyen on 01/06/2012.
|
|
// Copyright (c) 2012 Thong Nguyen All rights reserved.
|
|
//
|
|
|
|
#import "AudioPlayerView.h"
|
|
|
|
@interface AudioPlayerView()
|
|
-(void) setupTimer;
|
|
-(void) updateControls;
|
|
@end
|
|
|
|
@implementation AudioPlayerView
|
|
@synthesize audioPlayer, delegate;
|
|
|
|
- (id)initWithFrame:(CGRect)frame
|
|
{
|
|
self = [super initWithFrame:frame];
|
|
|
|
if (self)
|
|
{
|
|
CGSize size = CGSizeMake(180, 50);
|
|
|
|
playFromHTTPButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
|
|
playFromHTTPButton.frame = CGRectMake((320 - size.width) / 2, 60, size.width, size.height);
|
|
[playFromHTTPButton addTarget:self action:@selector(playFromHTTPButtonTouched) forControlEvents:UIControlEventTouchUpInside];
|
|
[playFromHTTPButton setTitle:@"Play from HTTP" forState:UIControlStateNormal];
|
|
|
|
playFromLocalFileButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
|
|
playFromLocalFileButton.frame = CGRectMake((320 - size.width) / 2, 120, size.width, size.height);
|
|
[playFromLocalFileButton addTarget:self action:@selector(playFromLocalFileButtonTouched) forControlEvents:UIControlEventTouchUpInside];
|
|
[playFromLocalFileButton setTitle:@"Play from Local File" forState:UIControlStateNormal];
|
|
|
|
playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
|
|
playButton.frame = CGRectMake((320 - size.width) / 2, 350, size.width, size.height);
|
|
[playButton addTarget:self action:@selector(playButtonPressed) forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 290, 280, 20)];
|
|
slider.continuous = YES;
|
|
[slider addTarget:self action:@selector(sliderChanged) forControlEvents:UIControlEventValueChanged];
|
|
|
|
[self addSubview:slider];
|
|
[self addSubview:playButton];
|
|
[self addSubview:playFromHTTPButton];
|
|
[self addSubview:playFromLocalFileButton];
|
|
|
|
[self setupTimer];
|
|
[self updateControls];
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
-(void) sliderChanged
|
|
{
|
|
if (!audioPlayer)
|
|
{
|
|
return;
|
|
}
|
|
|
|
NSLog(@"Slider Changed: %f", slider.value);
|
|
|
|
[audioPlayer seekToTime:slider.value];
|
|
}
|
|
|
|
-(void) setupTimer
|
|
{
|
|
timer = [NSTimer timerWithTimeInterval:0.25 target:self selector:@selector(tick) userInfo:nil repeats:YES];
|
|
|
|
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
|
|
}
|
|
|
|
-(void) tick
|
|
{
|
|
if (!audioPlayer || audioPlayer.duration == 0)
|
|
{
|
|
slider.value = 0;
|
|
|
|
return;
|
|
}
|
|
|
|
slider.minimumValue = 0;
|
|
slider.maximumValue = audioPlayer.duration;
|
|
|
|
slider.value = audioPlayer.progress;
|
|
}
|
|
|
|
-(void) playFromHTTPButtonTouched
|
|
{
|
|
[self.delegate audioPlayerViewPlayFromHTTPSelected:self];
|
|
}
|
|
|
|
-(void) playFromLocalFileButtonTouched
|
|
{
|
|
[self.delegate audioPlayerViewPlayFromLocalFileSelected:self];
|
|
}
|
|
|
|
-(void) playButtonPressed
|
|
{
|
|
if (!audioPlayer)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (audioPlayer.state == AudioPlayerStatePaused)
|
|
{
|
|
[audioPlayer resume];
|
|
}
|
|
else
|
|
{
|
|
[audioPlayer pause];
|
|
}
|
|
}
|
|
|
|
-(void) updateControls
|
|
{
|
|
if (audioPlayer == nil)
|
|
{
|
|
[playButton setTitle:@"Play" forState:UIControlStateNormal];
|
|
}
|
|
else if (audioPlayer.state == AudioPlayerStatePaused)
|
|
{
|
|
[playButton setTitle:@"Resume" forState:UIControlStateNormal];
|
|
}
|
|
else if (audioPlayer.state == AudioPlayerStatePlaying)
|
|
{
|
|
[playButton setTitle:@"Pause" forState:UIControlStateNormal];
|
|
}
|
|
else
|
|
{
|
|
[playButton setTitle:@"Play" forState:UIControlStateNormal];
|
|
}
|
|
}
|
|
|
|
-(void) setAudioPlayer:(AudioPlayer*)value
|
|
{
|
|
if (audioPlayer)
|
|
{
|
|
audioPlayer.delegate = nil;
|
|
}
|
|
|
|
audioPlayer = value;
|
|
audioPlayer.delegate = self;
|
|
|
|
[self updateControls];
|
|
}
|
|
|
|
-(AudioPlayer*) audioPlayer
|
|
{
|
|
return audioPlayer;
|
|
}
|
|
|
|
-(void) audioPlayer:(AudioPlayer*)audioPlayer stateChanged:(AudioPlayerState)state
|
|
{
|
|
[self updateControls];
|
|
}
|
|
|
|
-(void) audioPlayer:(AudioPlayer*)audioPlayer didEncounterError:(AudioPlayerErrorCode)errorCode
|
|
{
|
|
[self updateControls];
|
|
}
|
|
|
|
-(void) audioPlayer:(AudioPlayer*)audioPlayer didStartPlayingQueueItemId:(NSObject*)queueItemId
|
|
{
|
|
[self updateControls];
|
|
}
|
|
|
|
-(void) audioPlayer:(AudioPlayer*)audioPlayer didFinishBufferingSourceWithQueueItemId:(NSObject*)queueItemId
|
|
{
|
|
[self updateControls];
|
|
}
|
|
|
|
-(void) audioPlayer:(AudioPlayer*)audioPlayer didFinishPlayingQueueItemId:(NSObject*)queueItemId withReason:(AudioPlayerStopReason)stopReason andProgress:(double)progress andDuration:(double)duration
|
|
{
|
|
[self updateControls];
|
|
}
|
|
|
|
@end
|