#import <AudioToolbox/AudioToolbox.h> AudioServicesPlaySystemSound(1106);
//Get the filename of the sound file: NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"/jad0007a.wav"]; // declare a system sound id SystemSoundID soundID; //Get a URL for the sound file NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; //Use audio sevices to create the sound AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID); //Use audio services to play the sound AudioServicesPlaySystemSound(soundID);
AudioServices - iPhone Development Wiki()
#import <AVFoundation/AVFoundation.h> NSURL* url = [[NSBundlemainBundle] URLForResource:@"five"withExtension:@"mp3"]; AVAudioPlayer* audioPlay = [[AVAudioPlayeralloc] initWithContentsOfURL:url error:nil]; [audioPlay play];
AVAudiosession是AVFoundation框架提供的一个单例类,可以用来为我们的APP设置一个合适的音频环境。通过对他进行配置,我们可以为自己的音乐播放APP设置合适的特征。
Category | 说明 | 场景 | 输入 | 输出 | 混合 | 遵从静音 |
---|---|---|---|---|---|---|
AVAudioSessionCategoryAmbient | 背景音效 | NO | YES | YES | YES | |
AVAudioSessionCategoryPlayback | 音乐播放器 | NO | YES | NO | NO | |
AVAudioSessionCategoryOptionMixWithOthers | ||||||
AVAudioSessionCategorySoloAmbient | NO | YES | NO | YES | ||
AVAudioSessionCategoryRecord | YES | NO | NO | NO | ||
AVAudioSessionCategoryPlayAndRecord | YES | YES | NO | NO | ||
AVAudioSessionCategoryAudioProcessing | 使用硬件解码器处理音频,该音频会话使用期间,不能播放或录音 | |||||
AVAudioSessionCategoryMultiRoute | YES | YES | NO | NO | ||
[[AVAudioSession sharedInstance] setActive:YES error:nil];
手机上不止我们一款APP,在听歌的时候,如果有人给我们打电话;或者之前定的一个闹钟到时间了;或者使用了siri功能。这些会使用手机音频的应用,就会跟我们的APP音频起冲突。称作音频中断。
iOS系统的音频服务优先级高于第三方APP。当有电话进入、系统闹钟响起,都会导致第三方APP的audiosession中断。
有两种方式来处理这种中断
1.通过注册观察者来获取AVAudioSessionInterruptionNotification
事件的通知来响应中断的开始和结束。
2.设置AVAudioSession代理
//设置代理 可以处理电话打进时中断音乐播放 [[AVAudioSession sharedInstance] setDelegate:self];
Media Player framework 加入了MPRemoteCommandCenter这个类。使用block类实现远程控制回调。
要让APP支持RemoteControl,我们需要用到MPRemoteCommandCenter单例类。它提供了处理 remote control events所需要的对象。它的属性中包括了众多MPRemoteCommand类对象,代表着iOS所支持的不同类型的remote control evnt。为MPRemoteCommand对象添加target和action来响应其控制事件。
需要在UIApplication中实现remoteControlReceivedWithEvent来处理。
为了在锁屏界面和控制中心显示当前歌曲的信息,需要用到Media Player Framework
的MPNowPlayingInfoCenter
类。把需要显示的信息组织成Dictionary并赋值给nowPlayingInfo
属性就完成了。
一些常见的属性值如下:
// MPMediaItemPropertyAlbumTitle 专辑标题 // MPMediaItemPropertyAlbumTrackCount 声道个数 // MPMediaItemPropertyAlbumTrackNumber 左右声道 // MPMediaItemPropertyArtist 艺术家(歌曲作者) // MPMediaItemPropertyArtwork 锁屏界面的封面 // MPMediaItemPropertyComposer // MPMediaItemPropertyDiscCount // MPMediaItemPropertyDiscNumber // MPMediaItemPropertyGenre // MPMediaItemPropertyPersistentID // MPMediaItemPropertyPlaybackDuration 播放时长 // MPMediaItemPropertyTitle
除了上面的Item Property,还有一些播放信息的属性值,Playing Info Property,其中需要特别注意的是MPNowPlayingInfoPropertyPlaybackRate
和MPNowPlayingInfoPropertyElapsedPlaybackTime
。前者表示播放速率,后者表示已播放时间,即上图中进度条左边的时间。当设置了这两个值,并且当前正在播放,就会自动地根据前一个时间和播放速率来计算。
在iOS11之前,当歌曲暂停时,其实该值还是在增加。为了保证该值的精确,需要在暂停和重新开始播放时,重新设置MPNowPlayingInfoPropertyPlaybackRate的值。
特别在iOS 11时,我们需要指定playbackState
的值,当不是MPNowPlayingPlaybackStatePlaying
时,并不会在锁屏界面显示当前播放信息。
参考1:
参考2:
如对本文有疑问, 点击进行留言回复!!
iOS14Beta3续航怎么样 iOS14Beta3续航能力介绍
iOS14Beta3稳定性怎么样 iOS14Beta3升级建议介绍
uniapp编写H5界面放入IOS全面屏原生导航栏盖住H5界面
游戏APP解决方案,8月1日起下架无版号ios手游的出路在哪?
网友评论