ios - Using AVPlayer and AVPlayerViewController, but audio and video not present -
my code looks this:
_player = [avplayer playerwithurl:destination]; _playervc = [avplayerviewcontroller new]; _playervc.player = _player; dispatch_async(dispatch_get_main_queue(), ^{ [self presentviewcontroller:_playervc animated:yes completion:^{ [_player play]; }]; });
_player represents avplayer , _playervc represents avplayerviewcontroller. have strong global references these objects.
using terminal, played file located @ destination (open -a quicktime\ player destinationurlabsolutestring) , saw file loaded since playing.
it m4v file. have played m4a file , gave me audio. have substituted url have destination remote url , worked video. leads me believe has video. what's weird avplayerviewcontroller show black screen normal controls, , shows video 2 minutes , 23 seconds. upon opening video file manually, can see 2 minutes , 23 seconds.
i can forward through video dragging white dot indicative of video's position, never hear anything, nor see black screen , controls.
tl;dr
nslog(@"destination: %@",destination); prints: file:///users/mylogin/library/developer/coresimulator/devices/694f4c94-f785-4931-a312-4c3e7de8673a/data/containers/data/application/76c923be-5b25-41f7-9145-63414657fdf6/documents/mzvf_5914002519860647075.640x352.h264lc.d2.p.m4v
all in all, looks i'm retrieving video, reason player controller black , void of audio. appreciated.
source code:
.m file:
#import "viewcontroller.h" #import "view2viewcontroller.h" #import <avkit/avkit.h> #import <avfoundation/avfoundation.h> @interface viewcontroller () @property nsdictionary *requestedsong; @property (strong) avplayer *player; //declare globally @property (strong) avplayerviewcontroller *playervc; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; self.view.backgroundcolor = [uicolor yellowcolor]; uibutton *button = [[uibutton alloc]initwithframe:cgrectmake(0, 0, 200, 50)]; button.center = self.view.center; button.backgroundcolor = [uicolor bluecolor]; [button settitle:@"query songs" forstate:uicontrolstatenormal]; [button addtarget:self action:@selector(buttonclicked) forcontrolevents:uicontroleventtouchdown]; [self.view addsubview:button]; uibutton *button2 = [[uibutton alloc]initwithframe:cgrectmake(0, 0, 200, 50)]; button2.center = cgpointmake(button.center.x, button.center.y + 200); button2.backgroundcolor = [uicolor bluecolor]; [button2 settitle:@"listen first song" forstate:uicontrolstatenormal]; [button2 addtarget:self action:@selector(button2clicked) forcontrolevents:uicontroleventtouchdown]; [self.view addsubview:button2]; } -(void)button2clicked{ nsurlsession *session = [nsurlsession sessionwithconfiguration:[nsurlsessionconfiguration defaultsessionconfiguration] delegate:self delegatequeue:[[nsoperationqueue alloc]init]]; nsurl *url = [nsurl urlwithstring:[_requestedsong objectforkey:@"previewurl"]]; nsmutableurlrequest *req = [[nsmutableurlrequest alloc]initwithurl:url]; nsurlsessiondownloadtask *task = [session downloadtaskwithrequest:req]; [task resume]; } -(void)buttonclicked{ nsurlsession *session = [nsurlsession sessionwithconfiguration:[nsurlsessionconfiguration defaultsessionconfiguration] delegate:self delegatequeue:[[nsoperationqueue alloc]init]]; nsurl *url = [nsurl urlwithstring:@"https://itunes.apple.com/search?media=movie&entity=movie&term=mad"]; nsmutableurlrequest *req = [[nsmutableurlrequest alloc]initwithurl:url]; nsurlsessiondatatask *task = [session datataskwithrequest:req]; [task resume]; } -(nsurl*)localfilepathforurl:(nsurl *) previewurl{ //get directory use nsstring *documentspath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, true) objectatindex:0]; //create full path nsstring *fullpath = [documentspath stringbyappendingpathcomponent:previewurl.lastpathcomponent]; //append filename , append document path url return [nsurl fileurlwithpath:fullpath]; } - (void)urlsession:(nsurlsession *)session downloadtask:(nsurlsessiondownloadtask *)downloadtask didfinishdownloadingtourl:(nsurl *)location{ nsurl* originalurl = downloadtask.originalrequest.url; nsurl *destination = [self localfilepathforurl:originalurl]; nslog(@"destination: %@",destination); nsfilemanager *defaultfilemanager = [nsfilemanager defaultmanager]; if([defaultfilemanager fileexistsatpath:destination.absolutestring]){ [defaultfilemanager removeitematurl:destination error:nil]; } nserror *error; @try { [defaultfilemanager copyitematurl:location tourl:destination error:&error]; } @catch (nsexception *exception) { nslog(@"copy caught exception: %@",exception); return; } dispatch_async(dispatch_get_main_queue(), ^{ _player = [avplayer playerwithurl:destination]; _playervc = [avplayerviewcontroller new]; _playervc.player = _player; [self presentviewcontroller:_playervc animated:yes completion:^{ [_player play]; }]; }); nslog(@"hello"); } - (void)urlsession:(nsurlsession *)session datatask:(nsurlsessiondatatask *)datatask didreceivedata:(nsdata *)data{ nserror *error2; nsdictionary *dict = [nsjsonserialization jsonobjectwithdata:data options:1 error:&error2]; _requestedsong = [[dict valueforkey:@"results"] objectatindex:0]; nslog(@"searched: %@",[_requestedsong objectforkey:@"trackname"]); } - (void)urlsession:(nsurlsession *)session datatask:(nsurlsessiondatatask *)datatask didreceiveresponse:(nsurlresponse *)response completionhandler:(void (^)(nsurlsessionresponsedisposition disposition))completionhandler { completionhandler(nsurlsessionresponseallow); } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } @end
.h file:
#import <uikit/uikit.h> @interface viewcontroller : uiviewcontroller <nsurlsessiondatadelegate, nsurlsessiondownloaddelegate> @end
update:
upon using actual url endpoint mad max instead of downloading video, displays blank video. must mean there's weird format of video or how interacts avplayer.
you should setup observer avplayer status call play when ready play. how - check answers here: avplayer , local files
Comments
Post a Comment