2013-10-24 132 views
2

使用AVAudioPlayer我試圖在iphone播放器播放時播放聲音。此外當設備被鎖定。 事情是,在iPhone 4s ios 7 - 工作正常。 但是,iPhone 6與6和7 ios沒有任何反應。AVAudioPlayer在鎖定時不會在iPhone 5中播放音頻

-Info.plistRequired background modesPlayer.h

App plays audio or streams audio/video using AirPlay

實現:

@property (nonatomic, strong) AVAudioPlayer *notificationPlayer; 

<AVAudioPlayerDelegate, AVAudioSessionDelegate>#import <AVFoundation/AVFoundation.h>包括

另外在Player.m

//this is called on viewDidLoad 
-(void) prepareSound{ 
    [[AVAudioSession sharedInstance] setDelegate:self]; 
} 
//overriden function 
- (void)playAudio:(NSString *)path{ 
    [[AVAudioSession sharedInstance] 
    setCategory: AVAudioSessionCategoryAmbient 
    withOptions: AVAudioSessionCategoryOptionDuckOthers 
    error: nil]; 

    NSError *error; 
    NSURL *audioURL = [NSURL fileURLWithPath:path]; 

    self.notificationPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:audioURL error:&error]; 
    self.notificationPlayer.delegate = self; 
    [self.notificationPlayer prepareToPlay]; 
    [self.notificationPlayer setVolume:1.0]; 
    [self.notificationPlayer play]; 
    if (error) { 
     NSLog(@"error %@",[error localizedDescription]); 
    } 
} 

- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ 
    [player stop]; 
    [[AVAudioSession sharedInstance] setActive:NO withOptions:0 error:nil]; 
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient 
            withOptions: 0 
              error: nil]; 
    [[AVAudioSession sharedInstance] setActive:YES withOptions: 0 error:nil]; 
} 
//Call from here 
-(void) customMethod{ 
    [self playAudio:[[NSBundle mainBundle] pathForResource:@"3" ofType:@"wav"]]; 
} 

問候。

+0

您確認您已加入該行的'info.plist'爲合適的項目目標,而不是'test'目標?我犯了這個錯誤,花了大約2.5天的時間才弄明白!我認爲一切都是正確的,它不工作! – Neeku

+0

@Neeku,謝謝你的回覆!是的,我將該行添加到項目目標,而不是測試目標。 –

回答

1

我應該使用下面的代碼

- (void)playAudio:(NSString *)path { 
NSError *error; 
NSURL *audioURL = [NSURL fileURLWithPath:path]; 

self.notificationPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:audioURL error:&error]; 
self.notificationPlayer.delegate = self; 

AVAudioSession* audioSession = [AVAudioSession sharedInstance]; 
NSError *errorSession = nil; 
[audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDuckOthers error:nil]; 
[audioSession setActive:NO error:&errorSession]; 

[self.notificationPlayer prepareToPlay]; 
[self.notificationPlayer setVolume:1.0]; 
[self.notificationPlayer play]; 
if (error) { 
    NSLog(@"error %@",[error localizedDescription]); 
} 
NSLog(@"Should play music"); 
} 

- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ 
[player stop]; 
[[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; 
} 
2

另一個真正重要的事情需要注意,而在後臺播放聲音是你是否沒有權限的文件。如果你正在播放一個包文件,你有權限,並且大概有一些文件是從互聯網傳輸過來的,但是如果你已經將媒體下載到文檔目錄中,那麼你必須適當地設置權限,特別是如果你的應用程序本身鎖定了文件。

這很容易與許多人看到的症狀相匹配,即文件將繼續播放幾秒鐘,5-15秒然後停止。如果您正在監聽完成的方法,則會看到一個錯誤標誌。原因:AVAudioPlayer首次播放時不會將整個音頻文件加載到內存中。
例子:

NSURL url = [NSURL URLWithString:@""]; 

    NSError *error = nil; 
    [[NSFileManager defaultManager] setAttributes:@{NSFileProtectionKey : 
    NSFileProtectionCompleteUntilFirstUserAuthentication} ofItemAtPath:[url path] error:&error]; 

您的選項如下:

NSFileProtectionComplete; 
    NSFileProtectionCompleteUnlessOpen; 
    NSFileProtectionCompleteUntilFirstUserAuthentication; 
    NSFileProtectionNone; 
+0

感謝您提供這些詳細信息 –

相關問題