首页 > 其他分享 >iOS音频播放-后台

iOS音频播放-后台

时间:2023-02-10 15:22:30浏览次数:38  
标签:nil text 音频 iOS application manager void 播放 self

ViewController.m

//
//  AVPlayerViewController.m
//  AVAudioDemo
//
//  Created by pconline on 2017/8/23.
//  Copyright © 2017年 pconline. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "LGFAFNetworkingTool.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) AVPlayer *player;
@property(nonatomic,strong) UILabel *starTimeLabel;
@property(nonatomic,strong) UILabel *stateLabel;
@property(nonatomic,strong) UILabel *payCountLabel;
@property(nonatomic,strong) UITableView *payListTB;
@property(nonatomic,strong) NSMutableArray *payDataArr;

@property(nonatomic,strong)NSTimer*weakTimer;
@property(nonatomic,assign) int playCount;
@property(nonatomic,assign) int payCount;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    NSArray* localDataArr = [[NSUserDefaults standardUserDefaults] objectForKey:@"localPayDataDicts"];
     [self.payDataArr removeAllObjects];
     if(localDataArr.count>0){
         [self.payDataArr addObjectsFromArray:localDataArr];
     }
    [self setUpView];
    self.title = @"千店收款";
    self.weakTimer = [NSTimer scheduledTimerWithTimeInterval:4
                                                      target:self
                                                    selector:@selector(onTimer)
                                                    userInfo:nil
                                                     repeats:YES];
    [self.weakTimer fire];
}

-(void)onTimer{
    __weak __typeof__(self) weakSelf = self;
    NSLog(@"onTimer");
    NSString *url = @"https://www.sglituo.com/textToSounds";
    NSDictionary *params = @{@"staffId":@"od0Ld4k0Rto8RKSvf42M4QQCnFL0"};
    [LGFAFNetworkingTool networkWithMethod:PDAHTTPMethodPOST url:url parameters:params headers:nil success:^(id  _Nullable response) {
        int code = [response[@"code"] intValue];
        NSString *data = response[@"data"];
        NSLog(@"response= %@",data);
        if(code==10001){
            weakSelf.playCount++;
            weakSelf.stateLabel.text = [NSString stringWithFormat:@"循环次数: %d ,时间:%@",weakSelf.playCount,[weakSelf getCurrentTimes]];
            if (response[@"data"] != [NSNull null] && response[@"data"] != nil) {
                NSString *payDataStr = response[@"data"];
                [weakSelf reloadTBWith:payDataStr];
                weakSelf.payCount++;
                [weakSelf prepareToPlayWithUrl:response[@"data"]];
//                weakSelf.payCountLabel.text = [NSString stringWithFormat:@"付款次数 - %d - 当前时间=%@",self.payCount,[self getCurrentTimes]];
            }
        }
    } failure:^(NSError * _Nullable error) {
        NSLog(@"error= %@",error);
    }];
}

-(void)reloadTBWith:(NSString *)payDataStr{
    NSRange startRange = [payDataStr rangeOfString:@"/speeksrc/"];
    NSInteger startLocation = startRange.location+startRange.length;
    NSRange endRange = [payDataStr rangeOfString:@".mp3"];
    NSString *moneyStr = [payDataStr substringWithRange:NSMakeRange(startLocation,endRange.location-startLocation)];
    NSDictionary *payDict = @{@"money":[NSString stringWithFormat:@"金额:%@元",moneyStr],@"date":[NSString stringWithFormat:@"时间:%@",[self getCurrentTimes]]};
    [self.payDataArr insertObject:payDict atIndex:0];
    [[NSUserDefaults standardUserDefaults] setObject:self.payDataArr forKey:@"localPayDataDicts"];
    [self.payListTB reloadData];
}


- (void)prepareToPlayWithUrl:(NSString*)url{
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:url]];
    self.player = [[AVPlayer alloc] initWithPlayerItem:item];
    [self.player play];
}

