Iphone 按下viewcontroller后播放mp3时出现问题
在我的tableViewController中Iphone 按下viewcontroller后播放mp3时出现问题,iphone,objective-c,audio,avplayer,Iphone,Objective C,Audio,Avplayer,在我的tableViewController中 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 我按下我的viewController [self.navigationController pushViewController:[[[UIViewController alloc] initWithNibName:@"myViewContro
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
我按下我的viewController
[self.navigationController pushViewController:[[[UIViewController alloc] initWithNibName:@"myViewController" bundle:nil] autorelease] animated:YES];
在myViewController中,我导入了AVFoundation,并通过IB添加了播放/暂停/停止歌曲的三个按钮
myViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface fotos : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *player;
我已经用这种方式实现了
myViewController.m
@implementation fotos
@synthesize player;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"peter"
ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
player.delegate = self;
[player prepareToPlay];
[filePath release];
[fileURL release];
}
-(IBAction)play:(id)sender {
if(!player.playing)
{
[player play];
}
}
-(IBAction)pause:(id)sender {
[player pause];
}
-(IBAction)stop:(id)sender {
[player stop];
}
我在IB中连接了我的操作,但当我尝试播放我的文件时
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-
[UIViewController play:]: unrecognized selector sent to instance 0x5619e70'
*** Call stack at first throw:
(
0 CoreFoundation 0x01009be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0115e5c2 objc_exception_throw + 47
2 CoreFoundation 0x0100b6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00f7b366 ___forwarding___ + 966
4 CoreFoundation 0x00f7af22 _CF_forwarding_prep_0 + 50
5 UIKit 0x0041ea6e -[UIApplication sendAction:to:from:forEvent:] + 119
6 UIKit 0x004ad1b5 -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x004af647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
8 UIKit 0x004ae1f4 -[UIControl touchesEnded:withEvent:] + 458
9 UIKit 0x004430d1 -[UIWindow _sendTouchesForEvent:] + 567
10 UIKit 0x0042437a -[UIApplication sendEvent:] + 447
11 UIKit 0x00429732 _UIApplicationHandleEvent + 7576
12 GraphicsServices 0x01b18a36 PurpleEventCallback + 1550
13 CoreFoundation 0x00feb064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
14 CoreFoundation 0x00f4b6f7 __CFRunLoopDoSource1 + 215
15 CoreFoundation 0x00f48983 __CFRunLoopRun + 979
16 CoreFoundation 0x00f48240 CFRunLoopRunSpecific + 208
17 CoreFoundation 0x00f48161 CFRunLoopRunInMode + 97
18 GraphicsServices 0x01b17268 GSEventRunModal + 217
19 GraphicsServices 0x01b1732d GSEventRun + 115
20 UIKit 0x0042d42e UIApplicationMain + 1160
21 Training Cocoa 0x00001bc4 main + 102
22 Training Cocoa 0x00001b55 start + 53
)
terminate called after throwing an instance of 'NSException'
我也尝试过在viewDidLoad中玩,但什么都没有
有什么建议吗?
Thank's因为播放器是一个属性,请尝试通过引用它
[self player]
所以你可能有
[[self player]play]代码>谢谢,回答得很好。我注意到了我的新错误,现在我创建了一个引用myViewController的viewController,所有工作都是myViewController*myC=[[myViewController alloc]initWithNib…]并按下myC。
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-
[UIViewController play:]: unrecognized selector sent to instance 0x5619e70'
*** Call stack at first throw:
(
0 CoreFoundation 0x01009be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0115e5c2 objc_exception_throw + 47
2 CoreFoundation 0x0100b6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00f7b366 ___forwarding___ + 966
4 CoreFoundation 0x00f7af22 _CF_forwarding_prep_0 + 50
5 UIKit 0x0041ea6e -[UIApplication sendAction:to:from:forEvent:] + 119
6 UIKit 0x004ad1b5 -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x004af647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
8 UIKit 0x004ae1f4 -[UIControl touchesEnded:withEvent:] + 458
9 UIKit 0x004430d1 -[UIWindow _sendTouchesForEvent:] + 567
10 UIKit 0x0042437a -[UIApplication sendEvent:] + 447
11 UIKit 0x00429732 _UIApplicationHandleEvent + 7576
12 GraphicsServices 0x01b18a36 PurpleEventCallback + 1550
13 CoreFoundation 0x00feb064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
14 CoreFoundation 0x00f4b6f7 __CFRunLoopDoSource1 + 215
15 CoreFoundation 0x00f48983 __CFRunLoopRun + 979
16 CoreFoundation 0x00f48240 CFRunLoopRunSpecific + 208
17 CoreFoundation 0x00f48161 CFRunLoopRunInMode + 97
18 GraphicsServices 0x01b17268 GSEventRunModal + 217
19 GraphicsServices 0x01b1732d GSEventRun + 115
20 UIKit 0x0042d42e UIApplicationMain + 1160
21 Training Cocoa 0x00001bc4 main + 102
22 Training Cocoa 0x00001b55 start + 53
)
terminate called after throwing an instance of 'NSException'