IOS development expansion-playing music

Source: Internet
Author: User

IOS development expansion-playing music
1. Simple Description: a class called AVAudioPlayer is used for playing music files on a mobile phone. Note: (1) this class (AVAudioPlayer) can only be used to play local audio. (2) The short time (called sound effects) is created using AudioServicesCreateSystemSoundID, while the local time (called music) is longer using AVAudioPlayer. Ii. code example AVAudioPlayer class depends on the AVFoundation framework. Therefore, you must first import the AVFoundation framework and include its header file (including the main header file ). Import necessary audio files to the project. Sample Code: Copy code 1 // 2 // YYViewController. m 3 // 15-play music 4 // 5 6 # import "YYViewController. h "7 # import <AVFoundation/AVFoundation. h> 8 9 @ interface YYViewController () 10 11 @ end12 13 @ implementation YYViewController14 15-(void) viewDidLoad16 {17 [super viewDidLoad]; 18 19} 20 21-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event22 {23 24 // 1. url of the audio file 25 NSURL * url = [NSBundle mainBundle] URLForResource: @ "210419sion" withExtension: Nil]; 26 27 // 2. create a player (Note: One AVAudioPlayer can only play one url) 28 AVAudioPlayer * audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: url error: Nil]; 29 30/3. buffer 31 [audioPlayer prepareToPlay]; 32 33 // 4. play 34 [audioPlayer play]; 35} 36 37 @ end copy Code Description: run the program and click the Simulator Interface, but the audio file cannot be played, the reason is that the AVAudioPlayer created in the Code is a local variable and should be changed to a global attribute. You can adjust the Code as follows to play the audio: Copy code 1 # import "YYViewController. h "2 # import <AVFoundation/AVFoundation. h> 3 4 @ interface YYViewController () 5 @ property (nonatomic, strong) AVAudioPlayer * audioplayer; 6 @ end 7 8 @ implementation YYViewController 9 10-(void) viewDidLoad11 {12 [super viewDidLoad]; 13 14} 15 16-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event17 {18 19 // 1. url of the audio file 20 NSURL * url = [NSBu Ndle mainBundle] URLForResource: @ "235319sion" withExtension: Nil]; 21 22 // 2. create a player (Note: One AVAudioPlayer can only play one url) 23 self. audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL: url error: Nil]; 24 25 // 3. buffer 26 [self. audioplayer prepareToPlay]; 27 28 // 4. play 29 [self. audioplayer play]; 30} 31 32 @ end copy code Note: One AVAudioPlayer can only play one url. If you want to play multiple files, you have to create multiple players. 3. Create a new project with three buttons in the storyboard to control the playing, pause, and stop of the music. The program code is as follows: Copy code 1 # import "YYViewController. h "2 # import <AVFoundation/AVFoundation. h> 3 4 @ interface YYViewController () 5 @ property (nonatomic, strong) AVAudioPlayer * player; 6-(IBAction) play; 7-(IBAction) pause; 8-(IBAction) stop; 9 @ end10 11 @ implementation YYViewController12 13-(void) viewDidLoad14 {15 [super viewDidLoad]; 16 17 // 1. url of the audio file 18 NSURL * url = [[NSBundle mainBundle] URLForResource :@ "235319sion" withExtension: Nil]; 19 20 // 2. create a player (Note: One AVAudioPlayer can only play one url) 21 self. player = [[AVAudioPlayer alloc] initWithContentsOfURL: url error: Nil]; 22 23 // 3. buffer 24 [self. player prepareToPlay]; 25 26} 27 28-(IBAction) play {29 // start/Resume playback 30 [self. player play]; 31} 32 33-(IBAction) pause {34 // pause 35 [self. player pause]; 36} 37 38-(IBAction) stop {39 // stop 40 // Note: If you click stop, you must re-create the player, otherwise, some inexplicable problems may occur. 41 [self. player stop]; 42} 43 @ end copy the code. Note: If you click "stop", you must re-create the player. Otherwise, an inexplicable problem may occur. After you click stop, the player can no longer be used. If you continue to use the player, you will not be able to control the following things. Recommended code: Copy code 1 # import "YYViewController. h "2 # import <AVFoundation/AVFoundation. h> 3 4 @ interface YYViewController () 5 @ property (nonatomic, strong) AVAudioPlayer * player; 6-(IBAction) play; 7-(IBAction) pause; 8-(IBAction) stop; 9 @ end10 11 @ implementation YYViewController12 13 # pragma mark-lazy loading 14-(AVAudioPlayer *) player15 {16 if (_ player = Nil) {17 18 // 1. url of the audio file 19 NSURL * url = [NSBundle mainBu Ndle] URLForResource: @ "235319sion" withExtension: Nil]; 20 21 // 2. create a player (Note: One AVAudioPlayer can only play one url) 22 self. player = [[AVAudioPlayer alloc] initWithContentsOfURL: url error: Nil]; 23 24 // 3. buffer 25 [self. player prepareToPlay]; 26} 27 return _ player; 28} 29 30-(void) viewDidLoad31 {32 [super viewDidLoad]; 33} 34 35-(IBAction) play {36 // start playing/continue playing 37 [self. player play]; 38} 39 40-(IBAction) pause {41 // pause 42 [self. Player pause]; 43} 44 45-(IBAction) stop {46 // stop 47 // Note: If you click stop, you must re-create the player, otherwise, some inexplicable problems may occur. 48 [self. player stop]; 49 self. player = Nil; 50} 51 @ end copy code if you click the stop button, the concert starts playing from the beginning.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.