-(void)setUpView{
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *starTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 90, self.view.bounds.size.width, 30)];
    starTimeLabel.textAlignment = NSTextAlignmentLeft;
    self.starTimeLabel = starTimeLabel;
    starTimeLabel.font = [UIFont systemFontOfSize:14];
    starTimeLabel.text = [NSString stringWithFormat:@"开始时间:%@",[self getCurrentTimes]];
    [self.view addSubview:starTimeLabel];
    
    //循环次数
    UILabel *stateLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 120, self.view.bounds.size.width, 30)];
    stateLabel.font = [UIFont systemFontOfSize:14];
    stateLabel.textAlignment = NSTextAlignmentLeft;
    self.stateLabel = stateLabel;
    [self.view addSubview:stateLabel];
    
    
    self.payListTB= [[UITableView alloc] initWithFrame:CGRectMake(0, 150, self.view.bounds.size.width, self.view.bounds.size.height-150) style:UITableViewStylePlain];
    self.payListTB.delegate= self;
    self.payListTB.dataSource= self;
    //属性设置 行高
    self.payListTB.rowHeight= 50;
    [self.view addSubview:self.payListTB];
}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
    
    static NSString*idetifierCell = @"cell";
    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:idetifierCell];
    if(!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:idetifierCell];
    }
    if(self.payDataArr.count>0){
        NSLog(@"%@",self.payDataArr);
        NSDictionary *dict = self.payDataArr[indexPath.row];
        cell.textLabel.text = dict[@"money"];
        cell.detailTextLabel.text = dict[@"date"];
        cell.detailTextLabel.textColor = [UIColor colorWithRed:52/255.0 green:52/255.0 blue:52/255.0 alpha:1];
    }
    return cell;
}

//返回节数
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
    return 1;
}

//返回某个节中行数
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
    return self.payDataArr.count;
}

//获取当前的时间
-(NSString*)getCurrentTimes{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *datenow = [NSDate date];
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    return currentTimeString;
}

