多个音频播放器

时间:2011-09-25 16:19:44

标签: iphone objective-c xcode

有人可以告诉我如何用页面翻转效果(声音)编写一个小应用程序。 我希望有人点击“下一步”按钮,声音播放1秒钟,然后转到第二页,从第二页开始,如果单击“返回”按钮,则会播放另一声音1秒,然后返回原始页面。 不知怎的,我遇到了问题,当我点击后退按钮时,模拟器崩溃并出现“autorelease”错误消息。请在下面找到.h和.m文件。感谢您花时间阅读。

这是第一页的.m文件

#import "_0_RabbanasViewController.h"
#import "Rabbana2.h"
#import "InfoPage.h"

@implementation _0_RabbanasViewController



- (IBAction)info {

    // This part takes us to the info page
    InfoPage *info = [[InfoPage alloc] initWithNibName:@"InfoPage" bundle:nil];
    [UIView beginAnimations:@"flipView" context:Nil];
    [UIView setAnimationDuration:2];
    [UIView setAnimationCurve:UIViewAnimationOptionCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    [self.view addSubview:info.view];
    [info release];
    [UIView commitAnimations];


    // This part plays the next page turn noise
    NSURL *this = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/next.wav", [[NSBundle mainBundle] resourcePath]]];
    NSError *error;
    pagePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:this error:&error];
    [pagePlayer setDelegate:self];
    pagePlayer.numberOfLoops = 0;
    [pagePlayer play];
    //[pagePlayer release];

    //This will stop the music when turning page
    [audioPlayer stop];
    [audioPlayer release];
    clicked = 0;
    [start setImage:[UIImage imageNamed:@"Pplay.png"] forState:UIControlStateNormal];

}

- (IBAction)next {

    // This part takes us to the next view
    Rabbana2 *next = [[Rabbana2 alloc] initWithNibName:@"Rabbana2" bundle:nil];
    [UIView beginAnimations:@"flipView" context:Nil];
    [UIView setAnimationDuration:2];
    [UIView setAnimationCurve:UIViewAnimationOptionCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
    [self.view addSubview:next.view];
    //[next release];
    [UIView commitAnimations];



    // This part plays the next page turn noise
    NSURL *this = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/next.wav", [[NSBundle mainBundle] resourcePath]]];
    NSError *error;
    pagePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:this error:&error];
    [pagePlayer setDelegate:self];
    pagePlayer.numberOfLoops = 0;
    [pagePlayer play];
     //[pagePlayer release];

    //This will stop the music when turning page
    [audioPlayer release];
    clicked = 0;
    [start setImage:[UIImage imageNamed:@"Pplay.png"] forState:UIControlStateNormal]; 

}

// This button plays the audio
- (IBAction)play {

    if(clicked == 0){
        clicked = 1;
        NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/rabbana1.wav", [[NSBundle mainBundle] resourcePath]]];

        NSError *error;
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        [audioPlayer setDelegate:self];
        audioPlayer.numberOfLoops = 0;


        [audioPlayer play];
        [start setImage:[UIImage imageNamed:@"Sstop.png"] forState:UIControlStateNormal];

    } 
    else{
        [audioPlayer stop];
        [audioPlayer release];
        clicked = 0;
        [start setImage:[UIImage imageNamed:@"Pplay.png"] forState:UIControlStateNormal];
    } 

}

//If user does not do anything by the end of the sound set the button to start
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player
                        successfully: (BOOL) flag {
    if (flag==YES) {
        clicked = 0;
        [start setImage:[UIImage imageNamed:@"Pplay.png"] forState:UIControlStateNormal];
    }
}
/*
// If user click on the next button while audio playing, Audio should stop
- (void) audioPlayerDidNotFinishPlaying: (AVAudioPlayer *) player
                                success: (BOOL) flag {
    if (flag==YES) {
        [audioPlayer stop];
        [audioPlayer release];
        clicked = 0;
        [start setImage:[UIImage imageNamed:@"Pplay.png"] forState:UIControlStateNormal];
    }
}
*/

- (void)dealloc
{

    [super dealloc];
    //[self.pagePlayer.delegate = nil];
    //self.AVAudioPlayer = nil ;
}

- (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];
    [scroll setScrollEnabled:YES];
    [scroll setContentSize:CGSizeMake(275,445)];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    //[audioPlayer release];
    //[pagePlayer release];
    // 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

这是第二页的.m文件

#import "InfoPage.h"
#import "_0_RabbanasViewController.h"


@implementation InfoPage

- (IBAction)back {
    [UIView beginAnimations:@"flipView" context:Nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationOptionCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view.superview cache:YES];

    [self.view removeFromSuperview];
    [UIView commitAnimations];

    // This part plays the back page turn noise
    NSURL *this = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/back.wav", [[NSBundle mainBundle] resourcePath]]];
    NSError *error;
    pagePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:this error:&error];
    [pagePlayer setDelegate:self];
    pagePlayer.numberOfLoops = 0;
    [pagePlayer play];

    //This will stop the music when turning page
    [pagePlayer release];
    clicked = 0;


}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
   // [InfoPage dealloc];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
   // [pagePlayer release];
    self.view = nil;
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [scroll setScrollEnabled:YES];
    [scroll setContentSize:CGSizeMake(320,480)];
    // Do any additional setup after loading the view from its nib.
}

- (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

0 个答案:

没有答案