簡體   English   中英

iOS App無法在后台運行

[英]IOS App does not run in background

我希望我的應用程序音頻在后台運行,但我已按照本教程進行操作,但沒有用。 當我按下主屏幕按鈕時,我的應用程序音頻仍然停止,並且我意識到它沒有調用“ applicationDidBecomeActive”或“ applicationDidEnterBackground”(即使我禁用了“應用程序不在后台運行”設置,問題仍然存在)。 過去一周我一直在處理這個問題。

到目前為止,我已經完成了以下步驟:

-添加了AVFoundation框架並聲明了

#import <AVFoundation/AVFoundation.h>

在此處輸入圖片說明

-在音頻中設置AVAudioSession

NSString *audioName = [NSString stringWithFormat:@"audio%d", (nimages)];
        NSString *soundPath =[[NSBundle mainBundle]pathForResource:audioName ofType:@"mp3"];
        NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
        NSError *error = nil;
        AVAudioPlayer *audio = nil;
        audio = [[AVAudioPlayer alloc]initWithContentsOfURL:soundURL error:&error];
        audio.numberOfLoops = -1;
        audio.volume = 0.9;
        if (error) {
            NSLog(@"%@", [error localizedDescription]);
            NSLog(@"Unable to load file");                
        }else {
            //Make sure the system follows our playback status
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
            [[AVAudioSession sharedInstance] setActive: YES error: nil];
            //Load the audio into memory
            [audio prepareToPlay];
        }

-在plist中添加行

在此處輸入圖片說明

更新:

我的應用程序是一個音頻應用程序,即使它進入后台,用戶仍可以播放我的應用程序的特定音軌。 可能嗎?

我認為必須積極播放音頻才能使其正常工作。 如果您的應用程序實際上不是音頻應用程序,則可以嘗試將其設置為VoIP。 這不需要活動的連接。 它還不會阻止其他想要使用音頻的應用程序。 只需將后台模式設置為“應用程序提供IP語音服務”,然后它將一次在后台運行長達十分鍾; 您可以連接VoIP套接字並將內容推送到其中,如果您希望使其保持活動狀態或也可以喚醒它。

我建議您單擊此處閱讀“ Apple人機界面”

在少數情況下,允許該應用程序在后台運行,例如(音頻,下載,更新..etc),您可以在我提供的鏈接中找到全部內容。

使用它會運行它的運行代碼。

NSURL * url = [NSURL fileURLWithPath:[NSString stringWithFormat:@“%@ / notification.wav”,[[NSBundle mainBundle] resourcePath]]];

            NSError *error;

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback錯誤:無];

            AudioSessionInitialize( NULL, NULL, NULL,NULL);
            UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
            AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
            UInt32 value = YES;
            AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(value), &value);
            AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck, sizeof(value), &value);
            [[AVAudioSession sharedInstance] setActive: YES error: nil];
            AudioSessionSetActive(true);



            // self.appAudioPlayer=audioPlayer;
             AVAudioPlayer  *audioPlayer101 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
            self.audioPlayerForPlay.delegate = self;
            self.audioPlayerForPlay.numberOfLoops=-1;
            [self.audioPlayerForPlay prepareToPlay];
            UInt32 doChangeDefaultRoute = 1;
            AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);





            self.audioPlayerForPlay = audioPlayer101;
            [audioPlayer101 release];
            [self.audioPlayerForPlay play];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM