iOS後台運行播放無聲音頻 測試可行

來源:互聯網
上載者:User

標籤:播放   hex   error   handler   測試   ber   orm   tom   res   

如果打回來了,就自認倒黴吧

製作無聲音頻。

 

 

 

@interface AppDelegate ()

{

NSInteger count;

}

 

@property(strong, nonatomic)NSTimer *mTimer;

@property(assign, nonatomic)UIBackgroundTaskIdentifier backIden;

 

- (void)applicationDidEnterBackground:(UIApplication *)application {

 

_mTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countAction) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop] addTimer:_mTimer forMode:NSRunLoopCommonModes];

[self beginTask];

 

}

//計時

-(void)countAction{

NSLog(@"%li",count++);

}

//申請後台

-(void)beginTask

{

NSLog(@"begin=============");

_backIden = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

 

NSLog(@"將要掛起=============");

[self endBack];

}];

}

//登出後台

-(void)endBack

{

NSLog(@"end=============");

[[UIApplication sharedApplication] endBackgroundTask:_backIden];

_backIden = UIBackgroundTaskInvalid;

}

- (void)applicationWillEnterForeground:(UIApplication *)application {

 

[self endBack];

 

}

 

 

 

#import <AVFoundation/AVFoundation.h>

 

 

 

@property(strong, nonatomic)AVAudioPlayer *mPlayer;

 

@property(assign, nonatomic)CGFloat mCount;

 

- (void)viewDidLoad {

[super viewDidLoad];

 

 

_mCount = 0;

 

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(countTime) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

 

 

}

 

-(void)countTime{

_mCount+=10;

NSLog(@"%f",_mCount);

 

if ([[UIApplication sharedApplication] backgroundTimeRemaining] < 60.) {//當剩餘時間小於60時,開如播放音樂,並用這個假前台狀態再次申請後台

NSLog(@"播放%@",[NSThread currentThread]);

[self playMusic];

//申請後台

[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

NSLog(@"我要掛起了");

}];

}

}

 

-(void)playMusic{

//1.音頻檔案的url路徑,實際開發中,用無聲音樂

NSURL *url=[[NSBundle mainBundle]URLForResource:@"wusheng.mp3" withExtension:Nil];

 

//2.建立播放器(注意:一個AVAudioPlayer只能播放一個url)

_mPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];

 

//3.緩衝

[_mPlayer prepareToPlay];

_mPlayer.numberOfLoops = -1;

//4.播放

[_mPlayer play];

}

iOS後台運行播放無聲音頻 測試可行

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.