·iOS系統(tǒng)中的音頻播放方式·AVAudioPlayer产舞、AVPlayer裹刮、系統(tǒng)聲音、音頻隊列
·AVAudioPlayer·使用簡單方便庞瘸,但只能播放本地音頻捧弃,不支持流媒體播放
·AVPlayer·iOS4.0以后,可以使用AVPlayer播放本地音頻和支持流媒體播放擦囊,但提供接口較少违霞,處理音頻不夠靈活·音頻隊列
·音頻隊列主要處理流媒體播放,提供了強大且靈活的API接口(C函數(shù)的接口)瞬场,但處理起來也較為復雜
·播放系統(tǒng)聲音·音頻數(shù)據可分為壓縮和非壓縮的存儲類型买鸽。壓縮的音頻文件雖然文件體積較小(相對于非壓縮的)贯被,但需要耗費處理器的性能進行解壓和解碼眼五。
·如果音頻文件體積較小妆艘,壓縮后的音頻文件,也不會節(jié)省較大的磁盤空間看幼。像這一類小型非壓縮的文件可以注冊成為系統(tǒng)聲音·格式為:caf/wav/aiff格式批旺,且時長小于30s
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? //1.plist文件Required background modes
? ? //App plays audio or streams audio/video using AirPlay
? ? //2.設置后臺播放
? ? [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
? ? //1取得本地音頻文件路徑
? ? NSString*filePath =? [[NSBundle mainBundle] pathForResource:@"安靜" ofType:@"mp3"];
? ? NSURL*url = [NSURLfileURLWithPath:filePath];
? ? //創(chuàng)建全局播放器? AVAudioPlayer 只能播放本地音頻文件
? ? _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
? ? //2播放流媒體音樂,AVPlayer都支持
? ? //獲得網絡鏈接
? ? NSString *str = @"http://ra01.sycdn.kuwo.cn/resource/n3/32/56/3260586875.mp3";
? ? NSURL*url1 = [NSURLURLWithString:str];
? ? _player= [[AVPlayeralloc]initWithURL:url1];
}
//1播放本地音樂
- (IBAction)playLocalMusic:(id)sender {
? ? if(_audioPlayer.isPlaying){
? ? ? ? [_audioPlayerpause];
? ? }else{
? ? ? ? [_audioPlayerplay];
? ? }
}
//2播放流媒體音樂
- (IBAction)playMusic:(id)sender {
? ? if(_isPlay) {
? ? ? ? [_playerpause];
? ? }else{
? ? ? ? [_playerplay];
? ? }
? ? _isPlay = !_isPlay;
}
//3播放系統(tǒng)聲音
- (IBAction)playSystemSound:(id)sender {
? ? NSString *filePath = [[NSBundle mainBundle] pathForResource:@"44th Street Medium.caf" ofType:nil];
? ? NSURL*url = [NSURLfileURLWithPath:filePath];
? ? UInt32? systemSoundID =10;
? ? //注冊系統(tǒng)聲音
? ? AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &systemSoundID);
? ? //根據id播放系統(tǒng)聲音
? ? AudioServicesPlaySystemSound(systemSoundID);
? ? //設置手機振動
? ? AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
//設置鎖屏時界面顯示的播放信息
-(void)_initUI{
? ? /* MPMediaItemPropertyAlbumTitle
?? ? // MPMediaItemPropertyAlbumTrackCount
?? ? // MPMediaItemPropertyAlbumTrackNumber
?? ? // MPMediaItemPropertyArtist
?? ? // MPMediaItemPropertyArtwork
?? ? // MPMediaItemPropertyComposer
?? ? // MPMediaItemPropertyDiscCount
?? ? // MPMediaItemPropertyDiscNumber
?? ? // MPMediaItemPropertyGenre
?? ? // MPMediaItemPropertyPersistentID
?? ? // MPMediaItemPropertyPlaybackDuration
?? ? // MPMediaItemPropertyTitle*/
? ? MPMediaItemArtwork *artWork = [[MPMediaItemArtwork alloc ] initWithImage:[UIImage imageNamed:@"mp3.jpg"]];
?? NSDictionary *dic = @{
? ? ? ? ? ? ? ? ? ? ? ? ? MPMediaItemPropertyAlbumTitle:@"兄弟你變了",
? ? ? ? ? ? ? ? ? ? ? ? ? MPMediaItemPropertyArtist:@"龐龍",
? ? ? ? ? ? ? ? ? ? ? ? ? MPMediaItemPropertyArtwork:? artWork
? ? ? ? ? ? ? ? ? ? ? ? ? };
? ? [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dic];
}
-(BOOL )canBecomeFirstResponder{
? ? return YES;
}
-(void)viewWillAppear:(BOOL)animated{
? ? [superviewWillAppear:animated];
? ? [self becomeFirstResponder];
? ? [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
? ? [self_initUI];
}
-(void)viewWillDisappear:(BOOL)animated{
? ? [superviewWillDisappear:animated];
? ? [self resignFirstResponder];
? ? [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
}
//設置遠程監(jiān)控
-(void)remoteControlReceivedWithEvent:(UIEvent *)event{
? ? if (event.type == UIEventTypeRemoteControl) {
? ? ? ? switch(event.subtype) {
? ? ? ? ? ? case UIEventSubtypeRemoteControlPlay:
? ? ? ? ? ? ? ? [_audioPlayerplay];
? ? ? ? ? ? ? ? NSLog(@"播放");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case UIEventSubtypeRemoteControlPause:
? ? ? ? ? ? ? ? [_audioPlayerpause];
? ? ? ? ? ? ? ? NSLog(@"暫停");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case UIEventSubtypeRemoteControlNextTrack:
? ? ? ? ? ? ? ? NSLog(@"下一首");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case UIEventSubtypeRemoteControlPreviousTrack:
? ? ? ? ? ? ? ? NSLog(@"上一首");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
}