简体   繁体   English

(iOS)音频播放器作为应用程序中所有父视图控制器上的子视图控制器

[英](iOS) Audio player as child viewcontroller on all parent view controllers in the app

I am developing this music streaming application for iOS. 我正在为iOS开发此音乐流应用程序。 Everything is working great but haven't yet implemented the audio player functionality. 一切工作正常,但尚未实现音频播放器功能。 I'm dealing with some problems on how to have the same audio player as a child view controller in all of my view controllers in the entire app. 我正在处理一些问题,如何在整个应用程序的所有视图控制器中都具有与子视图控制器相同的音频播放器。 I'm not quite sure, but I think I have to use a singleton?. 我不太确定,但是我想我必须使用单例吗?

And how do I set up the audio player to be the same when I change view controller? 更改视图控制器后,如何将音频播放器设置为相同?

在此处输入图片说明

You need to get your terms and concepts right. 您需要正确设置术语和概念。 An audio player is not a view controller, so it will not be a child view controller of anything. 音频播放器不是视图控制器,因此它不是任何东西的子视图控制器。

What you want to do is to create a sound manager object and make it a singleton. 您要做的是创建一个声音管理器对象并将其设为单例。 (Do a google search on the singleton design pattern in Cocoa). (对可可中的单例设计模式进行Google搜索)。

A singleton typically has a class method that let's you fetch it: 单例通常具有一个类方法,让您获取它:

+ (MySoundManager *) sharedSoundManager;
{
  static dispatch_once_t once;
  static id _theSharedSoundManager;
  dispatch_once(&once, ^{
    _theSharedSoundManager = [[self alloc] init];
  });

  return _theSharedSoundManager;
}

And within your VCs, you get a pointer to your shared sound manager with a call like this: 在VC中,您可以通过以下调用获得指向共享声音管理器的指针:

[MySoundManager sharedSoundManager];

Then add methods to your sound manager to play sounds at the request of it's clients. 然后向您的声音管理器添加方法,以根据其客户的要求播放声音。

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

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