0

I'm trying to view the video in tableview from my server so fetching the video from the server using the json in ios. I have successfully connected the url to nsurl connection and appending to the nsdata everything is fine but the problem is when converting the nsdata to string it through the null.

this is code I have used for the url connection:

-(void)setDataSource:(vedios *)inVideosObj
{
    //here I'm connecting the nsobject to outlets of the tablecell so here have successfully connected to url connection video is nsobject

    self.titile.text = inVideosObj.title;

    url =[NSURL URLWithString:inVideosObj.video];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];

    NSLog(@"UDSA %@  %@ ",url,request);

    connection =[[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.responseData = [[NSMutableData alloc]init];
}

after that I have checked that rul request passing in console I'm getting the url like this:

2014-01-25 13:00:35.587 video[1835:a0b] UDSA http://localhost/image/vedios/reddy.mp4  <NSURLRequest: 0xb992c40> { URL: http://localhost/image/vedios/reddy.mp4 } 

after I'm appending to the nsdata this is the code:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
{
     [self.responseData appendData:data];

     //NSLog(@"dad %@",self.responseData);
}

once again I printed the nsdata which I have append to in my console im getting like this:

  2014-01-25 14:07:42.024 video[1911:a0b] dad <0000001c 66747970 6d703432 00000000    69736f6d 61766331 6d703432 00008d64 6d6f6f76 0000006c 6d766864 00000000 c8a43177 c8a43177 00000258 00010b78 00010000 01000000 00000000 00000000 00010000 00000000 00000000 00000000 00010000 00000000 00000000 00000000 40000000 00000000 00000000 00000000 00000000 00000000 00000000 00000003 00000015 696f6473 00000000 1007004f ffff2915 ff000052 19747261 6b000000 5c746b68 64000000 01c8a431 77c8a431 77000000 01000000 0000010b 6d000000 00000000 00000000 00010000 00000100 00000000 00000000 00000000 

now here I'm trying to convert the nsdata to string and passing the movieplayer

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *urlString = [[NSString alloc]initWithData:self.responseData encoding:NSASCIIStringEncoding];

    // NSString *urlString = [NSString stringWithUTF8String:[self.responseData bytes] ];

    NSLog(@"data :%@",urlString);
    MPMoviePlayerController *mov = [[MPMoviePlayerController alloc]initWithContentURL: [NSURL URLWithString:urlString]];
//  MPMoviePlayerController *mov = [[MPMoviePlayerController   alloc]initWithContentURL:[NSURL URLWithString:urlString]];
    NSLog(@"length %d",[self.responseData length]);

    // MPMoviePlayerController *mov = [[MPMoviePlayerController   alloc]initWithContentURL:url];

    self.movieplayer = mov;
    [self.movieplayer.view setFrame:CGRectMake(10,20, 100, 100)];

    // self.movieplayer.movieSourceType = MPMovieSourceTypeFile;
    [self.contentView addSubview:self.movieplayer.view];
    [self.movieplayer prepareToPlay];
    [self.movieplayer play];
}

after converting video not playing the tablevewcell I now printed the converted the nsdata sring it's giving the null in my console

2014-01-25 14:12:18.622 video[1934:a0b] data :

I don't no what to do im stuck here for very long I have everything to convert the nsdata to string its showing the same null

this are the link I have tied for the solution research links1 link2

nothing is working please help me how to resolve this problem where I'm doing wrong

this is the output for the self.responsedata

014-01-25 14:24:08.151 video[2019:a0b] reponse=<0000001c 66747970 6d703432 00000000 69736f6d 61766331 6d703432 00008d64 6d6f6f76 0000006c 6d766864 00000000 c8a43177 c8a43177 00000258 00010b78 00010000 01000000 00000000 00000000 00010000 00000000 00000000 00000000 00010000 00000000 00000000 00000000 40000000 00000000 00000000 00000000 00000000 00000000 00000000 00000003 00000015 696f6473 00000000 1007004f ffff2915 ff000052 19747261 6b000000 5c746b68 64000000 01c8a431 77c8a431 77000000 01000000 0000010b 6d000000 00000000 00000000 00010000 00000100 00000000 00000000 00000000 00000100 00000000 00000000 00000000 00400000 00000000 
Cœur
  • 37,241
  • 25
  • 195
  • 267
