简体   繁体   中英

Get link MP3 from a web

I want to get a MP3 link from a website, I don't know exactly what type of this website is, but it only has content like this (link: http://www.nhaccuatui.com/download/song/4Upyxq0QlytX )

{"error_message":"Success","data":{"stream_url":"http:\/\/download.s81.stream.nixcdn.com\/dd634cb8afcc15d7c17a8ce4c548709f\/533cc58e\/NhacCuaTui791\/KhongQuanTam-ChiDan_4cyw4_hq.mp3","is_charge":"false"},"error_code":0,"STATUS_READ_MODE":true}

What can I do if I want to get content (link mp3) from key (?) "stream_url" from this link to put it in to my iOS Applications?

If you rearrange your JSON it will look like this:

{
   "error_message":"Success",
   "data":{
      "stream_url":"http:\/\/download.s81.stream.nixcdn.com\/dd634cb8afcc15d7c17a8ce4c548709f\/533cc58e\/NhacCuaTui791\/KhongQuanTam-ChiDan_4cyw4_hq.mp3",
      "is_charge":"false"
   },
   "error_code":0,
   "STATUS_READ_MODE":true
}

From that you can see its a dictionary.

If you want to get it via url request do something like this:

NSDictionary *dictionaryData;
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.00];
NSHTTPURLResponse *response;
NSError *error = [[NSError alloc]init];
NSData *apiData = [NSURLConnection sendSynchronousRequest:requestURL returningResponse:&response error:&error];
dictionaryData = [NSJSONSerialization JSONObjectWithData:apiData options:kNilOptions error:&error];

Then you can get the url by doing this:

NSString *str = [[dictionaryData objectForKey:@"data"] objectForKey:@"stream_url"];

Then make another request for getting the file.

The data you presented is in JSON format. To access it, use NSJSONSerialization .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM