环境: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,相关播放不受影响。