• AVAudionPlayer支持后台播放


    刚才遇到了个不大不小的问题,就是AVAudionPlayer如何在后台播放音频文件。网上查找方法,最后解决,方法整理如下:

    1.需要加入AVaudionSession的代码,总体如下,各位可以抽减掉初始化的代码。

    1. - (void)viewDidLoad  
    2. {  
    3.     NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"in-the-storm" ofType:@"mp3"]];  
    4.   
    5.     AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];  
    6.   
    7.     [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];  
    8.     [[AVAudioSession sharedInstance] setActive: YES error: nil];  
    9.     [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];  
    10.   
    11.     [audioPlayer play];  
    12.   
    13.     [super viewDidLoad];  
    14. }  

    加入如上的代码后,把程序放到后台还是会自动消失声音,于是找到如下的第二步操作。

    2.修改plist文件。

    在plist文件中新增 Require background modes栏位,并填入内容为audio.

    经过以上两步的操作,大功告成。

    参考文献:

    http://stackoverflow.com/questions/7619794/play-music-in-background-in-iphone-using-avaudioplayer

    http://www.cocoachina.com/bbs/simple/?t41975.html

  • 相关阅读:
    C#将List<T>转化为DataTable
    SqlServer常用内置函数
    C#索引器
    验证Textbox的字符长度
    WM消息对应的Message消息中的Lparam和WParam
    对窗体操作的WM消息
    DllImport使用
    C#获取当前路径的七种方法
    注册ActiveX控件
    [转]VS2010中水晶报表安装应用及实例
  • 原文地址:https://www.cnblogs.com/iguor/p/2704818.html
Copyright © 2020-2023  润新知