Cod*_*ddy 3 objective-c avaudioplayer
我有一个非常简单的应用程序,我已经构建来测试AVAudioPlayer.我有一个16s的aif文件在我的Mac上播放得很好.把它放在一个简单的应用程序中,然后在我的iPhone上运行但没有声音.向对象发送'play'会返回成功,audioPlayerDidFinishPlaying会在16秒后成功返回.仍然没有声音.检查了iPhone上的音量,就好了.已检查以确保我的AVAudioPlayer对象的卷设置为1.0 - 确实如此.有人可以帮忙吗?
这是我的代码
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Soothing" ofType:@"aif"];
NSURL *playURL = [NSURL fileURLWithPath:soundPath];
NSError *error;
self.myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:playURL error:&error];
NSLog(@"About to play sound");
self.myAudioPlayer.delegate = self;
[self.myAudioPlayer prepareToPlay];
bool success = [self.myAudioPlayer play];
if (!success) {
NSLog(@"Failed to play file");
} else NSLog(@"Looks like it succeded");
Run Code Online (Sandbox Code Playgroud)
好的 - 搞清楚了.我没有为我的应用程序配置AVAudioSession.做了一件非常简单的事情(从Apple的示例代码中复制):
[[AVAudioSession sharedInstance] setDelegate: self];
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
if (setCategoryError)
NSLog(@"Error setting category! %@", setCategoryError);
Run Code Online (Sandbox Code Playgroud)
声音现在播放 - 加上学习了很多关于如何通过屏幕锁等控制播放,这将是有价值的.
| 归档时间: |
|
| 查看次数: |
2034 次 |
| 最近记录: |