您好,在我的应用程序中,我已将视频网址从我的服务器传递为json
,并且我在UITableView
中显示了视频标题。我想通过点击单元格来播放视频。我尝试了一些方法但它们没有用。
我的JSON获取视频代码:
-(void)details{
NSString *urlString = [NSString stringWithFormat:@"myurl"];
NSURL *url = [NSURL URLWithString:urlString];
NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"the str==%@",jsonString);
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
videoarray = [[NSMutableArray alloc]init];
NSArray *array=[results objectForKey:@"videos"];
for (int i=0; i<[array count]; i++) {
NSString *filed =[[array objectAtIndex:i]objectForKey:@"v_name"];
//my video url stored in a nstring video
NSString *video =[[array objectAtIndex:i]objectForKey:@"video"];
NSLog(@"the url==%@",video);
NSLog(@"the field==%@",filed);
// i have ready nsobject to store the data and im adding to the nsmutablearray
video1 *myimg =[[video1 alloc]initWithname:filed andvideo:video];
[videoarray addObject:myimg];
}
[self.mytableview reloadData];
}
当我打印网址时,它的json就像这样。
the url==http://jesusredeems.tv/media/videos/2014 Promise Message/Promise Message 2014-Part 2.mp4
之后在didSelectRowAtIndexPath
方法中我就这样给出了。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
video1 * curnt = [videoarray objectAtIndex:indexPath.row];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc]initWithContentURL: [NSURL URLWithString:curnt.video]];
[[player view] setFrame: [self.view bounds]]; // frame must match parent view
[self.view addSubview: [player view]];
[player play];
}
我已经使用上面的代码在点击单元格的同时播放视频,但在点击它后显示黑屏这样。
我已经在Media player
中导入了UITableView
,但它无效。请告诉我如何解决此问题以及上述代码中我出错的地方
感谢。
答案 0 :(得分:1)
试试这个
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//your array here
NSString * currentVideo = [array1 objectAtIndex:indexPath.row];
_moviePlayerTemp = [[MPMoviePlayerController alloc]initWithContentURL: [NSURL URLWithString:[self urlencode:currentVideo]]];
[[_moviePlayerTemp view] setFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
// frame must match parent view
[self.view addSubview: [_moviePlayerTemp view]];
[_moviePlayerTemp setShouldAutoplay:YES];
[_moviePlayerTemp prepareToPlay];
[_moviePlayerTemp play];
}
-(NSString *)urlencode:(NSString *)str
{
NSString *encodeString=(NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)str, NULL, (CFStringRef)@"", kCFStringEncodingUTF8));
return encodeString;
}
并像这样打电话。
NSString *string=@"http://jesusredeems.tv/media/videos/2014 Promise Message/Promise Message 2014-Part 2.mp4";
NSString *temp=[self urlencode:string];
答案 1 :(得分:0)
尝试按照以下帖子创建MPMoviePlayerController
:play a .mp4 or .mov video from either an Internet URL or a local file in iOS?
除此之外,创建MPMoviePlayerController变量是函数中的iVar变量NOT局部变量,就像在didSelectRowAtIndexPath
答案 2 :(得分:0)
应用内的视频。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *path =[[NSBundle mainBundle]pathForResource:@"YOUR Video File Name" ofType:@"mp4"];
_moviePlayer=[[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(playbackFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
[_moviePlayer setScalingMode:MPMovieScalingModeAspectFit];
[_moviePlayer setFullscreen:FALSE];
self.moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.view.frame = self.videoPlayerView.bounds;
[_moviePlayer setFullscreen:NO];
_videoPlayerView.hidden=NO;
[self.videoPlayerView addSubview:_moviePlayer.view];
[self.view bringSubviewToFront:self.videoPlayerView];
[_moviePlayer prepareToPlay];
[_moviePlayer play];
}
//After finishing playing.
-(void)finishedPlaying:(NSNotification *)sender
{
MPMoviePlayerController *player =[sender object];
[player stop];
// [player.view removeFromSuperview];
self.videoPlayerView.hidden=YES;
if([player isFullscreen])
[self dismissViewControllerAnimated:YES completion:nil];
_moviePlayer = nil;
}