0

私のアプリにはAVAudioPlayerのインスタンスがあり、これは mp3 のシーケンスを再生し、次のものに切り替えinitWithDataます:

ユーザーが画面をオフにしたり、バックグラウンドに移動したりしても、ユーザーがプレーヤーを聴き続けられるようにしたい. ユーザーがタップしたときに他のオーディオに切り替えるために使用できることは知ってremotecontroleventsいますが、iPod プレーヤーのように自動的に切り替えることはできますか?

4

1 に答える 1

4

AVAudioPlayerDelegateプロトコルを使用する必要があります。まず、インターフェースにそれを含めます。

@interface MyAppViewController : UIViewController <AVAudioPlayerDelegate> {
    AVAudioPlayer   *yourPlayer;
}

@property (nonatomic, retain) AVAudioPlayer *yourPlayer;

次に、実装では:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
//This method is called once the file has finished playing
//Insert code here to play the next file
}

メソッド(またはメソッドを配置する場所)でdelegateofyourPlayerを設定することを忘れないselfでください。viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad]

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];
    }

    self.yourPlayer = [[AVAudioPlayer alloc] init];
    self.yourPlayer.delegate = self;
}

また、info.plistの必須のバックグラウンドモードアプリがオーディオを再生するように変更する必要があります

リモートコントロールイベントの登録を解除することを忘れないでください(viewDidUnload):

[[UIApplication sharedApplication] endReceivingRemoteControlEvents]
于 2012-04-15T13:37:45.303 に答える