iOSから音楽プレーヤーを制作
4232 ワード
一、オーディオ再生と音響再生の基礎知識
サウンド再生
1、音響再生「1」機能紹介比較的短い音響を再生するためによくシステムとして使用されるヒント音を再生するためのフレームAudioToolBox「2」フレーム紹介1、SystemSoundID 2、AudioServicesCreateSystemSoundID 3、AudioServicesPlaySystemSound再生システム音声4、AudioServicesPlayAlertSound
1つのインスタンスはフレームワーク#import 1にインポートされ、1つのsoundIDがこの数字に基づいてどのシステム音声であるかを区別することを宣言する
2、システム音声を作成するサービス注意修復エラー
3、放送システムの音帯振動
振動やAudioServicesPlaySystemSound(soundID)を持たない.
オーディオ再生
1、紹介機能は、比較的長い音声、説明、音楽を再生するためのもので、AVFoundation を使用する.フレームワーク紹介AVAudioPlayer 初期化:(3)グローバル変数の音楽再生オブジェクト、または属性の音楽再生オブジェクトを宣言しなければ再生できない(4)再生ページを終了するときは必ず再生オブジェクトを空にしながらdelegateを 空にする.
インポートフレームワーク:#import宣言グローバル変数
オーディオの基本プロパティ:[audioPlayer prepareToPlay];現在の音楽のチャネルaudioPlayerを取得する.numberOfChannels audioPlayer.currentTime//現在の再生時間audioPlayer.Playing//audioPlayerを再生しているかどうかを判断します.numberOfLoops ;//ループ再生の今回のaudioPlayerを設定.durationは、オーディオを再生する時間audioPlayerを得る.panは左右のチャネル効果audioPlayerを設定.volume設定音量0.0-1.0はパーセント設定レートでenableRateをYESに設定する必要があります.audioPlayer.enableRate =YES; audioPlayer.rate =3.0; 音量volume = 0.1; 再生回数の負数が無限ループである0を1回1回2回順次類推audioPlayerとする.numberOfLoops = 0; 現在のピークaudioPlayer peakPowerForChannel:2平均ピークaudioPlayer averagePowerForChannel:2オーディオ再生のいくつかのプロキシメソッドを取得します.
コード例:(このリンクの下には完全な音楽プレーヤーDemoがあり、再生されたクラスがカプセル化されています)ViewDidLoadで
サウンド再生
1、音響再生「1」機能紹介比較的短い音響を再生するためによくシステムとして使用されるヒント音を再生するためのフレームAudioToolBox「2」フレーム紹介1、SystemSoundID 2、AudioServicesCreateSystemSoundID 3、AudioServicesPlaySystemSound再生システム音声4、AudioServicesPlayAlertSound
1つのインスタンスはフレームワーク#import 1にインポートされ、1つのsoundIDがこの数字に基づいてどのシステム音声であるかを区別することを宣言する
SystemSoundIDsoundID = 1;
2、システム音声を作成するサービス注意修復エラー
AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)([[NSBundle
mainBundle]URLForResource:@" .aiff"withExtension:nil]), &soundID) ;
3、放送システムの音帯振動
AudioServicesPlayAlertSound(soundID);
振動やAudioServicesPlaySystemSound(soundID)を持たない.
オーディオ再生
1、紹介
インポートフレームワーク:#import宣言グローバル変数
@interface ViewController ()
{
AVAudioPlayer*audioPlayer;
}
@end
オーディオの基本プロパティ:[audioPlayer prepareToPlay];現在の音楽のチャネルaudioPlayerを取得する.numberOfChannels audioPlayer.currentTime//現在の再生時間audioPlayer.Playing//audioPlayerを再生しているかどうかを判断します.numberOfLoops ;//ループ再生の今回のaudioPlayerを設定.durationは、オーディオを再生する時間audioPlayerを得る.panは左右のチャネル効果audioPlayerを設定.volume設定音量0.0-1.0はパーセント設定レートでenableRateをYESに設定する必要があります.audioPlayer.enableRate =YES; audioPlayer.rate =3.0; 音量volume = 0.1; 再生回数の負数が無限ループである0を1回1回2回順次類推audioPlayerとする.numberOfLoops = 0; 現在のピークaudioPlayer peakPowerForChannel:2平均ピークaudioPlayer averagePowerForChannel:2オーディオ再生のいくつかのプロキシメソッドを取得します.
//
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
NSLog(@" ");
}
//
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error{
}
//
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{
}
//
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags{
}
コード例:(このリンクの下には完全な音楽プレーヤーDemoがあり、再生されたクラスがカプセル化されています)ViewDidLoadで
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor brownColor];
[button setTitle:@"Play" forState:UIControlStateNormal];
[button setTitle:@"Pause" forState:UIControlStateSelected];
[button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[self playMusicWithName:@"TFBOYS- .mp3"];
:
- (void)play:(UIButton *)sender{
sender.selected = !sender.selected;
sender.selected != YES ? [audioPlayer pause]:[audioPlayer play];
}
- (void)playMusicWithName:(NSString *)name{
NSError *error;
//
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[[NSBundle mainBundle]URLForResource:name withExtension:nil] error:&error];
if (error) {
NSLog(@"%@",error);
}
[audioPlayer prepareToPlay];
[audioPlayer play];
NSLog(@"%ld",audioPlayer.numberOfChannels);
durtion:
-1.0 0.0 1.0
audioPlayer.pan = 0.0;
audioPlayer.volume = 0.1;
enableRate YES
audioPlayer.enableRate = YES;
0.5 1.0 2.0
audioPlayer.rate = 1.0;
currentTime
meteringEnabled YES
audioPlayer.meteringEnabled = YES;
[audioPlayer updateMeters];
NSLog(@" :%f",[audioPlayer peakPowerForChannel:2]);
NSLog(@" %f",[audioPlayer averagePowerForChannel:2]);
0 1
audioPlayer.numberOfLoops = 0;
audioPlayer.delegate = self;
}