DKAudioPlayer
适用于 iOS(iPhone 和 iPad)的音频播放组件,具有简洁灵活的界面设计。它可以添加到表视图的头视图。
GIF 示例
YouTube 示例
屏幕截图
要求
在 iOS 9 及以上版本上进行测试。但可能支持 iOS 6 及以上版本。
安装
DKAudioPlayer可以通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中:
pod 'DKAudioPlayer'
如何使用
有一个适用于iPhone和iPad的通用应用程序的示例项目。播放器可以拉伸到父视图控制器的宽度。
首先创建一个播放器对象实例
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:@"sample.mp3" ofType:nil];
if ( audioFilePath ) {
// The width of a player is equal to the width of a parent view
_audioPlayer = [[DKAudioPlayer alloc] initWithAudioFilePath:audioFilePath width:self.view.frame.size.width height:0];
// Setting the origin of an audio player
CGRect frame = _audioPlayer.frame;
frame.origin = CGPointMake(0, self.view.frame.size.height - self.tabBarController.tabBar.frame.size.height);
_audioPlayer.frame = frame;
// Adding player on a view
[self.view addSubview:_audioPlayer];
}
}
然后在对某些动作进行操作时,可以显示或隐藏播放器
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (! _audioPlayer.isVisible) {
[self.audioPlayer showAnimated:YES];
}
}
- (IBAction)showHideClicked:(id)sender
{
if (_audioPlayer.isVisible) {
[_audioPlayer hideAnimated:YES];
}
else {
[_audioPlayer showAnimated:YES];
}
}
如果将其添加到您应用程序的plist文件中,它将在后台运行
所需的背景模式 = 应用播放音频或通过AirPlay流式传输音频/视频
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
作者
Dennis Kutlubaev,[email protected]
许可
DKAudioPlayer可在MIT许可下获得。有关更多信息,请参阅LICENSE文件。