0

私のコード:

NSString *soundName = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];
NSURL *soundURL = [NSURL fileURLWithPath:soundName];
NSError *error = [[NSError alloc] init];
self.backgroundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];

if (self.backgroundPlayer == nil) {
    NSLog(@"error = %@",[error description]);
} else {
    [self.backgroundPlayer setDelegate:self];
    [self.backgroundPlayer setNumberOfLoops:HUGE_VAL];
    [self.backgroundPlayer setVolume:0.5f];
    [self.backgroundPlayer prepareToPlay];
    if ([self.backgroundPlayer prepareToPlay]) {
        if ([self.backgroundPlayer play]) {
            NSLog(@"playing");
        }
    } else {
        NSLog(@"error!");
    }
}

iPhoneをサイレントモードに切り替えても音は鳴ります。どうすればこの問題を解決できますか?

4

3 に答える 3

6

私はこれを試していませんが、ファイルを再生する前にオーディオ カテゴリを設定できます。

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil];
于 2013-02-25T12:06:41.770 に答える
2
CFStringRef state;

UInt32 propertySize = sizeof(CFStringRef);

AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if(CFStringGetLength(state) == 0)
{
//SI

    else
    {
    //NOT SILENT

    }

電話の状態を検出してプレーヤーを停止できます.. :)

于 2013-02-25T12:11:01.327 に答える
1

オーディオ セッションには AVAudioSessionCategoryAmbient を使用する必要があります。オーディオ セッション プログラミング ガイドを参照してください。

于 2013-02-25T12:07:48.367 に答える