AVAudioPlayer avtouch Application iphone Application Index 1. iphone Application 1) iphone Application 2) iphone Application Main Method 3) iphone Application nib(.xib) 2. avtouch Application 1) avtouch 2) avtouch Classes & Resources 3) avtouchappdelegate 4) avtouchviewcontroller 5) avtouchcontroller 1) nib(.xib) 2) 3) 4) 5) / 6) / 7) 8) 9) 10)
iphone Application 1. iphone Application 2. iphone OS Application 3. 4. main() 5. Primary thread 6. UIApplicationMain() 7. UIApplication 8. info.plist 9. MainWindow.xib 10. xib iphone Application Main Method #import <UIKit/UIKit.h> nil Info.plist xib MainWindow.xib xib File s Owner UIApplication. int main(int argc, char *argv[]) NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int retval = UIApplicationMain(argc, argv, nil, nil); // argc, argv C main // nil : // nil : [pool release]; return retval; nil UIApplicationMain() > MainWindow.xib > avtouch App Delegate. iphone UI UI ( iphone main UIApplicationMain )
iphone Application nib(.xib) iphone Application main UIApplicationMain nib(.xib) : File s Owner, First Responder, avtouch App Delegate, Window UIApplicationMain UIApplication File s Owner = UIApplication nib MainWindow.xib UIApplication MainWindow.xib File s Owner. UIApplication. avtouch Application Application avtouch / /
avtouch ClassesClasses & Resources avtouch & Resources AudioViews : avtouchappdelegate : avtouch avtouchviewcontroller : avtouch avtouchcontroller : avtouch main.m : avtouch audio main : avtouch images : avtouch avtouchviewcontroller.xib : avtouch MainWindow.xib : avtouch xib Info.plist : MainWindow.xib AudioToolbox : AVFoundation : AVAudioPlayer avtouchviewcontroller UI UI avtouchviewcontroller UISlider *progressbar; UILabel *filename; UILabel *currenttime; UILabel *duration; CALevelMeter *lvlmeter_in; UILabel UISlider *repeatfrom; UIButton *volumeslider; UILabel UIButton *rewbutton; UIButton UIButton *playbutton; *ffwbutton; *repeatbtn; *repeatto;
avtouch iphone Application 1. File > New Project iphone OS > Application View-based Application.
iphone Application 2. avtouchappdelegate, avtouchviewcontroller (header, implement ). - avtouchappdelegate :, xib,,. - avtouchviewcontroller :. * avtouch2 avtouch. avtouchappdelegate avtouch MainWindow.xib avtouchviewcontroller viewcontroller avtouchappdelegate.h avtouchappdelegate.m #import <UIKit/UIKit.h> @class avtouchviewcontroller; @interface avtouchappdelegate : NSObject <UIApplicationDelegate> IBOutlet UIWindow *window; // viewcontroller IBOutlet avtouchviewcontroller *viewcontroller; @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet avtouchviewcontroller *viewcontroller; @end #import "avtouchappdelegate.h" #import "avtouchviewcontroller.h" @implementation avtouchappdelegate @synthesize window; @synthesize viewcontroller; - (void)applicationdidfinishlaunching:(uiapplication *)application // viewcontroller mainwindow [window addsubview:viewcontroller.view]; [window makekeyandvisible]; - (void)dealloc [viewcontroller release]; [window release]; [super dealloc]; @end
avtouchviewcontroller avtouchappdelegate avtouch avtouchcontroller controller avtouchviewcontroller.h avtouchviewcontroller.m #import <UIKit/UIKit.h> @class avtouchcontroller; @interface avtouchviewcontroller : UIViewController IBOutlet avtouchcontroller *controller; @end #import "avtouchviewcontroller.h" #import "avtouchcontroller.h" @implementation avtouchviewcontroller - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceorientation // Return YES for supported orientations return (interfaceorientation == UIInterfaceOrientationPortrait); - (void)didreceivememorywarning [super didreceivememorywarning]; // Releases the view if it doesn't have a superview // Release anything that's not essential, such as cached data - (void)dealloc [super dealloc]; @end iphone Application * avtouchappdelegate.m applicationdidfinishlaunching - ios 4.0. void BOOL return YES;.. - (void)applicationdidfinishlaunching:(uiapplication *)application // Override point for customization after app launch [window addsubview:viewcontroller.view]; [window makekeyandvisible]; ios 3.0 - (BOOL)application:(UIApplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions // Override point for customization after application launch. // Add the view controller's view to the window and display. [window addsubview:viewcontroller.view]; [window makekeyandvisible]; ios 4.0 return YES;
iphone Application * avtouch,. Frameworks (control + ) Add > Existing Frameworks Add. ( command ) - AudioToolbox.framework : - AVFoundation.framework : - QuartzCore.framework : - OpenGLES.framework : iphone Application xib * Resource avtouchviewcontroller.xib MainWindow.xib. View-based ViewController xib. - MainWindow.xib - avtouchviewcontroller.xib * MainWindow.xib, iphone MVC,,.
iphone MainWindow.xib Application MainWindow.xib. @interface avtouch2appdelegate : NSObject <UIApplicationDelegate> UIWindow *window; avtouch2viewcontroller *viewcontroller; @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet avtouch2viewcontroller *viewcontroller; avtouchappdelegate.h [window addsubview:viewcontroller.view]; avtouchappdelegate.m avtouchappdelegate? avtouchviewcontroller viewcontroller, IBOutlet. IBOutlet Interface Builder Outlet, (.xip ) GUI. MainWindow.xib avtouchviewcontroller. avtouchviewcontroller. iphone avtouchcontroller Application 3. avtouchcontroller, avtouchviewcontroller.h. - File > New File iphone OS Cocoa Touch Class > Objective-C class (Subclass of NSObject) Next, avtouchcontroller Finish.
iphone avtouchcontroller Application 3. avtouchcontroller, avtouchviewcontroller.h. - Class avtouchcontroller. - avtouchviewcontroller.h avtouchcontroller controller IBOutlet. avtouchviewcontroller.xib avcontroller. #import <UIKit/UIKit.h> @class avtouchcontroller; @interface avtouch2viewcontroller : UIViewController IBOutlet avtouchcontroller *controller; @end avtouchviewcontroller.h... #import "avtouchcontroller.h"... avtouchviewcontroller.m avtouchviewcontroller.xib iphone Application 4. controller. avtouchviewcontroller.xib Tools > Inspector Library. Inspector Library. Library. avtouchcontroller. UISlider *progressbar; UILabel *filename; UILabel *currenttime; UILabel *duration; CALevelMeter *lvlmeter_in; UILabel *repeatfrom; UIButton *repeatbtn; UISlider *volumeslider; UILabel *repeatto; UIButton *rewbutton; UIButton *ffwbutton; UIButton *playbutton;
iphone avtouchcontroller.h Application 4.., xib,,. AppDelegate ViewController. import. #import <UIKit/UIKit.h> #import <AudioToolbox/AudioToolbox.h> #import <AVFoundation/AVFoundation.h>. IBOutlet UILabel *filename; IBOutlet UIButton *playbutton; IBOutlet UIButton *ffwbutton; IBOutlet UIButton *rewbutton; IBOutlet UISlider *volumeslider; IBOutlet UISlider *progressbar; IBOutlet UILabel *currenttime; IBOutlet UILabel *duration; IBOutlet UILabel *repeatfrom; IBOutlet UILabel *repeatto; IBOutlet UIButton *repeatbtn; IBOutlet CALevelMeter *lvlmeter_in; AVAudioPlayer *player; UIImage *playbtnbg; UIImage *pausebtnbg; NSTimer *updatetimer; NSTimer *rewtimer; NSTimer *ffwtimer; NSMutableArray *soundfiles; NSTimeInterval repeata,repeatb; kselectstate repeatstate; - (IBAction)playButtonPressed:(UIButton*)sender; // - (IBAction)rewButtonPressed:(UIButton*)sender; // - (IBAction)rewButtonReleased:(UIButton*)sender; // - (IBAction)ffwButtonPressed:(UIButton*)sender; // - (IBAction)ffwButtonReleased:(UIButton*)sender; // - (IBAction)volumeSliderMoved:(UISlider*)sender; // - (IBAction)progressSliderMoved:(UISlider*)sender; // - (IBAction)onRepeat; // avtouchcontroller.h avtouch, AudioToolbox, AVFoundation import #import <UIKit/UIKit.h> #import <AudioToolbox/AudioToolbox.h> #import <AVFoundation/AVFoundation.h> // typedef enum TimeNone=0,TimeFromSelected,TimeToSelected kselectstate; avtouchcontroller.h NSObject @interface avtouchcontroller : NSObject <UIPickerViewDelegate, AVAudioPlayerDelegate> // IB IBOutlet UILabel *filename; IBOutlet UIButton *playbutton;... // UI - (IBAction)playButtonPressed:(UIButton*)sender; - (IBAction)rewButtonPressed:(UIButton*)sender;... @property (nonatomic, retain)uilabel @property (nonatomic, retain)uibutton... @end *filename; *playbutton; value @property @property value setter/getter
xib iphone Application nib(.xib) avtouchcontroller. /, /,,,. avtouch.. - xib - - - 5., View Controller controller,. awakefromnib,.
nib(.xib) awakefromnib : AVAudioPlayer UI,, - (void)awakefromnib // AVAudioPlayer soundfiles = [[NSMutableArray alloc] initwithcapacity:3]; avtouchcontroller.mm playbtnbg = [[UIImage imagenamed:@"play.png"] retain]; pausebtnbg = [[UIImage imagenamed:@"pause.png"] retain]; [playbutton setimage:playbtnbg forstate:uicontrolstatenormal]; // updatetimer = nil; rewtimer = nil; AVAudioPlayer fileurl ffwtimer = nil;... // NSURL *fileurl = [[NSURL alloc] initfileurlwithpath: [[NSBundle mainbundle] pathforresource:@"mu02" oftype:@"mp3"]]; self.player = [[AVAudioPlayer alloc] initwithcontentsofurl:fileurl error:nil]; if (self.player) //, filename.text = [NSString stringwithformat: @"%@ (%d ch.)", [[player.url relativepath] lastpathcomponent], player.numberofchannels, nil]; [self updateviewforplayerinfo:player]; // [self updateviewforplayerstate:player]; // / player.numberofloops = 1; // player.delegate = self; // OSStatus result = AudioSessionInitialize(NULL, NULL, NULL, NULL); //... [fileurl release]; iphone Application 6.. 1, /. :,. currenttime.text = [NSString stringwithformat:@"%d:%02d", (int)p.currenttime / 60, (int)p.currenttime % 60, nil]; progressbar.value = p.currenttime; //progressbar,.. if (p.playing) // [playbutton setimage:((p.playing == YES)? pausebtnbg : playbtnbg) forstate:uicontrolstatenormal]; [lvlmeter_in setplayer:p]; updatetimer = [NSTimer scheduledtimerwithtimeinterval:.01 target:self selector:@selector(updatecurrenttime) userinfo:p repeats:yes]; else // [playbutton setimage:((p.playing == YES)? pausebtnbg : playbtnbg) forstate:uicontrolstatenormal]; [lvlmeter_in setplayer:nil]; updatetimer = nil;
: - avtouchcontroller.mm // -(void)updatecurrenttimeforplayer:(avaudioplayer *)p // ( : ) currenttime.text = [NSString stringwithformat:@"%d:%02d", (int)p.currenttime / 60, (int)p.currenttime % 60, nil]; progressbar.value = p.currenttime; //progressbar // / - (void)updateviewforplayerstate:(avaudioplayer *)p [self updatecurrenttimeforplayer:p]; if (updatetimer) [updatetimer invalidate]; if (p.playing) // [playbutton setimage:((p.playing == YES)? pausebtnbg : playbtnbg) forstate:uicontrolstatenormal]; [lvlmeter_in setplayer:p]; updatetimer = [NSTimer scheduledtimerwithtimeinterval:.01 target:self selector:@selector(updatecurrenttime) userinfo:p repeats:yes]; else // [playbutton setimage:((p.playing == YES)? pausebtnbg : playbtnbg) forstate:uicontrolstatenormal]; [lvlmeter_in setplayer:nil]; updatetimer = nil; 7., iphone. void RouteChangeListener( void * inclientdata, AudioSessionPropertyIDinID, UInt32 indatasize, const void * indata) avtouchcontroller* This = (avtouchcontroller*)inclientdata; if (inid == kaudiosessionproperty_audioroutechange) CFDictionaryRef routedict = (CFDictionaryRef)inData; NSNumber* reasonvalue = (NSNumber*)CFDictionaryGetValue (routedict, CFSTR(kAudioSession_AudioRouteChangeKey_Reason)); int reason = [reasonvalue intvalue]; if (reason == kaudiosessionroutechangereason_olddeviceunavailable) [This pauseplaybackforplayer:this.player]; avtouchcontroller.mm
8. avtouch :,. //. - (void)audioplayerdidfinishplaying:(avaudioplayer *)p successfully:(bool)flag avtouchcontroller.mm if (flag == NO) NSLog(@"Playback finished unsuccessfully"); [p setcurrenttime:0.]; [self updateviewforplayerstate:p]; p, flag yes, no //. - (void)audioplayerdecodeerrordidoccur:(avaudioplayer *)p error:(nserror *)error NSLog(@"ERROR IN DECODE: %@\n", error); // - (void)audioplayerbegininterruption:(avaudioplayer *)p NSLog(@"Interruption begin. Updating UI for new state"); // the object has already been paused, we just need to update UI [self updateviewforplayerstate:p]; // - (void)audioplayerendinterruption:(avaudioplayer *)p NSLog(@"Interruption ended. Resuming playback"); [self startplaybackforplayer:p]; p, error p p / /
iphone / Application. 9. / IBAction.,. if (player.playing == YES) [self pauseplaybackforplayer: player]; else [self startplaybackforplayer: player];,. -(void)pauseplaybackforplayer:(avaudioplayer*)p -(void)startplaybackforplayer:(avaudioplayer*)p < > < > / / : -, - // - (IBAction)playButtonPressed:(UIButton *)sender if (player.playing == YES) // [self pauseplaybackforplayer: player]; // else [self startplaybackforplayer: player]; // avtouchcontroller.mm // -(void)pauseplaybackforplayer:(avaudioplayer*)p [p pause]; [self updateviewforplayerstate:p]; // -(void)startplaybackforplayer:(avaudioplayer*)p if ([p play]) [self updateviewforplayerstate:p]; else NSLog(@"Could not play %@\n", p.url); / p player
/ (playbuttonpressed ) startplaybackforplayer / pauseplaybackforplayer updateviewforplayerstate updatecurrenttimeforplayer avtouchappdelegate avtouchcontroller applicationdidfinishlaunching: awakefromnib RouteChangeListener viewcontroller : window soundfiles : fileurl :,, dealloc: viewcontroller, window release player : AVAudioPlayer playbuttonpressed: player player startplaybackforplayer: pauseplaybackforplayer: updateviewforplayerinfo: p p avtouchviewcontroller updateviewforplayerstate: Controller : UI updatecurrenttimeforplayer: updatecurrenttime iphone / Application 10. /. / IBAction. rewtimer =[NSTimer scheduledtimerwithtimeinterval:skip_interval target:self selector:@selector(rewind) userinfo:player repeats:yes]; rewtimer = nil; rewtimer =[NSTimer scheduledtimerwithtimeinterval:skip_interval target:self selector:@selector(ffwd) userinfo:player repeats:yes]; rewtimer = nil; /. - (void)rewind - (void)ffwd < / >
/ / : / /, nil avtouchcontroller.mm // - (IBAction)rewButtonPressed:(UIButton *)sender if (rewtimer) [rewtimer invalidate]; rewtimer = [NSTimer scheduledtimerwithtimeinterval:skip_interval target:self selector:@selector(rewind) userinfo:player repeats:yes]; // - (IBAction)rewButtonReleased:(UIButton *)sender if (rewtimer) [rewtimer invalidate]; rewtimer = nil; // - (void)rewind AVAudioPlayer *p = rewtimer.userinfo; p.currenttime-= SKIP_TIME; [self updatecurrenttimeforplayer:p]; // - (IBAction)ffwButtonPressed:(UIButton *)sender if (ffwtimer) [ffwtimer invalidate]; ffwtimer = [NSTimer scheduledtimerwithtimeinterval:skip_interval target:self selector:@selector(ffwd) userinfo:player repeats:yes]; // - (IBAction)ffwButtonReleased:(UIButton *)sender if (ffwtimer) [ffwtimer invalidate]; ffwtimer = nil; // - (void)ffwd AVAudioPlayer *p = ffwtimer.userinfo; p.currenttime+= SKIP_TIME; [self updatecurrenttimeforplayer:p]; / / (rewbuttonpressed/ffwbuttonpressed ) -rewtimer / -ffwtimer -rewind / -ffwd updatecurrenttimeforplayer avtouchappdelegate avtouchcontroller applicationdidfinishlaunching: awakefromnib RouteChangeListener viewcontroller : window soundfiles : fileurl :,, ffwbuttonreleased: ffwbuttonpressed: dealloc: viewcontroller, window release player : AVAudioPlayer rewbuttonpressed: rewtimer rewbuttonreleased: updateviewforplayerinfo: player nil ffwtimer avtouchviewcontroller updateviewforplayerstate: p rewind: player nil Controller : UI updatecurrenttimeforplayer: updatecurrenttime p ffwd:
iphone Application 11. IBAction. volume. player.volume = [sender value]; < > // volumeslider player volume sender - (IBAction)volumeSliderMoved:(UISlider *)sender player.volume = [sender value]; avtouchcontroller.mm (volumeslidermoved ) player avtouchappdelegate avtouchcontroller applicationdidfinishlaunching: awakefromnib RouteChangeListener viewcontroller : window soundfiles : fileurl :,, dealloc: viewcontroller, window release player : AVAudioPlayer volumeslidermoved: avtouchviewcontroller Controller : UI updateviewforplayerinfo: updateviewforplayerstate: updatecurrenttimeforplayer: updatecurrenttime
iphone Application 12. IBAction.. player.currenttime = sender.value; [self updatecurrenttimeforplayer:player]; < > // progressslider player currenttime sender - (IBAction)progressSliderMoved:(UISlider *)sender player.currenttime = sender.value; [self updatecurrenttimeforplayer:player]; (progressslidermoved ) avtouchcontroller.mm player updatecurrenttimeforplayer avtouchappdelegate avtouchcontroller applicationdidfinishlaunching: awakefromnib RouteChangeListener viewcontroller : window soundfiles : fileurl :,, dealloc: viewcontroller, window release player : AVAudioPlayer progressslidermoved: avtouchviewcontroller updateviewforplayerinfo: updateviewforplayerstate: player Controller : UI updatecurrenttimeforplayer: updatecurrenttime
iphone Application 13. (from~to). from, to, reset IBAction. avtouchcontroller.h kselectstate.. typedef enum TimeNone=0, TimeFromSelected, TimeToSelected kselectstate; onrepeat. if (repeatstate == TimeNone) From else if (repeatstate == TimeFromSelected) to else reset if (repeatstate == TimeToSelected && player.currenttime >= repeatb) < > : (from~to) avtouchcontroller.mm - (IBAction)onRepeat if (repeatstate == TimeNone) // A.. From repeata repeata = player.currenttime; [repeatfrom settext:[nsstring stringwithformat:@"from : %.2d:%.2d",((int)repeatA / 60), ((int)repeata%60)]]; [repeatto settext:@"to : "]; [repeatbtn settitle:@"repeat to" forstate:uicontrolstatenormal]; [repeatbtn settitle:@"repeat to" forstate:uicontrolstatehighlighted]; repeatstate = TimeFromSelected; else if (repeatstate == TimeFromSelected) // B. To repeatb = player.currenttime; [repeatto settext:[nsstring stringwithformat:@"to : %.2d:%.2d",((int)repeatB / 60), ((int)repeatb%60)]]; [repeatbtn settitle:@"reset repeat" forstate:uicontrolstatenormal]; [repeatbtn settitle:@"reset repeat" forstate:uicontrolstatehighlighted]; repeatstate = TimeToSelected; else // [repeatto settext:@"to : "]; [repeatfrom settext:@"from : "]; [repeatbtn settitle:@"repeat from" forstate:uicontrolstatenormal]; [repeatbtn settitle:@"repeat from" forstate:uicontrolstatehighlighted]; repeatstate = TimeNone; if (repeatstate == TimeToSelected && player.currenttime >= repeatb) player.currenttime = repeata;
(volumeslidermoved ) player avtouchappdelegate avtouchcontroller applicationdidfinishlaunching: awakefromnib RouteChangeListener viewcontroller : window soundfiles : fileurl :,, dealloc: viewcontroller, window release player : AVAudioPlayer onrepeat: player avtouchviewcontroller Controller : UI updateviewforplayerinfo: updateviewforplayerstate: updatecurrenttimeforplayer: updatecurrenttime 14. : release. - (void)dealloc [super dealloc]; [filename release]; [playbutton release]; [ffwbutton release]; [rewbutton release]; [volumeslider release]; [progressbar release]; [currenttime release]; [duration release]; [lvlmeter_in release]; [updatetimer release]; [player release]; [playbtnbg release]; [pausebtnbg release]; avtouchcontroller.mm
Xib - 15.. control Controller Inspector.