【问题标题】:Sounds on multiple buttons and stop when others play in iOS多个按钮上的声音并在其他人在 iOS 中播放时停止
【发布时间】:2013-03-29 05:09:21
【问题描述】:

我正在尝试编写一个小应用程序来学习一些 iOS 开发的基础知识,如果这是基础知识,请原谅我。我查看了很多线程,但我认为我有几个特定于这个应用程序的问题。

  1. 我见过几种在按下按钮时播放音频的不同方式。一个涉及“CFBundleRef”(How to play iPhone tap sound?)和另一个“AVAudioPlayer”(Play Audio iOS Objective-C)。我如何知道使用哪个以及何时适当使用每个?

  2. 我目前使用第一个选项使音频正常工作,但我似乎无法弄清楚当按下另一个按钮时如何让声音停止。我该怎么做?

  3. 除了我所做的之外,还有更简单的方法可以将 15 个声音/按钮链接在一起吗?

这是 .m 文件:

#import "animalsViewController.h"

@interface animalsViewController ()

@end


@implementation animalsViewController


-(IBAction) boom; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"bird", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom2; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"bird2", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}
-(IBAction) boom3; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"bird3", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom4; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"chicken1", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom5; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"chicken2", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom6; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"cow", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom7; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"dog1", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom8; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"duck", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom9; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"horse", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom10; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"lion", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom11; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"monkey", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom12; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"owl", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom13; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"pig", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom14; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"rooster", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}

-(IBAction) boom15; {

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"sheep", CFSTR ("mp3"), NULL);
    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID);

}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}






@end

这是 .h 文件:

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>

@interface animalsViewController : UIViewController{


}

-(IBAction) boom;
-(IBAction) boom2;
-(IBAction) boom3;
-(IBAction) boom4;
-(IBAction) boom5;
-(IBAction) boom6;
-(IBAction) boom7;
-(IBAction) boom8;
-(IBAction) boom9;
-(IBAction) boom10;
-(IBAction) boom11;
-(IBAction) boom12;
-(IBAction) boom13;
-(IBAction) boom14;
-(IBAction) boom15;

@end

【问题讨论】:

  • 这是一个很好的问题,但你在帖子 1 中的做法也很好。特别是在初始代码之后简单地剪切和粘贴!然后简单地将按钮拖放到 IB 点!

标签: iphone ios objective-c


【解决方案1】:

使用单个 AVAudioPlayer 实例,以及类似以下内容..

-(void)playAudioOfType:(int)type{

     [self stopAudio];


     NSString *sound=@"";

     switch (type) {
       case 1:
          sound=@"bird";
        break;

       case 2:
          sound=@"bird2";
        break;

       case 3:
          sound=@"bird3";
        break;

       case 4:
          sound=@"chicken";
        break;

       default:
         break;
      }


      NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                     pathForResource:sound
                                     ofType:@"mp3"]];



NSError *error;
if(url){
    audioPlayer = [[AVAudioPlayer alloc]
                   initWithContentsOfURL:url
                   error:&error];



    if (error)
    {
        NSLog(@"Error in audioPlayer: %@", 
              [error localizedDescription]);
    } else {
        audioPlayer.delegate = self;
        [audioPlayer prepareToPlay];
    }

    [audioPlayer play];
}


 }

 -(void)stopAudio{

if(audioPlayer && [audioPlayer isPlaying]){
    [audioPlayer stop];
    audioPlayer=nil;
}


}

然后你的每一个动作你都可以像下面这样调用函数

-(IBAction) boom; {
     [self playAudioOfType:1]; //Will play bird.mp3
}

-(IBAction) boom1; {
     [self playAudioOfType:2]; //Will play bird2.mp3
}

-(IBAction) boom2; {
     [self playAudioOfType:3]; //Will play bird3.mp3
}

找到一个测试应用here ..您只需在应用中添加 mp3 文件,它应该可以工作..

【讨论】:

  • 那么,我需要清除所有 -(IBAction) boom1;行并用第一段代码替换它?抱歉.. 对 objc 和 ios 非常陌生:|
  • 不,您不必删除动作,您可以在不同类型的每个动作中使用相同的函数,并在函数内部定义声音文件。但更好的方法是创建单个动作为按钮分配了不同的标签。如果你不明白,请告诉我。。
  • 所以你是说去掉以“CFBundleRef ...”开头的位,然后把你包含的第二段代码,然后把第一段代码放在前面?如果不是..那么..我不明白:D
  • 我很抱歉.. 我还是不明白。我应该删除我的 .h 和 .m 文件并包含你写的内容吗? :/ 我知道它一定很简单,我只是很新。
  • 添加了一个测试应用,看我的评论。
【解决方案2】:

试试这个链接:Simple sound question

请参阅此问题的最后一个...

    -(void)stop
    {
            AudioServicesDisposeSystemSoundID (_soundID);
            self.IsPlaying = FALSE;
    }

【讨论】:

  • 这会去哪里?对不起..这里很新o.O
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多