【问题标题】:iOS add audio playing via AVPlayer to OS default player?iOS将通过AVPlayer播放的音频添加到OS默认播放器?
【发布时间】:2015-06-24 13:10:09
【问题描述】:

所以我们的想法是 - 当我们在 iPhone 的音乐应用中播放音频并按下主页按钮然后从屏幕底部滑动时,我们可以看到音频在默认 OS 播放器中播放,带有播放/暂停控件并且音频继续播放。

现在我需要做的是,我在我的应用程序中使用AVAudioPlayer 播放音频,如果用户按下主页按钮,音频需要继续播放,就像音乐应用程序一样。 但我不知道如何在操作系统默认播放器中添加音频?任何帮助或建议将不胜感激。

编辑:我需要使用流媒体播放音频,所以我想我需要使用AVPlayer 而不是AVAudioPlayer

【问题讨论】:

    标签: ios objective-c iphone avaudioplayer


    【解决方案1】:

    您可以使用 MPMovieplayerController,因为您很可能会使用 M3U8 播放列表。

    这是http://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/mpmovieplayercontroller_class/index.html的文档

    但它基本上是这样的:

    初始化 MPMoviePlayerController,分配文件类型(流),放入源 url,并将其呈现在视图堆栈中。

    对于背景音频,您必须像此答案 iOS MPMoviePlayerController playing audio in background 中那样使用 AudioSession

    【讨论】:

    • 那么最好或推荐的方式是什么,我的意思是“AVPlayer”或“MPMoviePlayer”。我对这两种服务都没有任何经验,请建议。
    • 嗨,Rob,请再帮忙.. 如果我使用 MPMovieplayerController,那么我必须显示电影播放器​​控制器,但我不想在我的视图中显示任何播放器。我有播放/暂停按钮和其他一些东西的简单视图,当用户点击播放按钮时,我只想播放音频。
    【解决方案2】:

    您可以将此作为后台音频任务执行,这样即使在用户按下主页按钮并且您的应用进入后台后,音频仍会继续播放。首先你创建一个 AVAudioSession。然后在 viewDidLoad 方法中设置一个 AVPlayerObjects 和一个 AVQueuePlayer 数组。 Ray Wenderlich 有一个很棒的教程,详细讨论了所有这些内容http://www.raywenderlich.com/29948/backgrounding-for-ios。您可以设置一个回调方法(观察者方法),以便应用在 - (void)observeValueForKeyPath 中流式传输时向其发送额外的音频数据。

    代码如下所示(来自 Ray Wenderlich 的教程):

    在 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];
    

    在回调方法中:

    - (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);
        }
    }
    

    不要忘记在实现文件中添加视图控制器私有 API 中的成员变量:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多