繁体   English   中英

在iOS的背景音乐中播放音乐

[英]Play music in background in ios

我正在使用AVAudioPlayer播放音乐文件,如果我使用笔尖文件,则在后台播放效果很好,但是当我使用情节提要尝试相同的代码时,音乐播放停止了。 我无法理解为什么使用情节提要时其在后台停止播放的原因。

我正在使用此代码

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

有什么可以帮助我在使用分镜脚本的背景下进行播放的吗? 提前致谢。

我们试试吧:

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

暂无
暂无

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

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