【问题标题】:Play music in background in ios在 ios 中播放背景音乐
【发布时间】:2014-05-04 04:55:10
【问题描述】:

我正在使用 AVAudioPlayer 流式传输音乐文件,如果我使用 nib 文件,它在后台播放良好,但是当我使用情节提要尝试相同的代码时,音乐播放停止。当我使用故事板时,我无法理解它停止在后台播放的原因。

我正在使用此代码

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[audioPlayer setVolume:0.1];
[audioPlayer play];

任何人都可以帮助我使用故事板在后台播放它。提前致谢。

【问题讨论】:

    标签: ios iphone audio-player


    【解决方案1】:

    让我们试试吧:

    @implementation YourViewController
    {
      AVAudioPlayer *audioPlayer;
    }
    
    // if your are using ARC, after exiting this method, system will kill your audioPlayer. This is the reason why you can not play the music. 
    // in your method:
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 2011-11-23
      相关资源
      最近更新 更多