- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有一个应用程序可以播放一堆歌曲,而用户可以翻阅一本漫画书。我使用 AVAudioPlayer 并将其设置为按固定顺序播放歌曲。因此,当一首歌曲结束时,下一首歌曲将播放。当应用程序打开时,这可以完美运行。当应用程序在后台时会出现问题。我将应用程序设置为在后台播放,效果很好。因此,当用户按下主屏幕时,音乐会继续播放。歌曲结束时会出现问题,假设像打开应用程序时一样播放下一首歌曲。相反,什么也没有发生。根据我的 NSLog 语句,正在调用正确的方法,但没有任何 react 。这是我的代码:
- (void)audioPlayerDidFinishPlaying: (AVAudioPlayer *)player successfully: (BOOL) flag {
NSLog(@"Song finished");
if ([songSelect isEqualToString: @"01icecapades"]) {
isPlay = @"yes";
songSelect = @"02sugarcube";
imageSelect = @"playbanner02";
[self performSelector:@selector(triggerSong) withObject:nil afterDelay:0];
[self performSelector:@selector(triggerBanner) withObject:nil afterDelay:0];
}
else if ([songSelect isEqualToString: @"02sugarcube"]) {
isPlay = @"yes";
songSelect = @"03bullets";
imageSelect = @"playbanner03";
[self performSelector:@selector(triggerSong) withObject:nil afterDelay:0];
[self performSelector:@selector(triggerBanner) withObject:nil afterDelay:0];
}
else if ([songSelect isEqualToString: @"03bullets"]) {
isPlay = @"yes";
songSelect = @"04satanama";
imageSelect = @"playbanner04";
[self performSelector:@selector(triggerSong) withObject:nil afterDelay:0];
[self performSelector:@selector(triggerBanner) withObject:nil afterDelay:0];
}
else if ([songSelect isEqualToString: @"04satanama"]) {
isPlay = @"yes";
songSelect = @"05uglyjoke";
imageSelect = @"playbanner05";
[self performSelector:@selector(triggerSong) withObject:nil afterDelay:0];
[self performSelector:@selector(triggerBanner) withObject:nil afterDelay:0];
}
else if ([songSelect isEqualToString: @"05uglyjoke"]) {
isPlay = @"yes";
songSelect = @"01icecapades";
imageSelect = @"playbanner01";
[self performSelector:@selector(triggerSong) withObject:nil afterDelay:0];
[self performSelector:@selector(triggerBanner) withObject:nil afterDelay:0];
}}
- (void)triggerSong {
NSLog(@"triggerSong called");
NSString *path;
NSError *error;
// Path the audio file
path = [[NSBundle mainBundle] pathForResource:songSelect ofType:@"mp3"];
// If we can access the file...
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
// Setup the player
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
//player = [initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
[player setDelegate: self];
// Set the volume (range is 0 to 1)
player.volume = 1.0f;
[player prepareToPlay];
[player setNumberOfLoops:0];
[player play];
NSLog(@"player play");
[error release];
player.delegate = self;
// schedules an action every second for countdown
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateTimeLeft) userInfo:nil repeats:YES];
}}
//Make sure the system follows our playback status
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
最佳答案
Relevant discussion
简短的回答:
您需要在第一个 View Controller 的 init 或 viewDidLoad 方法中使用此代码:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
-(id)init{
self = [super initWithNibName:@"MediaPlayerViewController" bundle:nil];
if(self){
//Need this to play background playlist
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
//MUSIC CLIP
//Sets up the first song...
NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"Music" ofType:@"mp3"];
if(musicPath){
NSURL *musicURL = [NSURL fileURLWithPath:musicPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
[audioPlayer setDelegate:self];
}
}
return self;
}
-(IBAction)playAudioFile:(id)sender{
if([audioPlayer isPlaying]){
//Stop playing audio and change test of button
[audioPlayer stop];
[sender setTitle:@"Play Audio File" forState:UIControlStateNormal];
}
else{
//Start playing audio and change text of button so
//user can tap to stop playback
[audioPlayer play];
[sender setTitle:@"Stop Audio File" forState:UIControlStateNormal];
}
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[audioButton setTitle:@"Play Audio File" forState:UIControlStateNormal];
[playRecordingButton setTitle:@"Play Rec File" forState:UIControlStateNormal];
//PLAY THE SECOND SONG
NSString *musicPath2 = [[NSBundle mainBundle] pathForResource:@"Music2" ofType:@"mp3"];
if(musicPath2){
NSURL *musicURL2 = [NSURL fileURLWithPath:musicPath2];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL2 error:nil];
[audioPlayer setDelegate:self];
NSLog(@"Play it again: \n%@", musicPath2);
[audioPlayer play];
}
}
关于iOS - AVAudioPlayer 在后台不会继续播放下一首歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32104578/
我是 Android 编程的新手,我正在尝试制作一个简单的媒体播放器应用程序,该应用程序可以从用户的 SD 卡中提取歌曲并使用户能够播放歌曲。我面临的问题是,当一首歌正在播放时,如果用户点击其他歌曲,
使用 AVAudioPlayer 我们可以使用这种方法循环播放多首歌曲: - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player suc
这是一道系统设计题。 假设我们有一个服务可以在听到歌曲时通知我们。让我们设计一个新服务,它能够返回过去 24 小时内听得最多的前 K 首歌曲。假设我们有大约 10 亿首歌曲和大约 2 亿用户。 将您的
由于我是 Flash CS6 actionscript 3 的新手,有人可以具体地举一个详细的例子,向我解释一下如何向我的播放器添加 2 首歌曲,并且仍然使用当前的播放和暂停按钮来控制它们吗? 非常感
我是一名优秀的程序员,十分优秀!