播放音樂和視頻及錄音

#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];

//獲取當(dāng)前應(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樣點(diǎn)

//雙聲道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];

//樣點(diǎn)位數(shù)

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

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

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

//采樣信號采用浮點(diǎn)數(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)系作者
  • 序言:七十年代末蚯嫌,一起剝皮案震驚了整個(gè)濱河市渠概,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌舶替,老刑警劉巖令境,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異顾瞪,居然都是意外死亡舔庶,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進(jìn)店門陈醒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來惕橙,“玉大人,你說我怎么就攤上這事钉跷∶逐校” “怎么了?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵爷辙,是天一觀的道長彬坏。 經(jīng)常有香客問我,道長犬钢,這世上最難降的妖魔是什么苍鲜? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任思灰,我火速辦了婚禮玷犹,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘洒疚。我一直安慰自己歹颓,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布油湖。 她就那樣靜靜地躺著巍扛,像睡著了一般。 火紅的嫁衣襯著肌膚如雪乏德。 梳的紋絲不亂的頭發(fā)上撤奸,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天,我揣著相機(jī)與錄音喊括,去河邊找鬼胧瓜。 笑死,一個(gè)胖子當(dāng)著我的面吹牛郑什,可吹牛的內(nèi)容都是我干的府喳。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼蘑拯,長吁一口氣:“原來是場噩夢啊……” “哼钝满!你這毒婦竟也來了兜粘?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤弯蚜,失蹤者是張志新(化名)和其女友劉穎孔轴,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體熟吏,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡距糖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了牵寺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片悍引。...
    茶點(diǎn)故事閱讀 40,133評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖帽氓,靈堂內(nèi)的尸體忽然破棺而出趣斤,到底是詐尸還是另有隱情,我是刑警寧澤黎休,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布浓领,位于F島的核電站,受9級特大地震影響势腮,放射性物質(zhì)發(fā)生泄漏联贩。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一捎拯、第九天 我趴在偏房一處隱蔽的房頂上張望泪幌。 院中可真熱鬧,春花似錦署照、人聲如沸祸泪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽没隘。三九已至,卻和暖如春禁荸,著一層夾襖步出監(jiān)牢的瞬間右蒲,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工赶熟, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留瑰妄,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓钧大,卻偏偏與公主長得像翰撑,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容