One, simple explanation
1. A brief description of music playback
(1) Music playback using a class called Avaudioplayer
(2) Avaudioplayer common methods
Load music files
Copy Code code as follows:
-(ID) Initwithcontentsofurl: (nsurl *) URL error: (nserror * *) Outerror;
-(ID) Initwithdata: (NSData *) Data error: (NSERROR * *) Outerror;
Ready to play (buffer, improve the fluency of playback)-(BOOL) Preparetoplay;
Play (played asynchronously)-(BOOL) plays;
Pause-(void) pause;
Stop-(void) stop;
is playing
Copy Code code as follows:
@property (readonly, getter=isplaying) BOOL playing;
Duration
Copy Code code as follows:
@property (readonly) nstimeinterval duration;
The current playback bit
Copy Code code as follows:
@property Nstimeinterval currenttime;
Number of playback (-1 for Infinite loop playback, other representatives play numberofloops+1 times @property Nsinteger numberofloops;
Volume
Copy Code code as follows:
Whether change rate is allowed
Copy Code code as follows:
@property BOOL enablerate;
Playback rate (1 is the normal rate, 0.5 is the general rate, 2 is the double rate)
Copy Code code as follows:
How many channels are there?
Copy Code code as follows:
@property (readonly) Nsuinteger numberofchannels;
2. Play multiple music files
Description: If you want to play multiple music files, the most fool way is to create a number of global players to play the corresponding music files, but this method does not apply to the need to play a large number of files.
Another approach is to encapsulate a tool class that plays music files.
To implement steps for encapsulating a tool class:
Creates a new class that inherits from the NSObject class. Provides three external interfaces:
respectively:
Play Music (parameter: file name, return value: BOOL)
Pause Music (parameter: file name)
Stop Music (parameter: file name)
The code in the tool class is designed as follows:
YYAudioTool.h file
Copy Code code as follows:
//
YYAudioTool.h
17-the playback of multiple music files
//
Created by Apple on 14-8-9.
Copyright (c) 2014 Yangyong. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface Yyaudiotool:nsobject
/**
* Play music files
*/
+ (BOOL) Playmusic: (NSString *) filename;
/**
* Pause Playback
*/
+ (void) Pausemusic: (NSString *) filename;
/**
* Play music files
*/
+ (void) Stopmusic: (NSString *) filename;
@end
YYAUDIOTOOL.M file
Copy Code code as follows:
//
yyaudiotool.m
17-the playback of multiple music files
//
Created by Apple on 14-8-9.
Copyright (c) 2014 Yangyong. All rights reserved.
//
#import "YYAudioTool.h"
@implementation Yyaudiotool
/**
* Store all the music players
*/
Static Nsmutabledictionary *_musices;
+ (Nsmutabledictionary *) musices
{
if (_musices==nil) {
_musices=[nsmutabledictionary dictionary];
}
return _musices;
}
/**
* Play Music
*/
+ (BOOL) Playmusic: (NSString *) filename
{
if (!filename) return no;//If no file name is passed in, go straight back
1. Remove the corresponding player
Avaudioplayer *player=[self Musices][filename];
2. If the player is not created, initialize it
if (!player) {
2.1 The URL of the audio file
Nsurl *url=[[nsbundle Mainbundle]urlforresource:filename Withextension:nil];
if (!url) return no;//if the URL is empty, go straight back
2.2 Creating the viewer
Player=[[avaudioplayer Alloc]initwithcontentsofurl:url Error:nil];
2.3 Buffers
if (![ Player Preparetoplay]) return no;//If the buffer fails, then go straight back
2.4 In a dictionary
[Self musices] [Filename]=player;
}
3. Play
if (![ Player IsPlaying]) {
If it is not currently playing, play
return [player play];
}
Return yes;//is playing, then returns YES
}
+ (void) Pausemusic: (NSString *) filename
{
if (!filename) return;//If no file name is passed in, it is returned directly
1. Remove the corresponding player
Avaudioplayer *player=[self Musices][filename];
2. Suspension
[Player pause];//if Palyer is empty, that's equivalent to [nil pause], so you don't have to do this
}
+ (void) Stopmusic: (NSString *) filename
{
if (!filename) return;//If no file name is passed in, it is returned directly
1. Remove the corresponding player
Avaudioplayer *player=[self Musices][filename];
2. Stop
[Player stop];
3. Remove the viewer from the dictionary
[[Self musices] removeobjectforkey:filename];
}
@end
Test program:
Drag the control in the storyboard and connect it to make control.
Import music footage that is available for playback.
The code for the test program is designed as follows:
Copy Code code as follows:
//
Yyviewcontroller.m
17-the playback of multiple music files
//
Created by Apple on 14-8-9.
Copyright (c) 2014 Yangyong. All rights reserved.
//
#import "YYViewController.h"
#import "YYAudioTool.h"
@interface Yyviewcontroller ()
-(ibaction) play;
-(ibaction) pause;
-(ibaction) stop;
-(ibaction) next;
Use an array to save all the music files
@property (Nonatomic,strong) Nsarray *songs;
Log the current index with an int attribute
@property (nonatomic,assign) int currentindex;
@end
Copy Code code as follows:
@implementation Yyviewcontroller
#pragma mark-lazy Load
-(Nsarray *) Songs
{
if (_songs==nil) {
self.songs=@[@ "235319.mp3", @ "309769.mp3", @ "120125029.mp3"];
}
return _songs;
}
-(void) viewdidload
{
[Super Viewdidload];
}
-(ibaction) play {
Start play/Resume Playback
[Yyaudiotool Playmusic:self.songs[self.currentindex]];
}
-(ibaction) Pause {
Pause Playback
[Yyaudiotool Pausemusic:self.songs[self.currentindex]];
}
-(ibaction) Stop {
Stop playing
[Yyaudiotool Stopmusic:self.songs[self.currentindex]];
}
Play the next song
-(ibaction) Next {
1. Stop Current playback first
[Self stop];
2. Set Current index +1
self.currentindex++;
if (Self.currentindex>=self.songs.count) {
self.currentindex=0;
}
3. Play Music
[Self play];
}
@end
Second, the tool class for the transformation, so that it can play audio files
Description
Sound effects only create, play, and Destroy (stop) three actions, because the sound effects are generally very short, so there is no method of pausing.
Add the playback of the sound file to the tool class and implement the following code:
YYAudioTool.h file
Copy Code code as follows:
//
YYAudioTool.h
17-the playback of multiple music files
//
Created by Apple on 14-8-9.
Copyright (c) 2014 Yangyong. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface Yyaudiotool:nsobject
/**
* Play music files
*/
+ (BOOL) Playmusic: (NSString *) filename;
/**
* Pause Playback
*/
+ (void) Pausemusic: (NSString *) filename;
/**
* Play music files
*/
+ (void) Stopmusic: (NSString *) filename;
/**
* Play sound files
*/
+ (void) PlaySound: (NSString *) filename;
/**
* Destroy sound effects
*/
+ (void) Disposesound: (NSString *) filename;
@end
YYAUDIOTOOL.M file
//
yyaudiotool.m
17-the playback of multiple music files
//
Created by Apple on 14-8-9.
Copyright (c) 2014 Yangyong. All rights reserved.
//
#import "YYAudioTool.h"
@implementation Yyaudiotool
/**
* Store all the music players
*/
Static Nsmutabledictionary *_musicplayers;
+ (Nsmutabledictionary *) musicplayers
{
if (_musicplayers==nil) {
_musicplayers=[nsmutabledictionary dictionary];
}
return _musicplayers;
}
/**
* Store all the sound effects ID
*/
Static Nsmutabledictionary *_soundids;
+ (Nsmutabledictionary *) soundids
{
if (_soundids==nil) {
_soundids=[nsmutabledictionary dictionary];
}
return _soundids;
}
/**
* Play Music
*/
+ (BOOL) Playmusic: (NSString *) filename
{
if (!filename) return no;//If no file name is passed in, go straight back
1. Remove the corresponding player
Avaudioplayer *player=[self Musicplayers][filename];
2. If the player is not created, initialize it
if (!player) {
2.1 The URL of the audio file
Nsurl *url=[[nsbundle Mainbundle]urlforresource:filename Withextension:nil];
if (!url) return no;//if the URL is empty, go straight back
2.2 Creating the viewer
Player=[[avaudioplayer Alloc]initwithcontentsofurl:url Error:nil];
2.3 Buffers
if (![ Player Preparetoplay]) return no;//If the buffer fails, then go straight back
2.4 In a dictionary
[Self musicplayers] [Filename]=player;
}
3. Play
if (![ Player IsPlaying]) {
If it is not currently playing, play
return [player play];
}
Return yes;//is playing, then returns YES
}
+ (void) Pausemusic: (NSString *) filename
{
if (!filename) return;//If no file name is passed in, it is returned directly
1. Remove the corresponding player
Avaudioplayer *player=[self Musicplayers][filename];
2. Suspension
[Player pause];//if Palyer is empty, that's equivalent to [nil pause], so you don't have to do this
}
+ (void) Stopmusic: (NSString *) filename
{
if (!filename) return;//If no file name is passed in, it is returned directly
1. Remove the corresponding player
Avaudioplayer *player=[self Musicplayers][filename];
2. Stop
[Player stop];
3. Remove the viewer from the dictionary
[[Self musicplayers] removeobjectforkey:filename];
}
Play sound
+ (void) PlaySound: (NSString *) filename
{
if (!filename) return;
1. Take out the corresponding sound effects
Systemsoundid soundid=[[self Soundids][filename] unsignedintegervalue];
2. Play sound
2.1 If the sound ID does not exist, then create
if (!soundid) {
The URL of the sound file
Nsurl *url=[[nsbundle Mainbundle]urlforresource:filename Withextension:nil];
if (!url) return;//if the URL does not exist, then return directly
Osstatus status = Audioservicescreatesystemsoundid (__bridge cfurlref) (URL), &soundid);
NSLog (@ "%ld", status);
To be deposited in a dictionary
[Self soundids] [Filename]=@ (Soundid);
}
2.2 With sound ID, play sound
Audioservicesplaysystemsound (Soundid);
}
Destroy sound effects
+ (void) Disposesound: (NSString *) filename
{
If the file name passed in is empty, return directly to the
if (!filename) return;
1. Take out the corresponding sound effects
Systemsoundid soundid=[[self Soundids][filename] unsignedintegervalue];
2. Destruction
if (Soundid) {
Audioservicesdisposesystemsoundid (Soundid);
2.1 After destruction, remove from the dictionary
[[Self soundids]removeobjectforkey:filename];
}
}
@end
Code test:
Code Description:
The printed value is 0 and the playback is successful (because the function is in C + +)