NSUserDefaults를 사용하여 음악 재생 상태를 저장할 수 있으므로보기가로드 될 때 음악이 재생되지 않습니다.
if ([[NSUserDefaults standardUserDefaults] stringForKey:@"music"] == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"drums" ofType:@"mp3"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
theAudio.delegate = self;
theAudio.numberOfLoops = -1;
[theAudio play];
[[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"]; }
앱을 종료 할 때이 키를 다시 초기화해야합니다. 그렇지 않으면 플레이어가 앱을 종료하고 다음에 앱을 다시 시작하면 @ "음악"문자열이 0이되지 않습니다. projectAppDelegate.m 파일
이동 : 음악이 응용 프로그램을 통해 재생되는
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"music"]; }
경우, 안전을 위해 다음과 같이 코드를 연주 음악을 넣어. projectAppDelegate.h 파일에 AVAudioPlayer * theAudio를 선언하는 것을 잊지 마십시오.
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSString *path = [[NSBundle mainBundle] pathForResource:@"drums" ofType:@"mp3"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
theAudio.delegate = self;
theAudio.numberOfLoops = -1;
[theAudio play];
[[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"]; }
여기에 약간의 비트가 누락되었을 수 있습니다. 몇 가지 테스트를 실행하여 원하는 방식으로 테스트하십시오. 방금 내 애플 리케이션을 시도하고 그것은 나를 위해 일했다.
'theAudio'란 무엇입니까? –