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.M3 //15-Play Music4 //5 6 #import "YYViewController.h"7 #import<AVFoundation/AVFoundation.h>8 9 @interfaceYyviewcontroller ()Ten One @end A - @implementationYyviewcontroller - the- (void) Viewdidload - { - [Super Viewdidload]; - + } - +-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event A { at - //1. URL path to the audio file -Nsurl *url=[[nsbundle Mainbundle]urlforresource:@"235319.mp3"Withextension:nil]; - - //2. Create player (note: One avaudioplayer can only play one URL) -Avaudioplayer *audioplayer=[[Avaudioplayer Alloc]initwithcontentsofurl:url Error:nil]; in - //3. Buffering to [Audioplayer Preparetoplay]; + - //4. Play the [Audioplayer play]; * } $ Panax 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 @interfaceYyviewcontroller ()5@property (nonatomic,strong) Avaudioplayer *Audioplayer;6 @end7 8 @implementationYyviewcontroller9 Ten- (void) Viewdidload One { A [Super Viewdidload]; - - } the --(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event - { - + //1. URL path to the audio file -Nsurl *url=[[nsbundle Mainbundle]urlforresource:@"235319.mp3"Withextension:nil]; + A //2. Create player (note: One avaudioplayer can only play one URL) atSelf.audioplayer=[[Avaudioplayer Alloc]initwithcontentsofurl:url Error:nil]; - - //3. Buffering - [Self.audioplayer Preparetoplay]; - - //4. Play in [Self.audioplayer play]; - } to + @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 @interfaceYyviewcontroller ()5@property (nonatomic,strong) Avaudioplayer *player;6-(ibaction) play;7-(ibaction) pause;8-(ibaction) stop;9 @endTen One @implementationYyviewcontroller A -- (void) Viewdidload - { the [Super Viewdidload]; - - //1. URL path to the audio file -Nsurl *url=[[nsbundle Mainbundle]urlforresource:@"235319.mp3"Withextension:nil]; + - //2. Create player (note: One avaudioplayer can only play one URL) +Self.player=[[Avaudioplayer Alloc]initwithcontentsofurl:url Error:nil]; A at //3. Buffering - [Self.player Preparetoplay]; - - } - --(ibaction) play { in //Start play/Resume playback - [Self.player play]; to } + --(ibaction) Pause { the //Pause * [Self.player pause]; $ }Panax Notoginseng --(ibaction) Stop { the //Stop + //Note: If you click Stop, make sure the player is newly created, otherwise there will be some inexplicable problems A [Self.player stop]; the } + @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 @interfaceYyviewcontroller ()5@property (nonatomic,strong) Avaudioplayer *player;6-(ibaction) play;7-(ibaction) pause;8-(ibaction) stop;9 @endTen One @implementationYyviewcontroller A - #pragma mark-lazy loading --(Avaudioplayer *) Player the { - if(_player==Nil) { - - //1. URL path to the audio file +Nsurl *url=[[nsbundle Mainbundle]urlforresource:@"235319.mp3"Withextension:nil]; - + //2. Create player (note: One avaudioplayer can only play one URL) ASelf.player=[[Avaudioplayer Alloc]initwithcontentsofurl:url Error:nil]; at - //3. Buffering - [Self.player Preparetoplay]; - } - return_player; - } in -- (void) Viewdidload to { + [Super Viewdidload]; - } the *-(ibaction) play { $ //Start play/Resume playbackPanax Notoginseng [Self.player play]; - } the +-(ibaction) Pause { A //Pause the [Self.player pause]; + } - $-(ibaction) Stop { $ //Stop - //Note: If you click Stop, make sure the player is newly created, otherwise there will be some inexplicable problems - [Self.player stop]; the Self.player=nil; - }Wuyi @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.