0

我在使用 AVAudioPlayer 时遇到问题,无法从其他视图获取声音。它与 AudioToolbox 完美配合,但不适用于 AVAudioPlayer。这是我的代码:

.h 文件:

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreText/CoreText.h>
#import "SimpleTableCelldetail.h"

int clicked;


@interface DetailViewController : UIViewController {

AVAudioPlayer *audioPlayer;
IBOutlet UIButton *start;

}

@property (strong, nonatomic) IBOutlet NSString *audioSon;

@end

.m 文件

-(IBAction)play:(id)sender {

if(clicked == 0) {
    clicked = 1;
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:audioSon, [[NSBundle mainBundle] resourcePath]]];

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = 0;

    [audioPlayer play];
    [start setTitle:@"Stop" forState:UIControlStateNormal];
}
else {
    clicked = 0;
    [audioPlayer stop];
    [start setTitle:@"Ecouter" forState:UIControlStateNormal];
}
}

当我运行此代码时,按钮不播放任何声音。audioSon 的值为“sound01.m4r”。

当我更换线路时

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:audioSon, [[NSBundle mainBundle] resourcePath]]];

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sound01.m4r", [[NSBundle mainBundle] resourcePath]]];

它有效,但 xcode 向我显示错误:

2012-08-01 01:59:44.697 CustomTable[12709:3703] Error loading /System/Library/Extensions/AppleHDA.kext/Contents/PlugIns/AppleHDAHALPlugIn.bundle/Contents/MacOS/AppleHDAHALPlugIn:  dlopen(/System/Library/Extensions/AppleHDA.kext/Contents/PlugIns/AppleHDAHALPlugIn.bundle/Contents/MacOS/AppleHDAHALPlugIn, 262): Symbol not found: ___CFObjCIsCollectable
  Referenced from: /System/Library/Frameworks/Security.framework/Versions/A/Security
  Expected in: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
 in /System/Library/Frameworks/Security.framework/Versions/A/Security

任何的想法?

谢谢。

4

1 回答 1

1

当您stringWithFormat应该使用stringByAppendingPathComponent. 如果您更改此设置,您将访问正确的文件并且您的程序应该可以工作。

NSURL *url = [NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:audioSon];
于 2012-08-01T00:35:43.943 回答