iphone - AVAudioPlayer 在 iPhone 5 上不工作

标签 iphone ios ios6 avaudioplayer iphone-5

有没有人在使用 iPhone 5 时遇到过 AVAudioPlayer 的问题?我的代码在 iPhone 4、iPhone 4S 和 iPad(第 3 代)上运行正常,但现在随机无法在 iPhone 5 上运行。我看到了这个问题,但没有人解决实际问题:

AVAudioPlayer is not working in iPhone 5

NSURL *soundURL = [self urlForAlarmSong:songKey];    
NSError *err;
self.audioAlert = [[[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&err] autorelease];

[self.audioAlert prepareToPlay];
currentVolume=[MPMusicPlayerController applicationMusicPlayer].volume;
[[MPMusicPlayerController applicationMusicPlayer] setVolume:1.0];

[self.audioAlert play];

audioAlert 是我在 header 中使用 (retain,nonatomic) 声明的 AVAudioPlayer。 err 变量每次都为 null,即使它不在 iPhone 5 上播放也是如此。此代码每次在 iPhone 4、iPhone 4S 和 iPad(第 3 代)上都能完美运行。

有没有人有什么想法??谢谢

最佳答案

 -(void)viewDidLoad

  {

  NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"click2"         ofType:@"mp3"]];

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    if(error)
    {
        NSLog(@"error in audio player:%@",[error localizedDescription]);
    }
    else

    {

     audioPlayer.delegate=self;

     [audioPlayer prepareToPlay];
    }

   }

按钮操作,

-(IBAction)SoundButton:(id)sender

     {

       [audioPlayer play];

     }

在你的 .h 文件中,不要忘记导入

   #import  AVFoundation/AVFoundation.h

并添加一个委托(delegate)AVAudioPlayerDelegate

关于iphone - AVAudioPlayer 在 iPhone 5 上不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13710304/

相关文章:

iphone - UIView 动画在新的 View Controller 被压入堆栈后继续运行

iphone - 我在哪里可以找到一些漂亮的 iPhone 按钮?

iphone - 带有推送通知的 iPhone 应用程序中的 Facebook 聊天

ios - 循环期间无法激活任何其他元素(iPhone应用程序)

ios - 以编程方式提取设置包 iOS 中的默认电子邮件帐户

iphone - UIPageViewController : get the currentPage

javascript - 无法缩放 UIWebView

ios - 只向数组插入 10 个元素

ios - 使用 IB/Xcode 4.5.1 和 iOS Simulator 6.0 自定义 UIView 操作方法

ios - 应用程序本地化后如何在 NSString 中查找单词?