zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    645. Set Mismatch
    400. Nth Digit
    633. Sum of Square Numbers
    507. Perfect Number
    453. Minimum Moves to Equal Array Elements
    441. Arranging Coins
    Girls and Boys
    二分图
    Gap
    SZU-A22
  • 原文地址:https://www.cnblogs.com/iguor/p/2704818.html
Copyright © 2011-2022 走看看