Xcode를 처음 사용하고 문제가 있습니다. 소리 단추를 여러 번 클릭하면 서로 겹치게됩니다. 사운드가 재생되는 동안 버튼을 클릭하면 현재 사운드가 멈추고 새 사운드가 시작되도록 코드를 어떻게 설정합니까?새 소리 버튼을 눌렀을 때 소리를 멈추는 방법
코드를 게시 할 수 있다면 많은 도움이 될 것입니다.
#import "Sound3ViewController.h"
@implementation Sound3ViewController
-(IBAction)playnow:(id)sender;{
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"winning"
ofType:@"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (IBAction)play2:(id)sender {
soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"onadrug"
ofType:@"mp3"]];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
sound.delegate = self;
[sound play];
}
- (void)dealloc
{
[super dealloc];
[sound release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
감사 잭,하지만 난 몇 가지 오류 메시지를받은 : 정확하지만,이 시도. 온라인 - (IBAction) playnow : (id) 보낸 사람 여기에 (함수가 아닌) "예상 표현", "playnow", "Expireded"또는 ";"라는 오류 메시지가 나타납니다. before : "토큰과 온라인 : allow = true; 난 오류가 발생했습니다 : "허용"undecalred (이 함수에서 처음 사용) – Dominick
물론, 부울 허용 = true' 세미콜론 (;)없이 쓴 이후 그냥 추가하십시오. 당신은 Obj-C 문법으로는 그렇게 실용적이지 않습니다. :)'allow '에 대해 신경 쓰지 않았습니다. C 파일이나 내부 객체 자체를 속성으로 저장하도록 선택해야하기 때문입니다. – Jack
잭, 너의 권리, 나는 목표 C를 배우려고 노력하고있다. :) – Dominick