AVAudioPlayer后台播放

本文详细介绍了如何在iOS应用中利用AVAudioPlayer进行音频播放,并探讨了实现背景播放的技术细节。重点包括初始化播放、音频文件管理、循环播放控制、声音同步与管理,以及在应用退出后保持播放状态的方法。通过配置AVAudioSession来确保在应用后台运行时音频播放不受干扰。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境:Xcode5, iOS7


关于AVAudioPlayer播放功能,用到了记录一下:

    self.playerAV = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:self.urlString ofType:@"mp3"]] error:nil];

    _playerAV.delegate = self;

    [_playerAV setNumberOfLoops:0];

    [_playerAV prepareToPlay];

    [_playerAV play];


初始化播放看document,问题不大,包括了播放时间、播放资源、循环、声音的同步、暂停、继续、音量等。

An instance of the AVAudioPlayer class, called an audio player, provides playback of audio data from a file or memory.

Apple recommends that you use this class for audio playback unless you are playing audio captured from a network stream or require very low I/O latency. For an overview of audio technologies, see Audio & Video Starting Point and “Using Audio” in Multimedia Programming Guide.

Using an audio player you can:

  • Play sounds of any duration
  • Play sounds from files or memory buffers
  • Loop sounds
  • Play multiple sounds simultaneously, one sound per audio player, with precise synchronization
  • Control relative playback level, stereo positioning, and playback rate for each sound you are playing
  • Seek to a particular point in a sound file, which supports such application features as fast forward and rewind
  • Obtain data you can use for playback-level metering


The AVAudioPlayer class lets you play sound in any audio format available in iOS and OS X. You implement a delegate to handle interruptions (such as an incoming phone call on iOS) and to update the user interface when a sound has finished playing. The delegate methods are described in AVAudioPlayerDelegate Protocol Reference.

To play, pause, or stop an audio player, call one of its playback control methods, described in “Configuring and Controlling Playback.”

This class uses the Objective-C declared properties feature for managing information about a sound—such as the playback point within the sound’s timeline, and for accessing playback options—such as volume and looping.

To configure an appropriate audio session for playback on iOS, refer to AVAudioSession Class Reference and AVAudioSessionDelegate Protocol Reference. To learn how your choice of file formats impacts the simultaneous playback of multiple sounds, refer to “iOS Hardware and Software Audio Codecs” in Multimedia Programming Guide.


这里记录下程序退出后继续播放,在播放前设置:

    //play audio in background

    AVAudioSession *session = [AVAudioSession sharedInstance];

    [session setActive:YES error:nil];

    [session setCategory:AVAudioSessionCategoryPlayback error:nil];

直接调用AVAudioSession单例,设置播放playback,


另外plist里要添加一项:Required background modes的App plays audio or streams audio/video using AirPlay

而xcode5的target下得capabilities里已添加显示



设置后再退出app,相关播放不受影响。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值