首頁  >  問答  >  主體

iOS app如何在进入背景模式的时候依然播放音乐

iOS app如何在进入背景模式的时候依然播放音乐

就像douban电台那样, 不但在退出程序以后依然有音乐播放, 就连双击home按钮以后左边的音乐控制栏都变成douban电台的图标了, 请问这是如何做到的?

伊谢尔伦伊谢尔伦2719 天前970

全部回覆(1)我來回復

  • PHP中文网

    PHP中文网2017-04-17 11:13:41

    1.后台播放是在Info.plistUIBackgroundModes里加上audio 参考https://developer.apple.com/library/i...

    2.“连双击home按钮以后左边的音乐控制栏都变成douban电台的图标了”,这是将自己程序中的某对象注册成相应远程控制消息的响应者。当用户在控制栏点击prev、next、play/pause时,程序会接到相应的消息,接到时你要按照消息对播放做相应控制。
    视图控制器中注册响应者:

    - (void) viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];
    }
    
    - (void) viewWillDisappear:(BOOL)animated {
        [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
        [self resignFirstResponder]; 
        [super viewWillDisappear:animated];
    }
    
    - (BOOL) canBecomeFirstResponder {
        return YES;
    }

    同时视图控制器需实现:

    - (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent;

    3.而让控制栏显式标题又是另外一码事了,你需要在程序中调用媒体中心接口,设置标题和封面。参考http://stackoverflow.com/questions/10...

    4.从ID3获取专辑封面和标题是第四码事。。

    回覆
    0
  • 取消回覆