gpt4 book ai didi

objective-c - 将 NSString 与 AVAudioPlayer 一起使用

转载 作者:行者123 更新时间:2023-11-29 04:30:50 26 4
gpt4 key购买 nike

我在使用 AVAudioPlayer 时遇到问题,无法从其他 View 获取声音。它可以与 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

有什么想法吗?

谢谢。

最佳答案

当您应该使用 stringByAppendingPathComponent 时,您在原始行中使用了 stringWithFormat。如果您更改此设置,您将访问正确的文件并且您的程序应该可以运行。

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

关于objective-c - 将 NSString 与 AVAudioPlayer 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11750780/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com