2010-06-15 143 views
14

我的MP3播放代碼:AVAudioPlayer在模擬器的工作,但不是在設備

NSError *error; 
soundObject = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:audioPathString] error:&error]; 
if (soundObject == nil) NSLog(@"%@", [error description]); 
soundObject.delegate = self; 
soundObject.numberOfLoops = 0; 
soundObject.volume = 1.0; 
NSLog(@"about to play"); 
[soundObject prepareToPlay]; 
[soundObject play]; 
NSLog(@"[soundObject play];"); 

用來玩細的MP3,它仍然沒有在模擬器上。但不在設備上。

我最近在軟件中添加了一些錄音代碼(不是我的)。它使用的AudioQueue東西略微超出了我的視野。這與AVAudioPlayer有衝突嗎?或者可能是什麼問題?我注意到,一旦audiorecording代碼開始工作,我不能再調整設備上的音量,所以它可能會阻止音頻播放?


編輯

的解決方案似乎是把這個在我的代碼。我把它的applicationDidFinishLaunching:

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil]; 
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; 
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride); 

第一行同時允許播放和錄製,而其他線路改道顯然的事情,使音量大。

所有的音頻代碼對我來說都是巫術。

+0

哪部分不起作用? – 2010-06-15 17:42:00

+0

在設備上播放聲音不起作用。它在模擬器上工作。 – cannyboy 2010-06-15 17:53:23

回答

1

這裏有一個快速,簡單的事情嘗試:

我跑進與圖像以及聲音類似的問題。 iPhone是敏感的,即使是文件擴展名,但模擬器不是。除了soundObject,請檢查NSURL對象!= nil。

你也可以試試[[NSBundle mainBundle] URLForResource:@"sound" ofType:@"mp3"];,並確保該文件正在YourApp.app/Resources/

-Stephen

+0

這似乎不是問題所在。 – cannyboy 2010-06-15 17:56:26

2

一些代碼,我知道作品下複製到的.app:

- (void)playOnce:(NSString *)aSound { 

// Gets the file system path to the sound to play. 
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"]; 

// Converts the sound's file path to an NSURL object 
NSURL *soundURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; 
self.soundFileURL = soundURL; 
[soundURL release]; 

AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error:nil]; 
self.theAudio = newAudio; // automatically retain audio and dealloc old file if new file is loaded 

[newAudio release]; // release the audio safely 

[theAudio prepareToPlay]; 

// set it up and play 
[theAudio setNumberOfLoops:0]; 
[theAudio setVolume: volumeLevel]; 
[theAudio setDelegate: self]; 
[theAudio play]; 

} 
0

東西時在模擬器上工作而不在設備上,首先要做的是重新啓動設備並檢查。有時重新啓動設備已爲我工作並節省了大量時間。否則,你可以開始調試

10

確保你的iPhone的「Ringer Button」沒有顯示紅色。

+0

多麼愚蠢...然而多麼真實!這是我的問題:) – 2016-06-04 23:43:22

+0

同樣,現在我覺得自己像個白癡! :D – Shiri 2016-07-20 10:13:58

+0

OH,使手機靜音的側面開關。的Bleh!大聲笑 – 2017-03-17 22:50:39

0

我在模擬器中有聲音,但沒有在設備上。它開始工作,當我設置AVAudioPlayer代理

0

如果您在iOS 10模擬器上運行代碼,它不會要求安全權限,但是,如果您在ios 10設備上運行代碼,則需要在plist中添加音頻權限密鑰。添加隱私 - 麥克風使用說明鍵入plist。

相關問題