IOS MP3 streaming media playback edge buffer edge playback Freestreamer Frame

Source: Internet
Author: User

On the internet to see someone said Avplay can play streaming media, then take over to use, and found that anyway it is to the MP3 cache after the completion of the play, the audio long to wait, for the M4A format is better, MP3 is not, and this experience is very poor. So on the Internet to collect methods, and found the Freestreamer library can achieve edge caching while playing MP3 files, online data is relatively small. Here's a general summary. My code doesn't write well, just give you what you need. mp3 and M4A format is followed by a douaudiostreamer summary can refer to the following

1. Add Freestreamer Library using Cocoapods

Pod ' Freestreamer ', ' ~> 3.7.2 ' 2.

#import <FSAudioStream.h>

-(void) Viewwillappear: (BOOL) animated{

[Superviewwillappear:animated];

Start to accept remote control prepare to add to audio center background playback

[[Uiapplicationsharedapplication]beginreceivingremotecontrolevents];

[selfbecomefirstresponder];

}

-(void) Viewwilldisappear: (BOOL) animated{

[Superviewwilldisappear:animated];

End remote control prepare to add to audio center background playback

[[Uiapplicationsharedapplication]endreceivingremotecontrolevents];

[selfresignfirstresponder];

}

Overriding the parent class becomes responder method preparation for adding to the audio center background playback

-(BOOL) Canbecomefirstresponder {

return YES;

}

#pragma mark rewrites the parent class method and accepts the handling of external events to prepare for adding to the audio center background playback

-(void) Remotecontrolreceivedwithevent: (uievent *) receivedevent {

if (Receivedevent.type ==uieventtyperemotecontrol) {

Switch (Receivedevent.subtype) {//Get event Type

Case uieventsubtyperemotecontroltoggleplaypause://Pause IOS6

self. Play =YES//The response method that calls the pause button for your project is also the case below

[selfplayaction];

break;

Case Uieventsubtyperemotecontrolprevioustrack://Previous Song

[selflastbuttonaction];

break;

Case uieventsubtyperemotecontrolnexttrack://Next Song

[selfnextbuttonaction];

break;

Case uieventsubtyperemotecontrolplay://Playback

self. Play =NO;//The response method that calls the pause button for your project is also the case below

[selfplayaction];

break;

Case uieventsubtyperemotecontrolpause://Pause iOS7

self. Play =YES//The response method that calls the pause button for your project is also the case below

[selfplayaction];

break;

default:

break;

}

}

}

Now Playing Center can display music information in the lock screen interface, and also to enhance the user experience.

#pragma mark passes the message to the lock screen. This method is invoked when playing songs and switching songs

-(void) Confignowplayingcenter: (Yipinxiangxibody *) Body {

Nsmutabledictionary * info = [Nsmutabledictionarydictionary];

The title of the music

[Info setObject:body.titleforKey:MPMediaItemPropertyTitle];

Artist of Music

[Info setObject:body.columnNameforKey:MPMediaItemPropertyArtist];

The cover of the music

UIImage *image = [[Uiimagealloc]initwithdata:[nsdatadatawithcontentsofurl:[nsurlurlwithstring: [ nsstringstringwithformat:@ "%@%@", Urladdress,body.cover]]];

Mpmediaitemartwork * artwork = [[Mpmediaitemartworkalloc]initwithimage:image];

[Info setObject:artworkforKey:MPMediaItemPropertyArtwork];

Complete Setup

[[Mpnowplayinginfocenterdefaultcenter]setnowplayinginfo:info];

NSLog (@ "----%LLD--------%f------", self.player.currenttime.value,self.totaltime);

}

#pragma mark initialization player

-(void) Playerinit: (Nsurl *) url{

if (!_audiostream) {

Creating Fsaudiostream Objects

_audiostream=[[fsaudiostreamalloc]init];

Set sound

[_audiostreamsetvolume:1];

}

_audiostream.url = URL;

[_audiostream play];

_audiostream.onfailure=^ (Fsaudiostreamerror error,nsstring *description) {

NSLog (@ "Play a problem%@", description);

};

__weak typeof (self) weakself =self;

_audiostream.oncompletion=^ () {

[Weakself nextbuttonaction];//continue playing next after playback completes

};

Timer gets playback progress and cache progress

self. Playertimer = [Nstimerscheduledtimerwithtimeinterval:1target:selfselector:@selector (playprogressaction) UserInfo:nilrepeats:YES];

Add video information to playing Center lock screen display playback information control playback pause

[selfconfignowplayingcenter:self. Yipinxiangxi.body];

}

#pragma mark audio cache and playback Progress tips

-(void) playprogressaction{

Fsstreamposition cur =self. audiostream.currenttimeplayed;

self. Playbacktime =cur.playbacktimeinseconds/1;

Double minuteselapsed =floor (fmod (self. playbacktime/60.0,60.0));

Double secondselapsed =floor (fmod (self. playbacktime,60.0));

self. nowtimelabel.text = [nsstringstringwithformat:@ "%02.0f:%02.0f", minuteselapsed, secondselapsed];

self. Sliderprogress.value = cur.position;//

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.