zoukankan      html  css  js  c++  java
  • iOS 的音频播放

    一、Audio Toolbox

    1.使用代码

    #import <AudioToolbox/AudioToolbox.h>
    AudioServicesPlaySystemSound(1106); 
    

    2.如果想用自己的音频文件创建系统声音来播放的同学可以参考如下代码。

    //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);
    

    3.系统提示音对应的列表

    AudioServices - iPhone Development Wiki(http://iphonedevwiki.net/index.php/AudioServices)

    二、AVAudioPlayer

    1.声音播放

    #import <AVFoundation/AVFoundation.h>
    
    
    NSURL* url = [[NSBundlemainBundle] URLForResource:@"five"withExtension:@"mp3"];
    AVAudioPlayer* audioPlay = [[AVAudioPlayeralloc] initWithContentsOfURL:url error:nil]; 
    [audioPlay play];
    

    三、AVAudiosession

    三、AVAudiosession

    三、AVAudiosession

    AVAudiosession是AVFoundation框架提供的一个单例类,可以用来为我们的APP设置一个合适的音频环境。通过对他进行配置,我们可以为自己的音乐播放APP设置合适的特征。

    1.category属性,默认值是AVAudioSessionCategorySoloAmbient

    Category 说明 场景 输入 输出 混合 遵从静音
    AVAudioSessionCategoryAmbient 可以与其他APP同时播放 背景音效 NO YES YES YES
    AVAudioSessionCategoryPlayback 不可与其他APP同时播放 音乐播放器 NO YES NO NO
    AVAudioSessionCategoryOptionMixWithOthers
    AVAudioSessionCategorySoloAmbient 默认,独占,设置后其他APP就停止播放 NO YES NO YES
    AVAudioSessionCategoryRecord YES NO NO NO
    AVAudioSessionCategoryPlayAndRecord YES YES NO NO
    AVAudioSessionCategoryAudioProcessing 使用硬件解码器处理音频,该音频会话使用期间,不能播放或录音
    AVAudioSessionCategoryMultiRoute YES YES NO NO

    2.静音状态下播放

     [[AVAudioSession sharedInstance] setActive:YES
    
                                           error:nil];
    
    

    四、音频中断

    手机上不止我们一款APP,在听歌的时候,如果有人给我们打电话;或者之前定的一个闹钟到时间了;或者使用了siri功能。这些会使用手机音频的应用,就会跟我们的APP音频起冲突。称作音频中断。

    iOS系统的音频服务优先级高于第三方APP。当有电话进入、系统闹钟响起,都会导致第三方APP的audiosession中断。

    有两种方式来处理这种中断

    1.通过注册观察者来获取AVAudioSessionInterruptionNotification事件的通知来响应中断的开始和结束。

    2.设置AVAudioSession代理

      //设置代理 可以处理电话打进时中断音乐播放
    
      [[AVAudioSession sharedInstance] setDelegate:self];
    
    

    五、远程控制

    1.iOS7.1之后

    Media Player framework 加入了MPRemoteCommandCenter这个类。使用block类实现远程控制回调。

    要让APP支持RemoteControl,我们需要用到MPRemoteCommandCenter单例类。它提供了处理 remote control events所需要的对象。它的属性中包括了众多MPRemoteCommand类对象,代表着iOS所支持的不同类型的remote control evnt。为MPRemoteCommand对象添加target和action来响应其控制事件。

    2.iOS7.1之前

    需要在UIApplication中实现remoteControlReceivedWithEvent来处理。

    六、锁屏界面显示歌曲信息

    为了在锁屏界面和控制中心显示当前歌曲的信息,需要用到Media Player FrameworkMPNowPlayingInfoCenter类。把需要显示的信息组织成Dictionary并赋值给nowPlayingInfo属性就完成了。

    一些常见的属性值如下:

     // MPMediaItemPropertyAlbumTitle           专辑标题 
     // MPMediaItemPropertyAlbumTrackCount      声道个数 
     // MPMediaItemPropertyAlbumTrackNumber     左右声道 
     // MPMediaItemPropertyArtist               艺术家(歌曲作者) 
     // MPMediaItemPropertyArtwork              锁屏界面的封面 
     // MPMediaItemPropertyComposer 
     // MPMediaItemPropertyDiscCount 
     // MPMediaItemPropertyDiscNumber 
     // MPMediaItemPropertyGenre 
     // MPMediaItemPropertyPersistentID 
     // MPMediaItemPropertyPlaybackDuration     播放时长 
     // MPMediaItemPropertyTitle 
    

    除了上面的Item Property,还有一些播放信息的属性值,Playing Info Property,其中需要特别注意的是MPNowPlayingInfoPropertyPlaybackRateMPNowPlayingInfoPropertyElapsedPlaybackTime。前者表示播放速率,后者表示已播放时间,即上图中进度条左边的时间。当设置了这两个值,并且当前正在播放,就会自动地根据前一个时间和播放速率来计算。

    在iOS11之前,当歌曲暂停时,其实该值还是在增加。为了保证该值的精确,需要在暂停和重新开始播放时,重新设置MPNowPlayingInfoPropertyPlaybackRate的值。

    特别在iOS 11时,我们需要指定playbackState的值,当不是MPNowPlayingPlaybackStatePlaying时,并不会在锁屏界面显示当前播放信息。

    参考1:http://www.samirchen.com/ios-avaudiosession-3/

    参考2:http://msching.github.io/blog/2014/11/06/audio-in-ios-8/

  • 相关阅读:
    $POST数组论证($GET || $COOKIE || $REQUEST 同理)
    PHP之preg_replace()与ereg_replace()正则匹配比较讲解
    PHP之mysql_real_escape_string()函数讲解
    浅析白盒审计中的字符编码及SQL注入
    PHP之list()函数讲解
    PHP之implode与explode函数讲解
    PHP之Error与Logging函数讲解
    Nextcloud私有云盘在Centos7下的部署笔记
    Linux下路由配置梳理
    实验c语言不同类型的指针互用(不推荐只是学习用)
  • 原文地址:https://www.cnblogs.com/v2m_/p/8575622.html
Copyright © 2011-2022 走看看