声音没有播放AVAudioPlayer

Aen*_*Tan 8 iphone audio core-audio avaudioplayer ios

我搜索过,我相信我的问题非常独特.我知道使用AVAudioPlayer时的模拟器5.1错误,这不是我的问题.我在iOS 5.1设备上运行.

这是我的头文件:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>

-(IBAction)pushBell;

@end
Run Code Online (Sandbox Code Playgroud)

和我的实施文件:

#import "BellViewController.h"

@interface BellViewController ()

@end

@implementation BellViewController

-(IBAction)pushBell {

    NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"bell1" ofType:@"caf"];
    NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
    NSError *error;

    AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
    [theAudio setDelegate:self];
    [theAudio setNumberOfLoops:0];
    [theAudio play];

}

@end
Run Code Online (Sandbox Code Playgroud)

我的.xib中有一个按钮来播放该文件.

为了确保我正确引用了我的音频文件,我故意输入了错误的文件名,实际上我得到了一个例外.为了确保文件可播放,我将其邮寄给自己并在设备上播放.

当我点击按钮时,我什么都没听到.没有错误.我不知道出了什么问题.

更多信息 iPhone 4与iOS 5.1 Xcode 4.3.2音频大约700kbps转换为.caf格式使用afconvert从一个大的.wav文件.

3lv*_*vis 20

解决了.

1.-检查您的文件是否已添加到Build Phases中的"Copy Bundle Resources". 复制捆绑资源

2.- ViewController.m

#import <AVFoundation/AVAudioPlayer.h>

@interface ViewController ()
@property (nonatomic, strong) AVAudioPlayer *theAudio;
- (IBAction)pushBell;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"beep-beep" ofType:@"caf"];
    NSURL *soundURL = [NSURL fileURLWithPath:soundPath];

    NSError *error = nil;
    self.theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
}

- (IBAction)pushBell {
    [self.theAudio play];
}
Run Code Online (Sandbox Code Playgroud)

3.-将IBAction连接到Interface Builder中的按钮.

完成.


jow*_*wie 19

这是我的问题,所以我只是在这里张贴它以防万一!

信不信由你,似乎有一个错误(至少在iOS 2上的iOS 5.1中),如果你将侧开关设置为静音并且它是静音的,那么将侧开关设置为锁定旋转,AVAudioPlayer听起来不会通过耳机播放!

所以我不得不去设置应用程序,将开关改回静音,然后取消iPad静音,将其切换回锁定旋转,我的应用程序开始正常工作.


Rok*_*arc 14

theAudio AVAudioPlayer甚至可以在开始播放之前取消分配(因为它被分配为pushBell方法中的局部变量.

将您的代码更改为:

头文件:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>

@interface BellViewController:UIViewController

@property (nonatomic, strong) AVAudioPlayer *theAudio;

-(IBAction)pushBell;

@end
Run Code Online (Sandbox Code Playgroud)

实施文件:

#import "BellViewController.h"

@implementation BellViewController

@synthesize theAudio = _theAudio;

- (void)viewDidLoad {

    if (!theAudio) {

        NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"bell1" ofType:@"caf"];
        NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
        NSError *error;

        _theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
        [self.theAudio setDelegate:self];
        [self.theAudio setNumberOfLoops:0];

        [self.theAudio prepareToPlay];
    }
}

-(IBAction)pushBell {

   [self.theAudio play];
}

@end
Run Code Online (Sandbox Code Playgroud)

我假设这BellController是一个UIViewController,如果它是一个不同类的类,只需相应地修改代码.

  • "音频不播放"似乎在我的编码生活中出现过很多次,每次都只是一个不同的原因!这个是我最新的...在ARC打开的情况下,`alloc init`不足以确保对象保留在内存中!谢谢 :) (2认同)