나는이 질문에 요청한 것과 비슷한 일을하려고에 재생을 중지하지 않습니다. 초기 뷰 페이지에는 다른 뷰를 호출하는 버튼이 있으며 사운드가 재생되지 않습니다. 이러한 다른 뷰에는 사용자가 돌아갈 수 있도록 BarButtonItem이있는 UINavigationController가 포함되어 있습니다. 다른보기 페이지에는 소리를 재생할 수있는 단추가 있습니다. 각 사운드에는 자체 재생 버튼이 있으며 이력서, 일시 중지 및 정지 버튼이 있습니다.스토리 보드를 사용하고 뷰를 변경, 소리 새보기
모든 사운드는 정상적으로 재생되지만, 메인 페이지의 뒤로 버튼을 누르면 사운드가 계속 재생됩니다. 원하는 동작은 뒤로 버튼을 통해 메인 페이지로 다시 이동할 때 사운드가 멈추는 것입니다.
스토리 보드가 나오기 전에 오디오를 멈추기 위해 뒤로 버튼을 쉽게 코딩 할 수 있지만 스토리 보드는 그렇게 단순하지는 않습니다. prepareForSegue 메서드를 구현해야하는 것으로 보입니다. 좋아, 속성 검사기에서 Segue 식별자를 설정할 수 있으며 이전 게시물을 다시 참조하면 showPlayer를 사용하도록 설정할 수 있습니다.
하지만이 멋진 예에서 볼 수 있듯이 나는, 내 사운드보기 중 하나에 prepareForSegue을 코딩 할 수있다 생각했다 : 메인 페이지 ViewController.m에서 http://www.scott-sherwood.com/?p=219
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"showPlayer"]){
SoundPageController *theAudio = (SoundPageController *)[segue ViewController];
theAudio.delegate = self;
}
}
, 나는 추가 시도 :
@property (strong, nonatomic) AVAudioPlayer *theAudio;
그리고 내가 좋아하는 뭔가를 추가하는 시도 :
- (void)viewDidLoad {
[super viewDidLoad];
// this is the important part: if we already have something playing, stop it!
if (audioPlayer != nil)
{
[audioPlayer stop];
}
// everything else should happen as normal.
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
이 작업을 수행 할 수 없어서 변경 사항을 원래대로 되 돌렸습니다. 나는 아래에 간단한 코드를 넣을 것이고, 당신이 그것을 올바르게하는 방법에 대한 제안이 있다면 알려 주시기 바랍니다!
ViewController.h :
@interface ViewController : UIViewController {
}
ViewController.m (거의 기본값) :
#import "ViewController.h"
#import "AppDelegate.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
SoundPage1.h : # import를
#import <AVFoundation/AVAudioPlayer.h>
@interface occupy : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer* theAudio;
}
//-(IBAction)PressBack:(id)sender; now the back button is linked up through storyboard
-(IBAction)pushButton1;
-(IBAction)pushButton2;
-(IBAction)play;
-(IBAction)stop;
-(IBAction)pause;
@end
SoundPage1.m
#import "SoundPage1.h"
#import "AppDelegate.h"
#import "ViewController.h"
@implementation SoundPage1
-(IBAction)pushButton {
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"mp3"];
if(theAudio)[theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
[theAudio play];
}
-(IBAction)pushButton2 {
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"mp3"];
if(theAudio)[theAudio release];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
theAudio.volume = 1.0;
[theAudio play];
}
// Old code from XCode 3.x when creating a back button that could stop the audio was easy
//-(IBAction)PressBack:(id)sender{
// [theAudio stop];
// ViewController* myappname = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
// [self.navigationController pushViewController:myappname animated:NO];
// }
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
재생, 중지 및 일시 중지 단추에 코드를 추가하십시오.
이 형식을 시도했는데, 읽는다면 미리 미안합니다.
의견을 보내 주셔서 감사합니다. Rob
와우 ... 나는 이것이 얼마나 쉬운 지 알았습니다. 뷰에서 사운드가 재생되는 것을 막을 수있는 솔루션은 viewWillDisappear입니다. - (무효) viewWillDisappear : (BOOL) 애니메이션 { [self.navigationController setNavigationBarHidden : NO animated : animated]; [super viewWillDisappear : animated]; [TheAudio stop]; } 멋진 작품입니다! 준비된 사람들에게 감사드립니다! Rob – user1362308