[Code Note] iOS-play the voice downloaded from the network, ios-voice

Source: Internet
Author: User

[Code Note] iOS-play the voice downloaded from the network, ios-voice

Code:

ViewController. m

 

# Import "ViewController. h "// recording # import <AVFoundation/AVFoundation. h> @ interface ViewController () {// player AVAudioPlayer * player;} @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib .} // when you click anywhere, play the sound-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {// https://wifamily.blob.core.chinacloudapi.cn/wom/2015090211559116.mp3 // sound when playing AVAudioSession * audioSession = [AVAudioSession sharedInstance]; NSError * err = nil; [audioSession setCategory: AVAudioSessionCategoryPlayback error: & err]; NSString * voiceUrl = @ "https://wifamily.blob.core.chinacloudapi.cn/wom/2015090211559116.mp3"; NSLog (@ "--- voiceUrl -- % @", voiceUrl); NSURL * url = [[NSURL alloc] initWithString: voiceUrl]; NSData * audioData = [NSData dataWithContentsOfURL: url]; // Save the data to the specified local location NSString * docDirPath = [inline (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]; NSString * filePath = [NSString stringWithFormat: @ "% @/%@.mp3", docDirPath, @ "temp"]; [audioData writeToFile: filePath atomically: YES]; // play NSError * playerError; NSURL * fileURL = [NSURL fileURLWithPath: filePath]; player = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: & playerError]; if (player = nil) {NSLog (@ "-- play -- error --- % @", [playerError description]);} else {[player play];} -(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.