在像钢琴这样的触摸事件上同时播放多种媒体
Play multiple media same time on touch event like Piano
我想在触摸事件时播放媒体。
在我的应用程序中有 4 个按钮。
当用户使用第一个手指点击第一个按钮时,想要开始在按钮 1 上播放音乐 1。
使用第二根手指立即点击按钮 2,然后想要开始在按钮 2 上播放音乐 2(用户还没有松开他的第一根手指)。
使用第三根手指立即单击按钮 3,然后想要开始在按钮 3 上播放音乐 3(用户还没有松开他的第一根手指和第二根手指)。
我该如何实施?
我对此一无所知。
请帮助我。
我只会用AVAudioPlayer播放音乐
提前致谢
这段代码对我有用。
ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioServices.h>
@interface ViewController : UIViewController
{
NSString *filePath;
}
@property(strong,nonatomic) AVAudioPlayer *playerAudio1;
@property(strong,nonatomic) AVAudioPlayer *playerAudio2;
@property(strong,nonatomic) AVAudioPlayer *playerAudio3;
@property(strong,nonatomic) AVAudioPlayer *playerAudio4;
-(IBAction)btnDownload:(id)sender;
-(IBAction)btn1_DownAction:(id)sender;
-(IBAction)btn2_DownAction:(id)sender;
-(IBAction)btn3_DownAction:(id)sender;
-(IBAction)btn4_DownAction:(id)sender;
-(IBAction)btn_Action:(id)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)btnDownload:(id)sender
{
NSString *urlToDownload = @"http://leocan.co/Salon/images/album/videos/b.mp3";
filePath= [appObj downloadFile:urlToDownload filename:@"myAudio.mp3"];
}
-(IBAction)btn1_DownAction:(id)sender
{
NSString *soundFile;
soundFile = [[NSBundle mainBundle] pathForResource:@"demo1" ofType:@"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:soundFile];
self.playerAudio1 = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
self.playerAudio1.numberOfLoops = -1;
self.playerAudio1.volume=1;
[self.playerAudio1 play];
NSLog(@"hold Down");
}
-(IBAction)btn2_DownAction:(id)sender
{
NSString *soundFile;
soundFile = [[NSBundle mainBundle] pathForResource:@"demo2" ofType:@"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:soundFile];
self.playerAudio2 = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
self.playerAudio2.numberOfLoops = -1;
self.playerAudio2.volume=1;
[self.playerAudio2 play];
NSLog(@"hold Down");
}
-(IBAction)btn3_DownAction:(id)sender
{
NSString *soundFile;
soundFile = [[NSBundle mainBundle] pathForResource:@"demo3" ofType:@"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:soundFile];
self.playerAudio3 = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
self.playerAudio3.numberOfLoops = -1;
self.playerAudio3.volume=1;
[self.playerAudio3 play];
NSLog(@"hold Down");
}
-(IBAction)btn4_DownAction:(id)sender
{
NSString *soundFile;
soundFile = [[NSBundle mainBundle] pathForResource:@"demo6" ofType:@"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:soundFile];
self.playerAudio4 = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
self.playerAudio4.numberOfLoops = -1;
self.playerAudio4.volume=1;
[self.playerAudio4 play];
NSLog(@"hold Down");
}
-(IBAction)btn_Action:(id)sender
{
NSLog(@"hold release");
if([sender tag]==1)
{
[self.playerAudio1 stop];
}
else if([sender tag]==2)
{
[self.playerAudio2 stop];
}
else if([sender tag]==3)
{
[self.playerAudio3 stop];
}
else
{
[self.playerAudio4 stop];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
我想在触摸事件时播放媒体。 在我的应用程序中有 4 个按钮。 当用户使用第一个手指点击第一个按钮时,想要开始在按钮 1 上播放音乐 1。 使用第二根手指立即点击按钮 2,然后想要开始在按钮 2 上播放音乐 2(用户还没有松开他的第一根手指)。 使用第三根手指立即单击按钮 3,然后想要开始在按钮 3 上播放音乐 3(用户还没有松开他的第一根手指和第二根手指)。 我该如何实施? 我对此一无所知。 请帮助我。
我只会用AVAudioPlayer播放音乐
提前致谢
这段代码对我有用。 ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioServices.h>
@interface ViewController : UIViewController
{
NSString *filePath;
}
@property(strong,nonatomic) AVAudioPlayer *playerAudio1;
@property(strong,nonatomic) AVAudioPlayer *playerAudio2;
@property(strong,nonatomic) AVAudioPlayer *playerAudio3;
@property(strong,nonatomic) AVAudioPlayer *playerAudio4;
-(IBAction)btnDownload:(id)sender;
-(IBAction)btn1_DownAction:(id)sender;
-(IBAction)btn2_DownAction:(id)sender;
-(IBAction)btn3_DownAction:(id)sender;
-(IBAction)btn4_DownAction:(id)sender;
-(IBAction)btn_Action:(id)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)btnDownload:(id)sender
{
NSString *urlToDownload = @"http://leocan.co/Salon/images/album/videos/b.mp3";
filePath= [appObj downloadFile:urlToDownload filename:@"myAudio.mp3"];
}
-(IBAction)btn1_DownAction:(id)sender
{
NSString *soundFile;
soundFile = [[NSBundle mainBundle] pathForResource:@"demo1" ofType:@"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:soundFile];
self.playerAudio1 = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
self.playerAudio1.numberOfLoops = -1;
self.playerAudio1.volume=1;
[self.playerAudio1 play];
NSLog(@"hold Down");
}
-(IBAction)btn2_DownAction:(id)sender
{
NSString *soundFile;
soundFile = [[NSBundle mainBundle] pathForResource:@"demo2" ofType:@"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:soundFile];
self.playerAudio2 = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
self.playerAudio2.numberOfLoops = -1;
self.playerAudio2.volume=1;
[self.playerAudio2 play];
NSLog(@"hold Down");
}
-(IBAction)btn3_DownAction:(id)sender
{
NSString *soundFile;
soundFile = [[NSBundle mainBundle] pathForResource:@"demo3" ofType:@"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:soundFile];
self.playerAudio3 = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
self.playerAudio3.numberOfLoops = -1;
self.playerAudio3.volume=1;
[self.playerAudio3 play];
NSLog(@"hold Down");
}
-(IBAction)btn4_DownAction:(id)sender
{
NSString *soundFile;
soundFile = [[NSBundle mainBundle] pathForResource:@"demo6" ofType:@"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:soundFile];
self.playerAudio4 = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
self.playerAudio4.numberOfLoops = -1;
self.playerAudio4.volume=1;
[self.playerAudio4 play];
NSLog(@"hold Down");
}
-(IBAction)btn_Action:(id)sender
{
NSLog(@"hold release");
if([sender tag]==1)
{
[self.playerAudio1 stop];
}
else if([sender tag]==2)
{
[self.playerAudio2 stop];
}
else if([sender tag]==3)
{
[self.playerAudio3 stop];
}
else
{
[self.playerAudio4 stop];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}