简体   繁体   English

iOS App无法在后台运行

[英]IOS App does not run in background

I want my app audio to run in background and I had followed this tutorial but it doesn't worked. 我希望我的应用程序音频在后台运行,但我已按照本教程进行操作,但没有用。 My app audio still stopped when I pressed home button and I realised it did not call "applicationDidBecomeActive" or "applicationDidEnterBackground" (problem continued even though I had disabled the setting "Application does not run in background") too. 当我按下主屏幕按钮时,我的应用程序音频仍然停止,并且我意识到它没有调用“ applicationDidBecomeActive”或“ applicationDidEnterBackground”(即使我禁用了“应用程序不在后台运行”设置,问题仍然存在)。 I had been dealing with this for the past week. 过去一周我一直在处理这个问题。

So far I had done this steps: 到目前为止,我已经完成了以下步骤:

-Added AVFoundation framework and declared -添加了AVFoundation框架并声明了

#import <AVFoundation/AVFoundation.h>

在此处输入图片说明

-Set up AVAudioSession in audio -在音频中设置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];
        }

-Add rows in plist -在plist中添加行

在此处输入图片说明

Updated: 更新:

My app is an audio app which the user can play specific soundtrack of my app even though it enter the background. 我的应用程序是一个音频应用程序,即使它进入后台,用户仍可以播放我的应用程序的特定音轨。 Is it possible? 可能吗?

I think it has to be actively playing audio for this to work. 我认为必须积极播放音频才能使其正常工作。 If your app isnt' actually an audio application, you might try setting the application up as VoIP instead; 如果您的应用程序实际上不是音频应用程序,则可以尝试将其设置为VoIP。 this doesn't require an active connection. 这不需要活动的连接。 It also won't block other applications wanting to use audio. 它还不会阻止其他想要使用音频的应用程序。 Just set the background mode is "App provides Voice over IP services", and then it'll run in the background for up to ten minutes at a time; 只需将后台模式设置为“应用程序提供IP语音服务”,然后它将一次在后台运行长达十分钟; you could connect a VoIP socket and push content to it if you want to keep it alive or wake up also. 您可以连接VoIP套接字并将内容推送到其中,如果您希望使其保持活动状态或也可以唤醒它。

I suggest you read "Apple Human Interface" by clicking here 我建议您单击此处阅读“ Apple人机界面”

There are limited situations where the App is allowed to work in background, such as (Audio, Downloading, Update..etc), you can find it all at the link I provided you. 在少数情况下,允许该应用程序在后台运行,例如(音频,下载,更新..etc),您可以在我提供的链接中找到全部内容。

Use this it will work its a running code. 使用它会运行它的运行代码。

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

            NSError *error;

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [[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