-(void)dealloc{
    [self.player pause];
    self.player = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(NSMutableArray *)payDataArr{
    if(!_payDataArr){
        _payDataArr = [NSMutableArray array];
    }
    return _payDataArr;
}

@end
View Code

appDelegate.m

//
//  AppDelegate.m
//  AVAudioDemo
//
//  Created by pconline on 2017/8/16.
//  Copyright © 2017年 pconline. All rights reserved.
//

#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
#import "BackgroundTaskManager.h"
@interface AppDelegate ()
@property UIBackgroundTaskIdentifier backgroundTask;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //后台播放
    //    AVAudioSession *session = [AVAudioSession sharedInstance];
    //    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    //    [session setActive:YES error:nil];
    
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    //停止播放
    [[BackgroundTaskManager shareManager] stopPlayAudioSession];
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    //开始播放
    [[BackgroundTaskManager shareManager] startPlayAudioSession];
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
View Code

LGFAFNetwork.h

//
//  LGFAFNetworkingTool.h
//  testOC
//
//  Created by bairui on 2023/2/9.
//

#import <Foundation/Foundation.h>
#import <AFNetworking.h>
 
NS_ASSUME_NONNULL_BEGIN
 
//网络请求枚举
typedef enum : NSUInteger {
    PDAHTTPMethodGET,
    PDAHTTPMethodPOST
} PDAHTTPMethod;
 
@interface LGFAFNetworkingTool : AFHTTPSessionManager
 
+ (void)networkWithMethod:(PDAHTTPMethod)method
                     url:(NSString *)urlString
              parameters:(nullable NSDictionary *)parameters
                 headers:(nullable NSDictionary <NSString *, NSString *> *)headers
                 success:(nullable void(^)(id _Nullable response))success
                  failure:(nullable void(^)(NSError * _Nullable error))failure;
@end
 
NS_ASSUME_NONNULL_END
View Code

LGFAFNetwork.m

//
//  LGFAFNetworkingTool.m
//  testOC
//
//  Created by bairui on 2023/2/9.
//

#import "LGFAFNetworkingTool.h"
 
@implementation LGFAFNetworkingTool
-(AFHTTPSessionManager *)sharedManager
{
  AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  //最大请求并发任务数
  manager.operationQueue.maxConcurrentOperationCount = 5;

  // 请求格式
  // AFHTTPRequestSerializer      二进制格式
  // AFJSONRequestSerializer      JSON
  // AFPropertyListRequestSerializer  PList(是一种特殊的XML,解析起来相对容易)
  
  manager.requestSerializer = [AFHTTPRequestSerializer serializer]; // 上传普通格式
  
  // 超时时间
  manager.requestSerializer.timeoutInterval = 30.0f;
  // 设置请求头
  [manager.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
  // 设置接收的Content-Type
  manager.responseSerializer.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"application/xml", @"text/xml",@"text/html", @"application/json",@"text/plain",nil];
  
  // 返回格式
  // AFHTTPResponseSerializer      二进制格式
  // AFJSONResponseSerializer      JSON
  // AFXMLParserResponseSerializer   XML,只能返回XMLParser,还需要自己通过代理方法解析
  // AFXMLDocumentResponseSerializer (Mac OS X)
  // AFPropertyListResponseSerializer  PList
  // AFImageResponseSerializer     Image
  // AFCompoundResponseSerializer    组合
  
  manager.responseSerializer = [AFJSONResponseSerializer serializer];//返回格式 JSON
  //设置返回C的ontent-type
  manager.responseSerializer.acceptableContentTypes=[[NSSet alloc] initWithObjects:@"application/xml", @"text/xml",@"text/html", @"application/json",@"text/plain",nil];

  return manager;
}


+ (void)networkWithMethod:(PDAHTTPMethod)method
                     url:(NSString *)urlString
              parameters:(nullable NSDictionary *)parameters
                 headers:(nullable NSDictionary <NSString *, NSString *> *)headers
                 success:(nullable void(^)(id _Nullable response))success
                 failure:(nullable void(^)(NSError * _Nullable error))failure {
   
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializerWithWritingOptions:NSJSONWritingPrettyPrinted];
    // 超时时间
    manager.requestSerializer.timeoutInterval = 30.0f;
    // 设置请求头
    [manager.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
    // 设置接收的Content-Type
    manager.responseSerializer.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"application/xml", @"text/xml",@"text/html", @"application/json",@"text/plain",nil];
    manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingMutableContainers];
    //设置返回C的ontent-type
    manager.responseSerializer.acceptableContentTypes=[[NSSet alloc] initWithObjects:@"application/xml", @"text/xml",@"text/html", @"application/json",@"text/plain",nil];
    
    if (method == PDAHTTPMethodPOST) {
        [manager POST:urlString parameters:parameters headers:headers progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (success != nil) {
                success(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failure != nil) {
                failure(error);
            }
        }];
    }
    
    if (method == PDAHTTPMethodGET) {
        [manager GET:urlString parameters:parameters headers:headers progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (success != nil) {
                success(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failure != nil) {
                failure(error);
            }
        }];
    }
}

@end
View Code

BackgroundTaskManager.h

//
//  BackgroundTaskManager.h
//  Teleprompter_iOS
//
//  Created by ileja on 2022/5/17.
//
//  后台保活工具

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface BackgroundTaskManager : NSObject

/// 创建单利
+ (BackgroundTaskManager *)shareManager;

/// 开始播放音乐
- (void)startPlayAudioSession;

/// 停止播放音乐
- (void)stopPlayAudioSession;

@end

NS_ASSUME_NONNULL_END
View Code

BackgroundTaskManager.m

//
//  BackgroundTaskManager.m
//  Teleprompter_iOS
//
//  Created by ileja on 2022/5/17.
//

#import "BackgroundTaskManager.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

@interface BackgroundTaskManager()

@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

@end

