转自: http://xiongzhend.blog.163.com/blog/static/6409850120108821651530/

在 iPhone 静音情况下,播放 MP3 文件的代码

在 iPhone 静音的情况下,iOS 系统自带的 iPod 应用可以正常播放音乐,但很多开发者的第三方应用中却不行,下面这段代码能保证您的应用在 iPhone 静音情况下,照常播放 MP3 文件。

- (IBAction) record:(id)sender
{
    
    NSError *error = nil;
    BOOL b = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&error];
    [[AVAudioSession sharedInstance] setDelegate:self];
    if(b){
        NSLog(@"set audio session category record victory%d",b);
    }else {
        NSLog(@"error=================%@",error);
    }
    b = [[AVAudioSession sharedInstance] setActive:YES error:&error];
    if(b){
        NSLog(@"set active yes %d",b);
    }else {
        NSLog(@"error=================%@",error);
    }
    NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"voice.caf"];
    //NSLog(@"filePath=========================%@",filePath);
    self.recordFileName = filePath;
    NSURL *url = [NSURL fileURLWithPath:filePath];
    
    AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];
    mRecorder = recorder;
    [recorder setDelegate:self];
    [recorder prepareToRecord];
    
    recorder.meteringEnabled = YES;
    
    BOOL audioHWAvailable = [[AVAudioSession sharedInstance] inputIsAvailable];
    
    // start recording
    [recorder recordForDuration:(NSTimeInterval) 3];
}

播放相对简单:

- (IBAction) play:(id)sender
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [[AVAudioSession sharedInstance] setDelegate:self];
    NSError *error = nil;
    BOOL b = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&error];
//    if(b){
//        NSLog(@"====================%d",b);
//    }else {
//        NSLog(@"error=================%@",error);
//    }
    
    b = [audioSession setActive:YES error:&error];
//    if(b){
//        NSLog(@"====================%d",b);
//    }else {
//        NSLog(@"error=================%@",error);
//    }
    
    NSURL *url = [NSURL fileURLWithPath:self.recordFileName];
    //NSLog(@"url============================%@",url);
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    player.meteringEnabled = YES;
    player.volume = 1.0;
    player.delegate = self;
    [player prepareToPlay];
    [player play];
    [pool release];
}

在 iPhone 静音情况下,播放 MP3 文件的代码相关推荐

  1. iPhone上的lrc播放器可以在播放mp3文件时显示歌词

    https://apps.apple.com/cn/app/%E6%96%B0lrc%E6%92%AD%E6%94%BE%E5%99%A82/id1535214306 长久以来,在iPhone上播放l ...

  2. 《新lrc播放器2》-iPhone上可以显示lrc歌词的播放器可以在播放mp3文件时显示lrc文件中的歌词的播放器

    https://apps.apple.com/cn/app/%E6%96%B0lrc%E6%92%AD%E6%94%BE%E5%99%A82/id1535214306 以前,在iPhone上播放lrc ...

  3. linux系统么播放音乐,在linux环境下播放MP3

    在linux环境下播放MP3 1:解压的顺序 (1)zlib-1[1].1.4.tar.tar (2)libid3tag-0.5.1b.tar.gz (3)libmad-0.15.1b.tar.gz ...

  4. centos 7 播放MP3文件

    如何在centos7下播放mp3 音频文件 centos 7 是无法直接播放mp3文件的.我是安装的vlc 播放器来播放. 首先linux 系统的硬件设备要有声卡支持才能播放音频. #> yum ...

  5. 使用go语言GUI库实现对mp3文件的播放1(简单的播放mp3文件)

    使用go语言GUI库实现对mp3文件的播放1(简单的播放mp3文件) 使用beep播放mp3文件(10num) 使用go语言GUI库fyne实现音乐播放器 要是想使用go语言实现播放mp3需要借助be ...

  6. 使用DirectSound播放MP3文件

    http://www.cppblog.com/codejie/archive/2009/03/26/77916.html 使用DirectSound播放MP3文件 将对MP3的支持代码加入到DSoun ...

  7. php调用播放mp3文件失败,需要修复从PHP传输私有MP3文件的错误

    我有一些私人音乐文件的集合,我不想让人们很难链接,所以我创建了一个基于访问令牌的系统,以允许一个人使用的应用场景,这个应用场景必须由人类根据请求产生.这只是确保正在播放的音乐不会被链接.我在使用PHP ...

  8. java循环播放声音代码_java-无法循环播放MP3文件

    好的,我正在尝试使声音文件在应用程序打开时永远循环播放.目前,这是我运行声音文件的一部分代码. public class Game { public static void main(String[] ...

  9. 如何使Ubuntu播放MP3文件

    Because of licensing issues, Ubuntu is unable to play MP3s out of the box. We'll show you how to pla ...

最新文章

  1. python如何保存列表_Python 基础知识全篇-列表(Lists)
  2. java 2类与对象[学堂在线]
  3. javascript注意点(1)
  4. CodeForces - 1245C Constanze's Machine(思维+找规律)
  5. 进程隐藏与进程保护(SSDT Hook 实现)(一)
  6. 本地启动 Hybris 服务器调试模式后,监听在 8000 端口
  7. SAP CRM WebClient UI的configuration按钮点击之后,发生了什么事情
  8. 题目1452:搬寝室(dp题目)
  9. PDOStatement::bindParam的一个陷阱
  10. 微软宣布免费 Web 版 Office 2010发布日期
  11. linux HZ 值_Linux操作系统中进程的操作命令(ps,kill,keep)
  12. DNS服务器上的IP地址修改
  13. 【语音识别】基于matlab特定人的语音识别分辨【含Matlab源码 534期】
  14. Nginx与TCP协议的关系
  15. socket pro
  16. 如何查看台式机计算机网络密码,如何从计算机检查已知的WiFi密码
  17. itext7生成pdf页脚页码
  18. SQLServer修改表数据
  19. jQuery选择器$()
  20. Python uniform() 函数

热门文章

  1. U盘的FAT32/NTFS/exFAT文件系统类型区别详解
  2. PyCharm2019 激活码,亲测有效
  3. 小米 红米 解账户锁 线刷包 教程 MIUI12 MIUI13
  4. 十个“三角形”汉字,好看、不好认
  5. Chapter 1-2: Installing Python - Installing Python 3 on a Macintosh | Python for Everybody 讲义笔记_En
  6. DWORD *类型的实参与SIZE_T *类型的形参不兼容,求指教
  7. Unity 工具类 之 Blender 的下载和简单使用(为捏脸做准备)
  8. Verilog语法之任务Task与函数Function
  9. 从政务云到国资云,折射国内云计算市场三大趋势
  10. 如何开启内存超频?来看