【问题标题】:playing sound with reference of images参考图像播放声音
【发布时间】:2011-06-15 06:36:24
【问题描述】:

如问题中所述,我在资源文件夹中添加了一些图像和声音文件。图像导航可以使用下一个和上一个按钮正常工作。现在我需要播放与图像相关的声音。工作代码如下所示。我需要知道我需要做什么才能让声音与图像一起播放。

我添加了 audioToolbox.framework 和 AVFoundation.framework。我在 Apple 开发者网站上阅读了有关 NSSound 的信息。

#import "next_previousViewController.h"

@implementation next_previousViewController

@synthesize image1, next1, next2;

NSMutableArray *list;
NSMutableArray *sound;

int state1 = 0;



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
    list = [[NSMutableArray alloc] init];
    [list addObject:@"A.png"];
    [list addObject:@"B.png"];
    [list addObject:@"C.png"];
    [list addObject:@"D.png"];

    //[image1 setImage:[UIImage imageNamed:@"A.png"]];

    sound = [[NSMutableArray alloc] init];
    [sound addObject:@"A.mp3"];
    [sound addObject:@"B.mp3"];
    [sound addObject:@"C.mp3"];
    [sound addObject:@"D.mp3"];
    [sound addObject:@"E.mp3"];

    [super viewDidLoad];
}

-(IBAction) doNext
{
    int i, count;
    //state = 1;
    count = [list count];
    for(i = state1; i < count; i++)
    {
        [image1 setImage:[UIImage imageNamed:[list objectAtIndex:state1]]];

        //--------------------For Sound-----------------------------------

        //-----------------------------END--------------------------------


        //exit(0);

        //UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"hello" message:[NSString stringWithFormat: @"%d", state1] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
        //[alert show];
        state1 = state1 + 1;
        break;

    }
}

-(IBAction) doPrev
{
    int i, count;
    count = [list count];
    for(i = state1; i < count; i++)
    {
        state1 = state1 - 2;
        [image1 setImage:[UIImage imageNamed:[list objectAtIndex:state1]]];
        //UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"hello" message:[NSString stringWithFormat: @"%d", state1] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
        //[alert show];

        state1 = state1 + 1;
        break;

    }
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}


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

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


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

@end

抱歉问题太长了。

【问题讨论】:

    标签: iphone cocoa-touch avfoundation


    【解决方案1】:

    这里有一个很好的播放声音文件的教程:

    http://www.71squared.com/2009/05/iphone-game-programming-tutorial-9-sound-manager/

    解释清楚,代码开源!也很容易添加到任何项目中!

    【讨论】:

      猜你喜欢
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 2020-11-22
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多