@implementation BackgroundTaskManager

/// 创建单利
+ (BackgroundTaskManager *)shareManager
{
   static BackgroundTaskManager *manager = nil;
   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{
       manager = [[BackgroundTaskManager alloc]init];
   });
   return manager;
}

/// 开始播放声音
- (void)startPlayAudioSession
{
   [self.audioPlayer play];
}

/// 停止播放声音
- (void)stopPlayAudioSession
{
   [self.audioPlayer stop];
}

- (AVAudioPlayer *)audioPlayer
{
    if (!_audioPlayer) {
        //设置后台模式和锁屏模式下依然能够播放
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
        [[AVAudioSession sharedInstance] setActive: YES error: nil];
        //初始化音频播放器
        NSError *playerError;
        NSURL *urlSound = [[NSURL alloc]initWithString:[[NSBundle mainBundle]pathForResource:@"pomodoSound" ofType:@"wav"]];
        _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:urlSound error:&playerError];
        _audioPlayer.numberOfLoops = -1;//无限播放
        _audioPlayer.volume = 0;
    }
    return _audioPlayer;
}

@end
View Code

 

标签:nil,text,音频,iOS,application,manager,void,播放,self
From: https://www.cnblogs.com/somethingWithiOS/p/17109042.html

相关文章

  • iOS AppStore上架流程图文详解2021版 (上)
    到了2021年,虽然网上也有大牛写过很多IOSApp上架流程资料,但随着苹果发布机制的微调有些已经过时了。我就趁着这次刚刚发布成功的鲜活经验,记录下来,做一下补充。1、首先得......
  • vue课程75 axios是只专注于网络请求的库
     <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"conte......
  • iOS 组件化 创建本地私有库
    cocoapods组件化私有库spec文件Librarynotfoundfor-l为什么要有组件化一个公司的项目在不断更新迭代,项目代码越来越复杂,模块间的耦合度越来越高,导致我们后续的开发......
  • 音乐播放器 — 用 vant4 中的滑块自定义播放器进度条
    一、运行效果   二、代码实现2.1、HTML:<!--音频播放器--><audioref="audio"src="音乐名称.mp3"id="audio"></audio><!--进度条--><di......
  • 技嘉 Gigabyte UEFI DualBIOS无限重启
    问题描述:无法开机,按下电源后,主板通电状态灯亮,CPU和显卡风扇转动,显示器显示下图,然后陷入无限重启死循环。始终无法进入BIOS。 可以尝试的解决方案是:关机后,按住主板电......
  • uni-app可以上架ios吗?
    上架iOS需要一个付费688的开发者账号,还没有的话申请一个或者借用。​​申请苹果开发者账号教程​​上架AppStore之前是先安装到苹果手机测试调试好,app能正常运行再上架​​......
  • 【新技术】免688申请ios证书真机调试
    虽然xcode现在可以免证书测试,但是很多跨平台开发者,如果还没有注册苹果开发者账号的话。把它安装到你自己的非越狱手机测试里也没办法。然而,新技术已经来了。你只需要一个普......
  • 中国的开发者如何让 iOS 应用在国外上架?
    一、上架基本需求资料1、苹果开发者账号(公司已有可以不用申请,需要开通开发者功能,每年99美元)2、开发好的APP借助辅助工具appuploader创建证书跟描述文件二、证书,描述文件(借......
  • 【IOS】教你如何在手机端轻松安装ipa文件-(安装器已失效21.10)
    在手机端安装ipa对很多朋友来说是一个很实用的技能,那么如何操作呢?今天向大家推荐一个由小新制作的​​捷径(workflow)​​流程——<​​苹果应用安装器​​>,无需......
  • 如何在mac上用(安装)iOS应用(app)?
    在手机端安装ipa对很多朋友来说是一个很实用的技能,那么如何操作呢?今天向大家推荐一个由小新制作的​​捷径(workflow)​​流程——<​​苹果应用安装器​​>,无需......