iOSオーディオ開発AVAudioPlayerの使用、音響再生AudioServicesPlaySystemSoundの使用


オーディオ開発AVAudioPlayerの使用AVAudioPlayerはiOSシステムが持参したローカルmp 3ファイルを再生できるクラスです(注意:ローカルのみ再生可能)参考:http://www.jianshu.com/p/589999e534611,先導入庫AVFoundation/AVFoundation.hエージェントAVAudioPlayerDelegate 2を行い、再生するmp 3ファイルをプロジェクトにインポートするには、使用するグローバル変数@property(nonatomic,strong)AVAudioPlayer*playerを定義する.property (weak, nonatomic) IBOutlet UIProgressView *Progress; @property (weak, nonatomic) IBOutlet UILabel *CurrentTime; @property (weak, nonatomic) IBOutlet UILabel *countTime; @property (nonatomic,strong)NSArray *sourceArray;//歌曲配列@property(nonatomic,copy)NSString*currentsong;@property (nonatomic,strong)NSTimer *timer;//タイマ
-(void)viewDidLoad { [super viewDidLoad];
_sourceArray = @[@"muise",@"music2"];

NSUserDefaults *usedefault = [NSUserDefaults standardUserDefaults];
NSString *songStr = [usedefault objectForKey:@"current_song"];
if (songStr.length == 0) {

    songStr = _sourceArray[0];

    [usedefault setObject:@"0" forKey:@"current_song"];
    [usedefault synchronize];
}
NSString *str = _sourceArray[[songStr intValue]];

[self PlayMusicWith:str];

_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
[_timer setFireDate:[NSDate distantFuture]];


// Do any additional setup after loading the view, typically from a nib.

}
//一時停止ボタン-(IBAction)Stop:(id)sender{
[self.player stop];
[_timer setFireDate:[NSDate distantFuture]];

}
//次のボタン-(IBAction)Next:(id)sender{
 [self NextSong];

}
//前のボタン-(IBAction)Previous:(id)sender{
NSUserDefaults *usedefault = [NSUserDefaults standardUserDefaults];
NSString *songStr = [usedefault objectForKey:@"current_song"];

int a = [songStr intValue]-1;

if (a <0) {

    a = (int)_sourceArray.count -1;
    [usedefault setObject:[NSString stringWithFormat:@"%d",a] forKey:@"current_song"];
    [usedefault synchronize];
}else{

    [usedefault setObject:[NSString stringWithFormat:@"%d",a]forKey:@"current_song"];
    [usedefault synchronize];
}


NSLog(@"   a==== %d",a);

 [self PlayMusicWith:_sourceArray[a]];

}
//プレーヤーを初期化して再生-(void)PlayMusicWith:(NSString*)songName{
NSURL *url = [[NSBundle mainBundle] URLForResource:songName withExtension:@"mp3"];
self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
self.player.delegate = self;
 NSInteger countTime = self.player.duration;
_countTime.text = [NSString stringWithFormat:@"%d:%d",(int)countTime/60,(int)countTime%60];//     
[self.player play];
//          
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];

}
//エージェントメソッド
//放送終了自動切り替え次へ-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer*)Player successfully:(BOOL)flag{NSLog(@"放送終了")//次への切り替え
[self NextSong];

}
//割り込み-(void)audioPlayerBeginInterruption:(AVAudioPlayer*)Player{NSLog(@"一般的にこの方法ではオーディオを一時停止します");//[Player stop];}
//割り込み終了時にこのメソッドが呼び出されます-(void)a u dioPlayerEndInterruption:(AVAudioPlayer*)player withOptions:(NSUInteger)flags{//このメソッドでオーディオ[player play];NSLog(@"継続再生");}
//復号エラーの場合このメソッドが呼び出されます-(void)a u d i o PlayerDecodeErrorDidOccur:(AVAudioPlayer)Player error:(NSError)error{NSLog(@"ファイルエラー");}
//次の曲-(void)Next Song{
NSUserDefaults *usedefault = [NSUserDefaults standardUserDefaults];
NSString *songStr = [usedefault objectForKey:@"current_song"];

int a = [songStr intValue]+1;

if (a >=_sourceArray.count) {

    a = 0;
    [usedefault setObject:@"0" forKey:@"current_song"];
    [usedefault synchronize];
}else{

    [usedefault setObject:[NSString stringWithFormat:@"%d",a]forKey:@"current_song"];
    [usedefault synchronize];
}

NSLog(@"   a==== %d",a);

[self PlayMusicWith:_sourceArray[a]];

}
-(void)timerAction{
NSUInteger a = self.player.currentTime;

//_CurrentTime.text = [[NSString stringWithFormat:@”%.2f”,a/60.00,self.player.currentTime] stringByReplacingOccurrencesOfString:@”.” withString:@”:”];
NSLog(@"kkkk %ld  %f",a,self.player.currentTime);


int b = a%60;

if (b<10) {


}

int c = (int)a/60;

_CurrentTime.text =[NSString stringWithFormat:@"%d:%d",c,b];// [[NSString stringWithFormat:@"%",a/60.00] stringByReplacingOccurrencesOfString:@"." withString:@":"];
_Progress.progress = _player.currentTime/_player.duration;

}
音響再生AudioServicesPlaySystemSound使用-(IBAction)Playmusic:(id)sender{
NSLog(@"    ");

[self.player play];

[_timer setFireDate:[NSDate distantPast]];
 NSLog(@"hhhh %f",self.player.currentTime);

}
-(void)playSoundEffect:(NSString*)name {
//       
NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:name ofType:nil];

NSURL *fileUrl = [NSURL fileURLWithPath:audioFilePath];

//1.      ID
SystemSoundID soundID = 0;

AudioServicesCreateSystemSoundID((__bridge CFURLRef)(fileUrl), &soundID);

//2.               ,        
AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL,soundCompleteCallBack, NULL);

//3.    
AudioServicesPlaySystemSound(soundID);

//               ,     
AudioServicesPlaySystemSoundWithCompletion(soundID, ^{


    NSLog(@"      ");
});


AudioServicesPlayAlertSound(soundID); //       

}
void soundCompleteCallBack(SystemSoundID soundID,void* clientDate) {
NSLog(@"    ");

} iOS 音频开发AVAudioPlayer的使用,音效播放AudioServicesPlaySystemSound使用_第1张图片