简体   繁体   English

iOS通过AVPlayer将音频播放添加到OS默认播放器?

[英]iOS add audio playing via AVPlayer to OS default player?

So the idea is - when we play audio in Music app in iPhone and press home button then swipe from bottom of the screen, we can see the audio playing in default OS player with play/pause controls and audio continue play. 因此我们的想法是 - 当我们在iPhone中的音乐应用程序中播放音频并按下主页按钮然后从屏幕底部滑动时,我们可以看到在默认操作系统播放器中播放的音频具有播放/暂停控制和音频继续播放。

Now what i need to do, i play an audio by using AVAudioPlayer in my app and if user press home button, the audio need to play continue as in case of music app. 现在我需要做的是,我在我的应用程序中使用AVAudioPlayer播放音频,如果用户按下主页按钮,音频需要继续播放,如同音乐应用程序一样。 But i have no idea how to add audio in OS default player? 但我不知道如何在OS默认播放器中添加音频? Any help or suggestion would be highly appreciated. 任何帮助或建议将受到高度赞赏。

Edit: I need to play audio using streaming so i guess i need to use AVPlayer instead of AVAudioPlayer 编辑:我需要使用流媒体播放音频,所以我想我需要使用AVPlayer而不是AVAudioPlayer

You could use an MPMovieplayerController since you'll be more than likely be using an M3U8 playlist. 您可以使用MPMovieplayerController,因为您很可能正在使用M3U8播放列表。

Here's the documentation http://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/mpmovieplayercontroller_class/index.html 这是文档http://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/mpmovieplayercontroller_class/index.html

But it basically goes like this: 但它基本上是这样的:

Initialize the MPMoviePlayerController, assign a file type (stream), put the source url, and present it in the view stack. 初始化MPMoviePlayerController,分配文件类型(流),放置源URL,并将其显示在视图堆栈中。

For the background audio, you'd have to use AudioSession like in this answer iOS MPMoviePlayerController playing audio in background 对于背景音频,您必须使用AudioSession,如此答案iOS MPMoviePlayerController在后台播放音频

You can do this as a background audio task so that the audio keeps playing even after the user presses the home button and your app goes into the background. 您可以将此作为后台音频任务执行此操作,以便即使在用户按下主页按钮并且您的应用程序进入后台后,音频仍会继续播放。 First you create an AVAudioSession. 首先,您创建一个AVAudioSession。 Then you set up an array of AVPlayerObjects and a AVQueuePlayer in your viewDidLoad method. 然后在viewDidLoad方法中设置一个AVPlayerObjects数组和一个AVQueuePlayer。 There's a great tutorial by Ray Wenderlich that discusses all of this in detail http://www.raywenderlich.com/29948/backgrounding-for-ios . Ray Wenderlich有一个很棒的教程,详细讨论了这一切http://www.raywenderlich.com/29948/backgrounding-for-ios You can set up a callback method (observer method) so that the app is sent additional audio data as it streams in - (void)observeValueForKeyPath. 您可以设置一个回调方法(观察者方法),以便在应用程序流入时发送其他音频数据 - (void)observeValueForKeyPath。

Here's how the code looks (from Ray Wenderlich's tutorial): 以下是代码的外观(来自Ray Wenderlich的教程):

in viewDidLoad: 在viewDidLoad中:

// Set AVAudioSession
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance]     setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];

// Change the default output audio route
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
  sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);

NSArray *queue = @[
[AVPlayerItem playerItemWithURL:[[NSBundle mainBundle]  URLForResource:@"IronBacon" withExtension:@"mp3"]],
[AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"FeelinGood" withExtension:@"mp3"]],
[AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"WhatYouWant" withExtension:@"mp3"]]];

self.player = [[AVQueuePlayer alloc] initWithItems:queue];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndAdvance;

[self.player addObserver:self
              forKeyPath:@"currentItem"
                 options:NSKeyValueObservingOptionNew
                 context:nil];

in a callback method: 在回调方法中:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"currentItem"])
    {
        AVPlayerItem *item = ((AVPlayer *)object).currentItem;
        self.lblMusicName.text = ((AVURLAsset*)item.asset).URL.pathComponents.lastObject;
        NSLog(@"New music name: %@", self.lblMusicName.text);
    }
}

Don't forget to add the member variables in your view controller's private API in the implementation file: 不要忘记在实现文件的视图控制器的私有API中添加成员变量:

@interface TBFirstViewController ()

@property (nonatomic, strong) AVQueuePlayer *player;
@property (nonatomic, strong) id timeObserver; 
@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM