2012-08-01 17 views
0

AVAudioPlayer從另一個視圖觸發聲音時遇到問題。它與AudioToolbox完美配合,但與AVAudioPlayer不兼容。這裏是我的代碼:在AVAudioPlayer中使用NSString

.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 

任何想法?

謝謝。

回答

1

當您應該使用stringByAppendingPathComponent時,您在原始行中使用stringWithFormat。如果你改變這個,你將訪問正確的文件,你的程序應該工作。

NSURL *url = [NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:audioSon]; 
+0

很棒!謝謝達斯汀。 Xcode的錯誤仍然在這裏..但它可以等待! – hhd59 2012-08-01 01:09:18