I. Music Playback class Concept
There are several classes under IOS that can support song and sound playback:
- Systemsound
Avaudioplayer #重要 in the Avfoundtion library
Medimpmusicplayercontroller
Common Audio Controls
3. Mpmediapickercontroller Local music library selector
5. Mpvolumeview Playback progress bar
Here is a PPT in explaining several concepts:
Https://ccrma.stanford.edu/~jsanchez/NSSpain.pdf
This tutorial also uses different mechanisms to play the example:
Https://github.com/jsanchezsierra/AudioLab
The design of Sound visualization
If you want the program to output sound, waveform, spectrum, and other effects,
Be sure to take a look at this tutorial:
Ipodvisualizer
Http://www.raywenderlich.com/36475/how-to-make-a-music-visualizer-in-ios
It is a kind of interface that uses Avaudioplayer Averagepowerforchannel to output waveform files.
Mpmusicplayercontroller not found support for this feature
Auriotouch
Apple also gives an example of output: The waveform of the Auriotouch recording data, with the normal waveform file, and the FFT operation to obtain the spectral data. can refer to.
Source code here: https://developer.apple.com/library/prerelease/ios/samplecode/aurioTouch/Introduction/Intro.html
and the updated version (Apple has removed this version)
Https://github.com/caseytcaprice/aurioTouch2
According to
Https://github.com/irtemed88/PitchDetector
Pitchdetector
To draw a more perfect waveform file:
Https://github.com/irtemed88/PitchDetector
Speakhere
Apple's official example shows the real-time recording of the wave opening:
Https://developer.apple.com/library/ios/samplecode/SpeakHere/Introduction/Intro.html
Avtouch
Example of a simpler sound-to-waveform
Https://developer.apple.com/library/ios/samplecode/avTouch/Introduction/Intro.html
Select a system Song file
There are three songs from the music app, one native sandbox with songs, another network song,
Select System Music Library
The third song on the System music Library,
It can be called by the Mpmediapickercontroller class.
-(IBAction) Addpressed: (id) Sender {Mpmediatype mediatype = Mpmediatypemusic; Mpmediapickercontroller *picker = [[Mpmediapickercontroller alloc] initwithmediatypes:mediatype]; Picker.delegate = self; [Picker Setallowspickingmultipleitems:yes]; Picker.prompt = nslocalizedstring (@ "Select items to play", @ "Select items to Play "); [self presentviewcontroller:picker animated:YES completion: nil];}
It returns a Mpmediapickercontrollerdelegate interface via the
Mpmediaitemcollection Selected song column list, just call the following interface for Mpmediapickercontroller to play, as well as the previous, next
[Self.player setQueueWithItemCollection:self.collection];
If it is avaudioplayer to play you need to do a little more. The Mpmediaitemcollection song URL can be taken out, directly to Avaudioplayer can, this URL format similar to
ipod-library://item/item.m4a?id=1529654720874100371
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) collection { MPMediaItem *item = [[collection items] objectAtIndex:0]; NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL]; // Play the item using AVPlayer self.avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; [self.avAudioPlayer play];}
List playback
Mmmediaplayer is auto support, play the next song, the previous one can be as follows:
Last Song:
- (IBAction)rewindPressed:(id)sender { if ([self.player indexOfNowPlayingItem] == 0) { [self.player skipToBeginning]; } else { [self.player endSeeking]; [self.player skipToPreviousItem]; }}
Next song:
- (ibaction) fastforwardpressed: (ID) Sender {nsuinteger Nowplayingindex = [Self. Player Indexofnowplayingitem]; [Self. Player Endseeking]; [Self. Player Skiptonextitem];if ([Self. Player Nowplayingitem] = =Nil) {if ([Self. Collection Count] > nowplayingindex+1) {Added more songs while playing [Self. Player Setqueuewithitemcollection:Self. collection]; Mpmediaitem *item = [[Self.collection items] Objectatindex:nowplayingindex+1]; [self.player Setnowplayingitem:item]; [self.player Play];} else {//no More Songs [self.player stop]; nsmutablearray *items = [nsmutablearray arrayWithArray:[< Span class= "Hljs-keyword" >self.toolbar items]]; [Items Replaceobjectatindex:3 withobject:self.play"; [self.toolbar Setitems:items];}}}
Pause and Resume Playback:
- (ibaction) playpausepressed: (ID) Sender {[Self. Pause settintcolor:[Uicolor Blackcolor]]; Mpmusicplaybackstate playbackstate = [Self. Player Playbackstate];Nsmutablearray *items = [Nsmutablearray arraywitharray:[self.toolbar items]]; if (playbackstate = mpmusicplaybackstatestopped | | playbackstate = = mpmusicplaybackstatepaused) {[self.player play]; [Items Replaceobjectatindex:3 withobject:self.pause"; } else if (playbackstate = = mpmusicplaybackstateplaying) {[ Span class= "Hljs-keyword" >self.player pause]; [Items Replaceobjectatindex:3 withobject:self.play"; } [self.toolbar setitems:items animated:no";}
Avaudioplayer has an end-of-play call, so after the last broadcast, reset a song
Music background Playback
If you need to play music in the background, you need to declare it in Info.plist
Otherwise it will be forcibly killed by the system.
Network audio playback
Avaudioplayer does not directly support the network audio playback, you can download the data to a nsdata, and then play.
NSData *mydata=[[NSDataalloc]initWithContentsOfURL:[NSURLURLWithString:command]]; AVAudioPlayer *player=[[AVAudioPlayeralloc]initWithData:mydata error:nil]; [player prepareToPlay]; [player play];
But this is an example of incompetence for streaming media. So you can use the updated avplayer, which can be played directly (but with fewer underlying interfaces)
[[AVPlayer alloc] initWithURL:[NSURL URLWithString:@"http://stream.jewishmusicstream.com:8000"]];
Music Button control
Audioplayer can receive the control of the button of the line control and the Bluetooth headset,
The previous question to really have a song to play when the button can be captured.
This can be remotecontrolreceivedwithevent in appdelegate to capture a variety of keystrokes.
- (void) Remotecontrolreceivedwithevent: (uievent *) event{NSLog (@"Remotecontrolreceivedwithevent%d", event. subtype);Switch (event. subtype) {Case Uieventsubtyperemotecontrolplay: [Self postnotificationwithname:remotecontrolplaybuttontapped];BreakCase Uieventsubtyperemotecontrolpause: [self postnotificationwithname:remotecontrolpausebuttontapped]; break; case uieventsubtyperemotecontrolstop: [self Postnotificationwithname:remotecontrolstopbuttontapped]; break; case uieventsubtyperemotecontrolnexttrack: [self Postnotificationwithname:remotecontrolforwardbuttontapped]; break; case uieventsubtyperemotecontrolprevioustrack: [self Postnotificationwithname:remotecontrolbackwardbuttontapped]; break; default: [self postnotificationwithname: Remotecontrolotherbuttontapped]; break;}
Theoretically, you can play a piece of audio in the background (volume set to 0) to capture the headset keys in your app. For example, the Blue Dot Workshop uses this method to use the Bluetooth headset as a self-timer.
However, this usage is not approved by the app Store, so you can only use the ad hoc distribution method.
Specific reference:
Https://github.com/MosheBerman/ios-audio-remote-control
It is in the background constantly playing a network radio program to achieve the goal, the actual blue Dot Workshop test and drop app comes with a sound file, such as the CAF effect is the same.
IOS Audio Development Experience Summary