0

iOS開発の基本を学ぶために小さなアプリをまとめようとしていますので、これが基本的なものである場合はご容赦ください。かなりの数のスレッドを見てきましたが、このアプリに固有の質問がいくつかあると思います。

  1. ボタンが押されたときにオーディオを再生するいくつかの異なる方法を見てきました。1 つは "CFBundleRef" ( iPhone のタップ音を再生する方法) と、もう 1 つは "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
4

2 に答える 2

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
}

ここでテストアプリを見つけてください..mp3ファイルをアプリに追加するだけで、動作するはずです..

于 2013-03-29T05:18:35.797 に答える
0

このリンクを試してください:簡単な音声の質問

この質問の最後を参照してください...

    -(void)stop
    {
            AudioServicesDisposeSystemSoundID (_soundID);
            self.IsPlaying = FALSE;
    }
于 2013-03-29T05:21:37.203 に答える