iOS Development outreach-Music playback
A simple explanation
Music playback uses a class called Avaudioplayer, which can be used to play local music files on your phone.
Attention:
(1) This class (Avaudioplayer) can only be used to play local audio.
(2) shorter time (called sound effects) is created using Audioservicescreatesystemsoundid, while local time is longer (called music) using the Avaudioplayer class.
Second, code example
The Avaudioplayer class relies on the Avfoundation framework, so you must first import the Avfoundation framework and include its header file (including the primary header file) before using the class.
Import the necessary audio files that need to be played into the project.
code example:
1//2// YYVIEWCONTROLLER.M 3// 15-Play Music 4//5 6 #import "YYViewController.h" 7 #import <AVFOUNDATION/AVF Oundation.h> 8 9 @interface yyviewcontroller () @end12 @implementation YYViewController14-(void) Viewdi dLoad16 { [super viewdidload];18 }20-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event22 { //1. audio file URL path nsurl *url=[[nsbundle mainbundle]urlforresource:@ "235319.mp3" withextension: Nil];26 //2. Creating a player (note: A avaudioplayer can play only one URL). avaudioplayer *audioplayer=[[avaudioplayer Alloc]initwithcontentsofurl:url error:nil];29 //3. Buffer [Audioplayer preparetoplay];32 33 //4. Play [Audioplayer play];35}36 Notoginseng @end
Code Description: Run the program, click the emulator interface, but not able to play audio files, because the code created in the Avaudioplayer player is a local variable, should be adjusted to the global properties.
You can adjust the code as follows to play audio:
1 #import "YYViewController.h" 2 #import <AVFoundation/AVFoundation.h> 3 4 @interface Yyviewcontroller () 5 @pr Operty (Nonatomic,strong) Avaudioplayer *audioplayer; 6 @end 7 8 @implementation Yyviewcontroller 9-(void) ViewDidLoad11 { [super viewdidload];13 14}15 16-( void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event17 {//1 . The URL path of the audio file, nsurl *url=[[ NSBundle mainbundle]urlforresource:@ "235319.mp3" withextension:nil];21 //2. Create player (note: A avaudioplayer can only play one URL) self.audioplayer=[[avaudioplayer alloc]initwithcontentsofurl:url Error: Nil];24 //3. Cushion [Self.audioplayer preparetoplay];27 //4. Play [ Self.audioplayer play];30}31 @end
Note: A avaudioplayer can only play one URL, and if you want to play multiple files, you have to create multiple players.
Iii. Related Notes
Create a new project and put three buttons in the storyboard to control the playback, pause, and stop of the music, respectively.
The program code is as follows:
1 #import "YYViewController.h" 2 #import <AVFoundation/AVFoundation.h> 3 4 @interface Yyviewcontroller () 5 @pr Operty (Nonatomic,strong) Avaudioplayer *player; 6-(ibaction) play; 7-(ibaction) pause; 8-(ibaction) stop; 9 @end10 @implementation YYViewController12-(void) ViewDidLoad14 { viewdidload];16 [super] //1. URL path of audio file nsurl *url=[[nsbundle mainbundle]urlforresource:@ "235319.mp3" withextension:nil];19 / /2. Creating a player (note: A avaudioplayer can only play one URL) self.player=[[avaudioplayer alloc]initwithcontentsofurl:url Error: Nil];22 //3. Buffer [Self.player preparetoplay];25}27-(ibaction) play {+/ /Start play/Resume 30 [Self.player play];31}32-(ibaction) Pause { //pause] [Self.player pause];36}37-(ibaction) Stop { / /Stop Max //NOTE: If you click Stop, then be sure to let the player to create a new, otherwise there will be some inexplicable problems of its face [Self.player stop];42}43 @end
Note: If you click "Stop", then be sure to create a new player, otherwise there will be an inexplicable problem.
After you click Stop, the player will not actually be able to continue to use it, and if you continue to use it, then some of the things that follow are out of control.
Recommended Code:
1 #import "YYViewController.h" 2 #import <AVFoundation/AVFoundation.h> 3 4 @interface Yyviewcontroller () 5 @prope Rty (Nonatomic,strong) Avaudioplayer *player; 6-(ibaction) play; 7-(ibaction) pause; 8-(ibaction) stop; 9 @end10 @implementation YYViewController12 #pragma mark-lazy load-(Avaudioplayer *) player15 {(_player==nil) {17 18//1. URL path to the audio file Nsurl *url=[[nsbundle mainbundle]urlforresource:@ "235319.mp3" Withextensio n:nil];20 21//2. Create player (note: One avaudioplayer can only play one URL). Self.player=[[avaudioplayer Alloc]initwithco Ntentsofurl:url error:nil];23 24//3. Buffer [Self.player preparetoplay];26}27 return _playe r;28}29-(void) viewDidLoad31 {+ [super viewdidload];33}34-(ibaction) Play {36//start playing/continue to play the PNS [SELF.P Layer play];38}39-(ibaction) Pause {41//Pause [Self.player pause];43}44-(ibaction) Stop {46//Stop 47 Note: If you click Stop, be sure to let the player to create a new, otherwise there will be some inexplicableThe problem with its face is [Self.player stop];49 self.player=nil;50}51 @end
If you click the Stop button, the concert starts playing from the beginning.
Four, play multiple files
Click, URL, and press common to build a view.
It can be found that this URL is read-only and therefore can only be set by Initwithcontentsofurl, which means that only one audio file can be played by a player object.
So how do you play multiple audio files?
Consider encapsulating a tool class that plays music, and the next article will show you how to do it.
iOS Development outreach-Music playback