IOS audio function development (1) sorting
To add audio to the project, first import the audio framework AVFoundation. framework
Create a new class that inherits from UIViewController. I will name it FirstVC here.
First, initialize the Root View in AppDelegate. m.
1 // 2 // AppDelegate. m 3 // YinPinShiPin 4 // 5 // Created by chulijian on 14-11-8. 6 // Copyright (c) 2014 company. all rights reserved. 7 // 8 9 # import "AppDelegate. h "10 # import" FirstVC. h "11 @ implementation AppDelegate12 13 18 19-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions20 {21 self. window = [[[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds] autorelease]; 22 // Override point for customization after application launch.23 24 FirstVC * firstVC = [[FirstVC alloc] init]; 25 self. window. rootViewController = firstVC; 27 28 self. window. backgroundColor = [UIColor whiteColor]; 29 [self. window makeKeyAndVisible]; 30 return YES; 31}
Then import the AVFoundation framework to FirstVC. h.
1 // 2 // FirstVC.h 3 // YinPinShiPin 4 // 5 // Created by chulijian on 14-11-8.
6 // Copyright (c) 2014 company. All rights reserved.
7 //
8 //
9 //
#import
10
11 // if you want to use encapsulated audio classes, import boxes, and import Class header files, it is indispensable;
12 #import
13
14 @interface FirstVC : UIViewController
15 {
16 AVAudioPlayer * avAudioPlayer; // player player17
UIProgressView * progressV; // playback progress 18
UISlider * volumeSlider; // voice control 19
NSTimer * timer; // monitors the audio playback progress of 20
}
21
22 @end
Then enter the code in the viewDidLoad method in FirstVC. m. You need to import an audio file to play the video. Just like adding an image, drag it to the project.
1-(void) viewDidLoad 2 {3 [super viewDidLoad]; 4 // Do any additional setup after loading the view. 5 // initialize three buttons 6 UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; 7 [button setFrame: CGRectMake (100,100, 60, 40)]; 8 [button setTitle: @ "Play" forState: UIControlStateNormal]; 9 [button addTarget: self action: @ selector (play) forControlEvents: UIControlEventTouchUpInside]; 10 [self. view addSubview: button]; 11 12 UIButton * button1 = [UIButton buttonWithType: callback]; 13 [button1 setFrame: CGRectMake (100,150, 60, 40)]; 14 [button1 setTitle: @ "pause" forState: UIControlStateNormal]; 15 [button1 addTarget: self action: @ selector (pause) forControlEvents: UIControlEventTouchUpInside]; 16 [self. view addSubview: button1]; 17 18 UIButton * button2 = [UIButton buttonWithType: Large]; 19 [button2 setFrame: CGRectMake (100,200, 60, 40)]; 20 [button2 setTitle: @ "stop" forState: UIControlStateNormal]; 21 [button2 addTarget: self action: @ selector (stop) forControlEvents: UIControlEventTouchUpInside]; 22 [self. view addSubview: button2]; 23 24 // read the audio file from the budle path light music-Saks go home this file name is your song name, mp3 is your audio format 25 NSString * string = [[NSBundle mainBundle] pathForResource: @ "light music-Saxophone going home" ofType: @ "mp3"]; 26 // convert the audio file to the url format 27 NSURL * url = [NSURL fileURLWithPath: string]; 28 // initialize the audio class and add the playback file 29 avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: url error: nil]; 30 // set the proxy 31 avAudioPlayer. delegate = self; 32 33 // set the initial volume to 34 // avAudioPlayer. volume = 1; 35 36 // set the number of times the music is played-1 to keep repeating 37 avAudioPlayer. numberOfLoops =-1; 38 39 // pre-play 40 [avAudioPlayer prepareToPlay]; 41 42 // initialize a playback progress bar 43 progressV = [[UIProgressView alloc] initWithFrame: CGRectMake (20, 50,200, 20)]; 44 [self. view addSubview: progressV]; 46 47 // use nstmer to monitor the audio playback progress 48 timer = [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self49 selector: @ selector (playProgress) 50 userInfo: nil repeats: YES]; 51 // initialize volume control 52 volumeSlider = [[UISlider alloc] initWithFrame: CGRectMake (20, 70,200, 20)]; 53 [volumeSlider addTarget: self action: @ selector (volumeChange) 54 forControlEvents: UIControlEventValueChanged]; 55 // set the minimum volume of 56 volumeSlider. minimumValue = 0.0f; 57 // set the maximum volume of 58 volumeSlider. maximumValue = 10.0f; 59 // The initial volume of 60 volumeSlider. value = 5.0f; 61 62 [self. view addSubview: volumeSlider]; 64 65 // voice switch control (mute) 66 UISwitch * swith = [[UISwitch alloc] initWithFrame: CGRectMake (100, 20, 60, 40)]; 67 [swith addTarget: self action: @ selector (onOrOff :) forControlEvents: UIControlEventValueChanged]; 68 // The default status is 69 swith. on = YES; 70 [self. view addSubview: swith]; 72}
The corresponding custom method code is as follows:
1 // play 2-(void) play 3 {4 [avAudioPlayer play]; 5} 6 // pause 7-(void) pause 8 {9 [avAudioPlayer pause]; 10} 11 // stop 12-(void) stop13 {14 avAudioPlayer. currentTime = 0; // set the current playback time to 015 [avAudioPlayer stop]; 16} 17 // playback progress bar 18-(void) playProgress19 {20 // assign a value to progressview based on the percentage of audio playback duration; 21 progressV. progress = avAudioPlayer. currentTime/avAudioPlayer. duration; 22} 23 // voice switch (mute or not) 24-(void) onOrOff :( UISwitch *) sender25 {26 avAudioPlayer. volume = sender. on; 27} 28 29 // playback volume control 30-(void) volumeChange31 {32 avAudioPlayer. volume = volumeSlider. value; 33} 34 35 // The method called when the playback is complete (the method in the proxy). You need to set the proxy to call 36-(void) audioPlayerDidFinishPlaying :( AVAudioPlayer *) player successfully :( BOOL) flag37 {38 [timer invalidate]; // nstdate pause invalidate to make... invalid; 39}
Demo will be sorted and uploaded later
You can also define a UISlider to control the playback progress.