標籤: ios Objective-Chtml
發佈時間: 2014/12/12 7:48:06ios
在個人項目我有 Url 的音頻......看看個人代碼...objective-c
ViewController.h數組
#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface ViewController : UIViewController<AVAudioPlayerDelegate,NSURLConnectionDelegate> { AVAudioPlayer *song; // NSMutableData *fileData; NSData *myData; } @property (nonatomic, retain) AVAudioPlayer *song; @property(nonatomic,retain) NSData *myData; @end
ViewController.mide
#import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize song,myData; - (void)viewDidLoad { [super viewDidLoad]; NSURL *myUrl; myUrl = [NSURL URLWithString:@"http://p0.bcbits.com/download/track/60523f9784a2912348eff92f7b7e21e8/mp3-128/1269403107?c22209f7da1bd60ad42305bae71896761f6cd1f4d6c29bf401ef7e97b90c3d5939b57f5479b37ad4d41c48227740d7ba8b5f79b76aa8d6735b8f87c781f44fb019762a2903fe65aeafb30d90cb56b385e27cd770cc9f9d60e5ea3f130da6ad3db728ff7e24a09fab9f351120810ee91f78f1e94fcabc98aabb37d4248358f6da4a0fa7a74fe8c89da2a768&fsig=cd06c325fc41b6f8edb0409011e8839c&id=1269403107&stream=1&ts=1395489600.0"]; myData = [NSData dataWithContentsOfURL:myUrl]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
在上面的代碼如何能夠將轉換 mydata 到一個數組???或如何能夠下載這個音頻和在手機內存中存儲??atom
您能夠存儲設備內存做爲像下面將音頻數據spa
myData = [NSData dataWithContentsOfURL:myUrl]; [myData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/audio.mp3"] atomically:YES];
要播放您保存在設備中的音頻文件:3d
添加 AudioToolbox.framework
和 AVFoundation.framework
爲您的項目生成code
在.h 文件中htm
#import <AudioToolbox/AudioToolbox.h> #import <AVFoundation/AVFoundation.h> AVAudioPlayer *player;
在.m 文件
-(void)initPlayer { AVAudioSession *audioSession = [AVAudioSession sharedInstance]; UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride); NSError *err; [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&err]; audioSession.delegate = self; [audioSession setActive:YES error:&err]; player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/audio.mp3"]] error:nil]; } -(void)playFile { if (player.playing) { [player pause]; isPlaying = NO; } else { [player play]; isPlaying = YES; } }
謝謝你 !