Using NSData with MPMoviePlayerViewController - ios

I am trying to use NSData with MPMoviePlayerViewController.
NSData *data = [NSData dataWithBytesNoCopy:mData3->mappedAddress+100398125 length:2313453 freeWhenDone:NO];
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF16StringEncoding];
NSURL *movieURL = [NSURL fileURLWithPath:dataString];
MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
This leads to the player opening for a second and then being dismissed.
When I access the movie file locally using the URL to the file in the main bundle it plays perfectly.
How does one use NSData to play a video on iOS?
Thanks

There is no way to play a movie directly from an in-memory NSData blob.

Related

AVAudioPlayer initialization with web URL crashes

I have a sound file on a server and I try to play it on device.
Using this code:
NSURL audioURL = [NSURL URLWithString:#"http://..."];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error]
crashes with the error:
Description of exception being thrown: '-[NSTaggedPointerString getCharacters:range:]: Range {0, 12} out of bounds; string length 5
The url has the length 73.
Firstly, why is it crashing instead of populating my error object?
If I use this code:
NSURL audioURL = [NSURL URLWithString:#"http://..."];
NSData *audioData = [NSData dataWithContentsOfURL:audioURL];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:audioData error:&error]
it works.
Looking at the name of the methods, it looks like they do the same thing, so why isn't the first method working?
Reading the AVAudioPlayer init methods' documentation reveals nothing related to this problem.
Are you streaming your audio file?
AVAudioPlayer cannot stream from URL. You can use it with files stored on device.
If you want to stream your file from URL you need to use AVPlayer
The following code:
AVPlayerItem *item = [AVPlayerItem playerItemWithURL: audioURL];
AVPlayer *myAVPlayer = [AVPlayer playerWithPlayerItem: item];
EDIT
When you are going to use your AVAudioPlayer with URL you need to present your URL like this:
NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:#"myMusic" ofType:#"mp3"];
NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
Taken from here
Hope it will help

iOS: How to save a video in your directory and play it afterwards?

I am working in an iOS project. I want may application to download a video from the internet programmatically. Then I want to play it. I know how can I play a local video from the Resources, but my question is how could I download it , and the find it to be played.
I am using MPMoviePlayerController to run the video.
Thanking in advance
I found the answer
here I saved the video
NSString *stringURL = #"http://videoURL";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
NSString *documentsDirectory ;
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"videoName.mp4"];
[urlData writeToFile:filePath atomically:YES];
}
and this code is for playing the video form its directory
NSString *filepath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"videoName.mp4"];
//video URL
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController play];
If you have a valid url of the video, Apple provides an API to directly buffer videos with NSURL.
You should hold a reference to the MPMoviePlayerController object from the controller so that ARC doesn't release the object.
#property (nonatomic,strong) MPMoviePlayerController* mc;
Make the URL
NSURL *url = [NSURL URLWithString:#"http://www.example.com/video.mp4"];
Init MPMoviePlayerController with that URL
MPMoviePlayerController *controller = [[MPMoviePlayerController alloc] initWithContentURL:url];
Resize the controller, add it to your view, play it and enjoy the video.
self.mc = controller; // so that ARC doesn't release the controller
controller.view.frame = self.view.bounds;
[self.view addSubview:controller.view];
[controller play]; //Start playing
For more detail you can visit this playing video from a url in ios7

Playing audio using url is not working on iOS7

In my application, I have an audio url. I want to play the audio using that url on my device. It does not seem to be working.
Here is the code I am using.
- (IBAction)play:(id)sender {
NSURL *url = [NSURL URLWithString:#"http://jesusredeems.in/media/Media Advt/mp3_player/Songs/viduthalaiyin geethangal_vol1/93.Ellame Koodum.mp3"];
NSData *data = [NSData dataWithContentsOfURL:url];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
[audioPlayer play];
}
I have used the above code. It's not working. Please tell me where I'm going wrong in the above code.
You need to add percentage escape in your query string, also you need to declare the AVAudioPlayer as a global variable.
So write the property in your class extension:
#property (nonatomic, strong) AVAudioPlayer *audioPlayer;
And modify your method like:
- (IBAction)play:(id)sender
{
NSString *urlstr = #"http://jesusredeems.in/media/Media Advt/mp3_player/Songs/viduthalaiyin geethangal_vol1/93.Ellame Koodum.mp3";
urlstr = [urlstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlstr];
NSData *data = [NSData dataWithContentsOfURL:url];
_audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:nil];
[_audioPlayer play];
}
You need to add percentage encoding %20 of space in URL
NSString *strURl=#"http://jesusredeems.in/media/Media Advt/mp3_player/Songs/viduthalaiyin geethangal_vol1/93.Ellame Koodum.mp3";
NSURL *url = [NSURL URLWithString:[strURl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if still not working use delegate methods
audioPlayer = self;
USe delegate methods
-(void)audioPlayerDecodeErrorDidOccur:
(AVAudioPlayer *)player
error:(NSError *)error
{
NSLog(#"%#",error);
}

how to stream mp3 file in iphone?

I'm streaming my mp3 file but it is starting very very late
after launch the application i have to wait for 1 or 2 mints then it plays the music
why it is so ?
can anybody have an idea ?
Please Do Tell me how can i play direct track without waiting too long
here what i m doing with my code in viewDidLoad method
-(void)viewDidLoad
{
NSString *urlstr=#"http://media.hyderabadplus.com/naats/Aamir Liaquat Hussain - Panjtan Ko Salaam/Ramzan Assalam.MP3";
NSURL *url = [NSURL URLWithString:[urlstr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSData *songData=[NSData dataWithContentsOfURL:url];
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:songData error:nil];
self.audioPlayer.numberOfLoops=0;
self.audioPlayer.delegate=self;
[self.audioPlayer prepareToPlay];
[self.audioPlayer play];
}

load video from File Path

i am trying to Load video from Path in my file system Project , i have code for the URL stream
NSString *path = [[NSBundle mainBundle] pathForResource:#"US_Very_High_Dive_Boudia_US_44_x264" ofType:#"mp4"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
You don't. MPMoviePlayer knows best when to load data. Convert the file path to a NSURL, or even better, just use
NSURL *myURL =`[[NSBundle mainBundle] URLForResource:#"US_Very_High_Dive_Boudia_US_44_x264" withExtension:#"mp4"];`
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: myURL];

Resources