user3230582
  • 43
  • 2
  • 8
  • You are downloading a mp4 file. You can't convert that to text. Try to initialize `MPMoviePlayerController` with the `http://localhost/image/vedios/reddy.mp4` URL directly. – Matthias Bauch Jan 25 '14 at 08:53
  • this is the output im getting for the self.responsedata – user3230582 Jan 25 '14 at 08:55
  • `-(void)setDataSource:(vedios *)inVideosObj` has a typo. – revolver Jan 25 '14 at 08:58
  • @Mathias i want to display a array of videos in my table directly giving url may wont work – user3230582 Jan 25 '14 at 08:58
  • @revolver i have string in the invideoobj – user3230582 Jan 25 '14 at 09:00
  • Also, don't use ASCII encoding. Never. Ever. The encoding you **should** be using **everywhere** is UTF-8 (`NSUTF8StringEncoding`, `kCFStringEncodingUTF8`). –  Jan 25 '14 at 09:03
  • ya i have already tried with UTF-8 NSUTF8StringEncoding, kCFStringEncodingUTF8 its showing the same through null @H2CO3 – user3230582 Jan 25 '14 at 09:06
  • Encoding won't change anything, because you are downloading a mp4 file! This is not a valid string in any encoding, and because of this it can't be converted to a NSString. In mp4 files `0000001c` is the box length of the atom (or box) that follows. `66747970` is the type of the box, this is ASCII (hex of course) and it means `f t y p`. `6d703432` is the file type, again ASCII and it is `m p 4 2`. So you have a MP4 v2 file, and not JSON or whatever you expected. Which is not surprising because your url ends in `.mp4` – Matthias Bauch Jan 25 '14 at 09:10
  • ok @MatthiasBauch pls tell is there any alternative way to do it – user3230582 Jan 25 '14 at 09:13
  • @user3230582 **What** are you trying to **actually** do? If you have an MP4 file, you can't just convert it to a string. It doesn't make sense. You can play it, you can save the data to a file, you can convert it to another video format... what's your goal? –  Jan 25 '14 at 09:15
  • @H2CO3 i want to view a array videos in the tableview dynamically by just storing the video url in my server this is what excalty im trying to do – user3230582 Jan 25 '14 at 09:21
  • @H2CO3 pls tell me is there any alternative way to do it – user3230582 Jan 25 '14 at 09:22
  • @user3230582 and you want to display the URL of the video? –  Jan 25 '14 at 09:45
  • @H2CO3 no i want to display the video using the url – user3230582 Jan 25 '14 at 09:50
  • @user3230582 Then use `MPMoviePlayerController`, as Matthias Bauch has already suggested. –  Jan 25 '14 at 09:53
  • @H2CO3 ya im using the MPMoviePlayerController only the problem its not passing the url to the moiveplayer – user3230582 Jan 25 '14 at 09:57
  • @user3230582 **what** is "not passing the URL"? –  Jan 25 '14 at 09:57
  • @H2CO3 i have stored url in self.responsedata it a mutabledata what im trying to do converting mutable into string and passing through the movieplayer but its through null thats my problem – user3230582 Jan 25 '14 at 10:03
  • @H2CO3 Using ASCII encoding in `NSString *urlString = [[NSString alloc]initWithData:self.responseData encoding:NSASCIIStringEncoding];` is actually fine, provided the url string is percent encoded. ;) ` – CouchDeveloper Jan 25 '14 at 11:39
  • @CouchDeveloper pls tell me how to encode the url string – user3230582 Jan 25 '14 at 11:47
  • A response which contains "data" specifies the kind of this data with the "Content-Type" header. This "type" is a MIME type appended by an optional _charset parameter_, e.g. "text/plain; charset=utf-8". You can query the response object for the MIME type and the charset parameter. Once you got the "charset" you can obtain the corresponding NSStringEncoding (via system functions), which you need to specify when creating a NSString from a NSData object. – CouchDeveloper Jan 25 '14 at 11:56

3 Answers3

0

It is very strange thing you do here

   NSString *urlString = [[NSString alloc]initWithData:self.responseData encoding:NSASCIIStringEncoding];

   MPMoviePlayerController *mov = [[MPMoviePlayerController alloc]initWithContentURL: [NSURL URLWithString:urlString]];

In your self.responseData you've got NSData of required mp4 file. You only need to store it

[self.responseData writeToFile:pathToFile atomically:NO];

in Documents directory as it is. Then you should make local NSURL with path of file pathToFile

NSURL *localURL = [NSURL fileURLWithPath:pathToFile];

and use it

MPMoviePlayerController *mov = [[MPMoviePlayerController alloc]initWithContentURL:localURL];
malex
  • 9,874
  • 3
  • 56
  • 77
  • is it possible for a array vedio if i store in the database is it will bring the all the vedios uing the path – user3230582 Jan 25 '14 at 09:17
  • If you want a set of videos (array of videos) you need to download them one after another, save locally in separate files with different paths. A then you will have access to them via local url path separately. – malex Jan 25 '14 at 10:14
0

What kind of data is present in inVideosObj.video?

If it is url for the video, the you should pass this url directly in MPMoviePlayerController.

MPMoviePlayerController *mov = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:inVideosObj.video]];

If it contains the video data, then you need to write that data in a file and give file path in MPMoviePlayerController.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"video.mp4"];

[inVideosObj.video writeToFile:path atomically:YES];

NSURL *videoUrl= [NSURL fileURLWithPath:path];

MPMoviePlayerController *mov = [[MPMoviePlayerController alloc] initWithContentURL: videoUrl];
-1

This this, if your data is not null terminated,

NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

If your data is null terminated, try this:

NSString *str = [NSString stringWithUTF8String:[data bytes]];

Edit

Check this, it may help. :)

Community
  • 1
  • 1
Rashad
  • 11,057
  • 4
  • 45
  • 73
  • Are you sure you are getting correct data from web? That should do the work, I've done this many times before. There might be some problem in server side. – Rashad Jan 25 '14 at 10:52