OS可以在appDelegate上使用AVAudioPlayer

OS可以在appDelegate上使用AVAudioPlayer

本文介绍了iOS可以在appDelegate上使用AVAudioPlayer吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个选项卡的TabBarController,我想在两个选项卡上播放音乐.现在我在主appDelegate

I have a TabBarController with two tabs and I want to play music on both tabs. Right now I have my code on the main appDelegate

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                         pathForResource:@"My Song"
                                         ofType:@"m4a"]]; // My Song.m4a

NSError *error;
    self.audioPlayer = [[AVAudioPlayer alloc]
            initWithContentsOfURL:url
            error:&error];
if (error)
{
    NSLog(@"Error in audioPlayer: %@",
        [error localizedDescription]);
} else {
    //audioPlayer.delegate = self;
    [audioPlayer prepareToPlay];
}

但是我在UIApplicationMain

是否有更好的方法来完成我想做的事情?如果这是我应该怎么做,该从哪里开始检查问题?

Is there a better way to accomplish what I'm trying to do? If this is how I should do it, where do I start checking for problems?

推荐答案

是的,您可以在App Delegate中使用AVAudioPlayer.

yes you can use AVAudioPlayer in App Delegate.

您需要做的是:-在appDelegate.h文件中执行:-

What you need to do is:-In appDelegate.h file do:-

#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

AVAudioPlayer *_backgroundMusicPlayer;
BOOL _backgroundMusicPlaying;
BOOL _backgroundMusicInterrupted;
UInt32 _otherMusicIsPlaying;

制作backgroundMusicPlayer属性并对其进行合成.

Make backgroundMusicPlayer property and sythesize it.

appDelegate.m文件中执行:-

在FinishLaunching方法中添加这些行

Add these lines in did FinishLaunching method

NSError *setCategoryError = nil;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&setCategoryError];

    // Create audio player with background music
    NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:@"SplashScreen" ofType:@"wav"];
    NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
    NSError *error;
    _backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
    [_backgroundMusicPlayer setDelegate:self];  // We need this so we can restart after interruptions
    [_backgroundMusicPlayer setNumberOfLoops:-1];   // Negative number means loop forever

现在实现委托方法

#pragma mark -
#pragma mark AVAudioPlayer delegate methods

- (void) audioPlayerBeginInterruption: (AVAudioPlayer *) player {
    _backgroundMusicInterrupted = YES;
    _backgroundMusicPlaying = NO;
}

- (void) audioPlayerEndInterruption: (AVAudioPlayer *) player {
    if (_backgroundMusicInterrupted) {
        [self tryPlayMusic];
        _backgroundMusicInterrupted = NO;
    }
}

- (void)tryPlayMusic {

    // Check to see if iPod music is already playing
    UInt32 propertySize = sizeof(_otherMusicIsPlaying);
    AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &_otherMusicIsPlaying);

    // Play the music if no other music is playing and we aren't playing already
    if (_otherMusicIsPlaying != 1 && !_backgroundMusicPlaying) {
        [_backgroundMusicPlayer prepareToPlay];
        if (soundsEnabled==YES) {
            [_backgroundMusicPlayer play];
            _backgroundMusicPlaying = YES;


        }
    }
}

这篇关于iOS可以在appDelegate上使用AVAudioPlayer吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 06:05