2010-06-15 5 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

어떤 부분이 작동하지 않습니까? –

+0

장치에서 사운드를 재생할 수 없습니다. 그것은 시뮬레이터에서 작동합니다. – cannyboy

답변

1

여기에 시도하는 빠르고 간단한 일입니다. iPhone은 파일 확장자에 대해서도 대소 문자를 구분하지만 시뮬레이터는 중요하지 않습니다. soundObject 외에도 NSURL 개체! = nil을 확인하십시오.

- (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

그게 문제는 아닌 것 같습니다. – cannyboy

2

나는 작동하는 일부 코드 아래 .APP에 복사되고 있는지 확인 할 수 있습니다 시뮬레이터에서 작동하며 장치가 아닌 경우 작동하려면 먼저 장치를 다시 시작하고 확인하십시오. 때로는 장치를 다시 시작하면 저에게 효과적이며 많은 시간을 절약 할 수 있습니다. 그렇지 않으면 디버깅을 시작할 수 있습니다.

0

뭔가 :

또한 [[NSBundle mainBundle] URLForResource:@"sound" ofType:@"mp3"];을 시도하고 파일이 YourApp.app/Resources/

-Stephen

10

iPhone의 "Ringer Button"이 빨간색으로 표시되지 않는지 확인하십시오.

+0

어리석은 ... 그리고 어떻게 진실한! 그게 내 문제 였어 :) –

+0

같은 그리고 지금 나는 완전한 바보 같아! : D – Shiri

+0

OH, 전화를 끄는 사이드 스위치. 블레! lol –

0

시뮬레이터에는 소리가 들렸지만 기기에는 소리가 들리지 않았습니다. AVAudioPlayer 대리인을 설정할 때 작동하기 시작했습니다.

0

iOS 10 시뮬레이터에서 코드를 실행하는 경우 보안 권한을 요청하지 않지만 ios 10 장치에서 코드를 실행하는 경우 plist에 오디오 권한 키를 추가해야합니다. 개인 정보 추가 - 마이크 사용 설명 plist의 키.

관련 문제