IOS Audio Development Experience Summary 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
Frequent use of 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 uses different mechanisms to play the sample at the same time:
Https://github.com/jsanchezsierra/AudioLab
The design of Sound visualization
Suppose 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 gave 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. Be able to participate in the test.
Source code here: https://developer.apple.com/library/prerelease/ios/samplecode/aurioTouch/Introduction/Intro.html
and the updated version (Apple has removed this version number)
Https://github.com/caseytcaprice/aurioTouch2
Basis
Https://github.com/irtemed88/PitchDetector
Pitchdetector
To draw a more perfect waveform file:
Https://github.com/irtemed88/PitchDetector
Speakhere
Apple's official sample shows the real-time recording of the sound wave opening:
Https://developer.apple.com/library/ios/samplecode/SpeakHere/Introduction/Intro.html
Avtouch
A simple example of a sound-to-turn 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 is the local sandbox comes with songs, there is also a 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.delegateself; [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 need to mpmediapickercontroller call such as the following interface can be played, as well as the previous, the next song
[Self.player setQueueWithItemCollection:self.collection];
Let's say that avaudioplayer to play requires a lot of other things to do.
You can take out the song URL in the Mpmediaitemcollection. Directly to Avaudioplayer, this URL format is 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 their own initiative to support, play the next song, the previous one can be used such as the following methods:
Last Song:
- (IBAction)rewindPressed:(id)sender { if ([self.player0) { [self.player skipToBeginning]; else { [self.player endSeeking]; [self.player skipToPreviousItem]; }}
Next song:
- (ibaction) Fastforwardpressed: (IDSender {Nsuinteger Nowplayingindex = [ Self. PlayerIndexofnowplayingitem]; [ Self. PlayerEndseeking]; [ Self. PlayerSkiptonextitem];if([ Self. PlayerNowplayingitem] = =Nil) {if([ Self. CollectionCount] > nowplayingindex+1) {//Added more songs while playing[ Self. PlayerSetqueuewithitemcollection: Self. Collection]; Mpmediaitem *item = [[ Self. CollectionItems] objectatindex:nowplayingindex+1]; [ Self. PlayerSetnowplayingitem:item]; [ Self. PlayerPlay]; }Else{//No More songs[ Self. PlayerStop];Nsmutablearray*items = [Nsmutablearrayarraywitharray:[ Self. ToolbarItems]]; [Items Replaceobjectatindex:3Withobject: Self. Play]; [ Self. ToolbarSetitems:items]; } }}
Pause and Resume Playback:
- (ibaction) Playpausepressed: (ID) Sender {[ Self. Pausesettintcolor:[UicolorBlackcolor]]; Mpmusicplaybackstate playbackstate = [ Self. PlayerPlaybackstate];Nsmutablearray*items = [Nsmutablearrayarraywitharray:[ Self. ToolbarItems]];if(playbackstate = = Mpmusicplaybackstatestopped | | playbackstate = = mpmusicplaybackstatepaused) { [ Self. PlayerPlay]; [Items Replaceobjectatindex:3Withobject: Self. Pause]; }Else if(playbackstate = = mpmusicplaybackstateplaying) { [ Self. PlayerPause]; [Items Replaceobjectatindex:3Withobject: Self. Play]; } [ Self. ToolbarSetitems:items Animated:NO];}
Avaudioplayer has an end-of-play call, so after the last broadcast, reset a song to
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 network audio playback, the ability to first download 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, it can play directly (but the lower interface is less)
[[AVPlayer alloc] initWithURL:[NSURL URLWithString:@"http://stream.jewishmusicstream.com:8000"]];
Music Button control
The Audioplayer is able to receive the key control of the line control and Bluetooth headset,
Before the question to really have a song to play when the ability to capture keys.
This can be remotecontrolreceivedwithevent in appdelegate to capture various keystrokes.
- (void) Remotecontrolreceivedwithevent: (uievent *) event{NSLog(@"Remotecontrolreceivedwithevent%d", event. Subtype);Switch(Event. Subtype) { CaseUieventsubtyperemotecontrolplay: [ SelfPostnotificationwithname:remotecontrolplaybuttontapped]; Break; CaseUieventsubtyperemotecontrolpause: [ SelfPostnotificationwithname:remotecontrolpausebuttontapped]; Break; CaseUieventsubtyperemotecontrolstop: [ SelfPostnotificationwithname:remotecontrolstopbuttontapped]; Break; CaseUieventsubtyperemotecontrolnexttrack: [ SelfPostnotificationwithname:remotecontrolforwardbuttontapped]; Break; CaseUieventsubtyperemotecontrolprevioustrack: [ SelfPostnotificationwithname:remotecontrolbackwardbuttontapped]; Break;default: [ SelfPostnotificationwithname:remotecontrolotherbuttontapped]; Break; }}
Theory. The ability to continuously play a piece of audio in the background (volume set to 0) captures the headset keys in the app today.
For example, the Blue Dot Workshop uses this method to use the Bluetooth headset as a self-timer.
However, the use of this method is not over the app Store audit. Therefore, only the ad hoc distribution method can be used.
Detailed references:
Https://github.com/MosheBerman/ios-audio-remote-control
It is in the background constantly playing a network radio program to achieve the purpose. The actual blue Dot workshop test put the app with a sound file, like the CAF effect is the same.
IOS Audio Development Experience Summary