1

プログラムがバックグラウンドで繰り返しサウンドを再生できるようにしようとしています。

  1. info.plist に (Required Background Modes) プロパティと (apps play audio) を追加します。

  2. 私のストーリーボードでは、アプリはルート TableViewController を使用して UINarvigation コントローラーを開始します。私のルートTableViewController.mに#importがあります。

  3. ルート TableViewController に NSTimer プロパティ my_timer があります。次のようにviewDidLoadメソッドで設定しました:

my_timer = [NSTimer scheduleTimerWithTimeInterval:1.0 target:self selector: @selector(doMyFunction) userInfo:nil repeats:YES];

  1. doMyFunction メソッドには、playSystemSound(1003) があります。

私のアプリは、フロント モードでは期待どおり定期的にサウンドを再生しますが、バック グラウンド モードでは決して再生されません。何が正しく行われていないのかわかりません。助けてくれてありがとう。

4

1 に答える 1

2

playSystemSound がバックグラウンドでオーディオを再生することはありません。これを行うには、ViewDidLoad の直後に、メイン ソース コードでこの関数を呼び出します。

-(void)createAudioSession 
{

    // Registers this class as the delegate of the audio session.
    [[AVAudioSession sharedInstance] setDelegate: self];

    // Use this code instead to allow the app sound to continue to play when the screen is locked.
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];

    NSError *myErr;

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:&myErr];

}

それで

AVAudioPlayer * FXPlayer;

FXPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:resourcePath] error:&err];

FXPlayer.numberOfLoops = -1; // will loop forever

[FXPlayer play];
于 2013-01-20T12:10:47.940 に答える