iphone - 后台模式下的音频切换 AVFoundation - Objective-c

标签 iphone objective-c ios ios4 avfoundation

我有一个名为 LocalAudio 的音频对象数组。为了播放音频,我使用 AVAudioPlayer。我已经实现了委托(delegate)方法:audioPlayerDidFinishPlaying:,我在其中放置了代码来切换 LocalAudio 数组中的下一个声音。

 -(void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
 {
    [self switchTrack:nextAudioIndex];
 }

音频在后台播放,但声音不切换。那么如何在后台模式下切换音频呢? 为了进行调试,我使用 iPad 2。

最佳答案

我使用相同的逻辑并且它有效,所以其他地方一定是不正确的。

编辑: 示例

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    if (flag == NO)
        NSLog(@"Playback finished unsuccessfully");

    [player setCurrentTime:0.0];


    [backgroundMusicPlayer release];
    backgroundMusicPlayer = nil;


    if (![music isEqualToString:@"10"]) {
        [self playMusicWithKey:[NSString stringWithFormat:@"%i", ([music intValue] + 1)] timesToRepeat:0];
    }
}

哪里

- (void) playMusicWithKey:(NSString*)theMusicKey  timesToRepeat:(NSUInteger)theTimesToRepeat {

    NSError *error;
    NSString *path = [musicLibrary objectForKey:theMusicKey];

    // Initialize the AVAudioPlayer

    if (backgroundMusicPlayer == nil) {

                backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
                [backgroundMusicPlayer setDelegate: self];
                [backgroundMusicPlayer setVolume:backgroundMusicVolume];
                musicLenght = backgroundMusicPlayer.duration;
        }

    // If the backgroundMusicPlayer object is nil then there was an error
    if(!backgroundMusicPlayer) {
        NSLog(@"ERROR SoundManager: Could not play music for key '%@'", theMusicKey);
        return;
    }       

    // Set the number of times this music should repeat.  -1 means never stop until its asked to stop
    [backgroundMusicPlayer setNumberOfLoops:theTimesToRepeat];

    // Play the music
    [backgroundMusicPlayer prepareToPlay];
    [backgroundMusicPlayer play];

}

关于iphone - 后台模式下的音频切换 AVFoundation - Objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7624986/

相关文章:

ios - 需要一些关于 iOS 项目面向对象设计的建议

iphone - 开发者拒绝状态?

objective-c - 当 UIScrollView inside UIScrollView 时滚动

iphone - 如何在 iOS 中为应用程序图标制作动画?

iphone - 在计算器屏幕上逐位删除数字

IOS,在 UIScrollView 内的 UILabel 上设置手势点击

ios - objective-c - ScrollView 内的多个 ScrollView

iphone - UILabel 中的内阴影

iphone - 如何从 json 对象中提取实际的 NSString 作为 NSArray

iphone - 如何从我的 iPhone App 文档目录中选择所有图像