播放音樂和視頻及錄音

#import"ViewController.h"

#import

#import

@interfaceViewController()

@end

@implementationViewController

{

AVAudioRecorder* audioRecorder;

AVAudioPlayer* player;

}

- (void)viewDidLoad {

[superviewDidLoad];

//? ? MPMediaPickerController* pickerController=[[MPMediaPickerController alloc]initWithMediaTypes:MPMediaTypeMusic];

//? ? pickerController.prompt=@"請選擇音樂";

//? ? pickerController.allowsPickingMultipleItems=YES;

//? ? pickerController.delegate=self;

//? ? [self presentViewController:pickerController animated:YES completion:^{

//? ? }];

//? ? NSString* filePath=[[NSBundle mainBundle] pathForResource:@"include" ofType:@"mp4"];

//? ? NSURL* sourceNoviewURL=[NSURL fileURLWithPath:filePath];

//? ? AVAsset* moviewAsset=[AVURLAsset URLAssetWithURL:sourceNoviewURL options:nil];

//? ? AVPlayerItem* playItem=[AVPlayerItem playerItemWithAsset:moviewAsset];

//? ? AVPlayer* player=[AVPlayer playerWithPlayerItem:playItem];

//? ? AVPlayerLayer* playerLayer=[AVPlayerLayer playerLayerWithPlayer:player];

//? ? playerLayer.frame=self.view.layer.bounds;

//? ? playerLayer.videoGravity=AVLayerVideoGravityResizeAspect;

//? ? [self.view.layer addSublayer:playerLayer];

//? ? [player play];

//獲取當前應(yīng)用的音頻會話

AVAudioSession* audioSession=[AVAudioSessionsharedInstance];

//設(shè)置音頻類別,playAndRecored即可播放,也可錄制

[audioSessionsetCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];

[audioSessionsetActive:YESerror:nil];

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

}

-(NSString*)documentsPath{

if(!_documentsPath) {

NSArray* searchPaths=

NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

_documentsPath=[searchPathsobjectAtIndex:0];

}

return_documentsPath;

}

//22050采樣率

//8 16 24樣點

//雙聲道2

- (IBAction)player:(id)sender {

}

- (IBAction)recorder:(id)sender {

if(audioRecorder!=nil&&audioRecorder.isRecording) {

[audioRecorderpause];

}

else{

NSString* destinationString=[[selfdocumentsPath]

stringByAppendingPathComponent:@"sound.wav"];

NSURL* destinationURL=[NSURLfileURLWithPath:destinationString];

NSMutableDictionary*recordSettings=[[NSMutableDictionaryalloc]init];

//音頻格式

[recordSettingssetObject:[NSNumbernumberWithInt:kAudioFormatLinearPCM]forKey:AVFormatIDKey];

//采樣率

[recordSettingssetObject:[NSNumbernumberWithFloat:22050]forKey:AVSampleRateKey];

//通道數(shù)

[recordSettingssetObject:[NSNumbernumberWithInt:2]forKey:AVNumberOfChannelsKey];

//樣點位數(shù)

[recordSettingssetObject:[NSNumbernumberWithInt:16]forKey:AVLinearPCMBitDepthKey];

//采用高位優(yōu)先的記錄格式

[recordSettingssetObject:[NSNumbernumberWithBool:YES]forKey:AVLinearPCMIsBigEndianKey];

//采樣信號采用浮點數(shù)

[recordSettingssetObject:[NSNumbernumberWithBool:YES]forKey:AVLinearPCMIsFloatKey];

//初始化

audioRecorder=[[AVAudioRecorderalloc]initWithURL:destinationURLsettings:recordSettingserror:nil];

audioRecorder.delegate=self;

[audioRecorderrecord];

}

}

//-(void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection{

//? ? MPMusicPlayerController* player=[[MPMusicPlayerController alloc]init];

//? ? [player setQueueWithItemCollection:mediaItemCollection];

//? ? [player play];

//}

//-(void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker{

//

//}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容