zoukankan      html  css  js  c++  java
  • iOS后台运行播放无声音频 测试可行

    如果打回来了,就自认倒霉吧

    制作无声音频。


    @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];

    }

  • 相关阅读:
    ThinkPHP 3.2 调用自定义函数库
    phpstorm 2017版代码提示功能开启解决方案
    phpstorm 2017激活
    JavaScript返回上一页和返回上一级页面并刷新
    PHP处理Ajax请求与Ajax跨域
    13个能快速开发android的经典项目
    分享6款优秀的 AR/VR 开源库
    Android-----购物车(包含侧滑删除,商品筛选,商品增加和减少,价格计算,店铺分类等)
    Android------视频播放器(包含全屏播放,快退,快进,腾讯新闻的列表播放等)
    吴恩达课后作业学习2-week1-2正则化
  • 原文地址:https://www.cnblogs.com/xujiahui/p/9019173.html
Copyright © 2011-2022 走看看