I would like to know if it's possible to play a video from an NSData object... with the MPMoviePlayerController.
Ben's answer works perfectly on simulator but wont work on the device, You cannot write anywhere on the device. Check code below
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"myMove.mp4"];
[videoData writeToFile:path atomically:YES];
NSURL *moveUrl = [NSURL fileURLWithPath:path];
player = [[MPMoviePlayerController alloc]init];
[player setContentURL:moveUrl];
player.view.frame = viewPlayer.bounds;
[viewPlayer addSubview:player.view];
[player play];
As far as I know this is not possible. If the data comes from your DB, can you save it into a temporary file and play that?
It is better to use NSFileManager in this case instead writeToFile
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"myMove.mp4"];
[[NSFileManager defaultManager] createFileAtPath:path contents:videoData attributes:nil];
NSURL *moveUrl = [NSURL fileURLWithPath:path];
player = [[MPMoviePlayerController alloc]init];
[player setContentURL:moveUrl];
player.view.frame = viewPlayer.bounds;
[viewPlayer addSubview:player.view];
[player play];
create a file with the type of your NSData for example if your NSData is type of mp4 create a file with that type - for example - "myMove.mp4"
copy past the file to your app resurces
add this code
NSData *mediaData; //your data
NSString *movePath=[[NSBundle mainBundle] pathForResource:#"myMove" ofType:#"mp4"];
[mediaData writeToFile:movePath atomically:YES];
NSURL *moveUrl= [NSURL fileURLWithPath:movePath];
MPMoviePlayerController *movePlayer=[[MPMoviePlayerController alloc]init];
[movePlayer setContentURL:moveUrl];
[movePlayer play];
Related
Should I store the multiple image files as NSData to SQLite or
save physical files to Document Folder??
Thanks
Store the image in the application directory. This is best and easy way to handle. Try the following code. it will be help you.
/Store Image/Songs files to Application Directory
+(BOOL)writeToFile:(NSData *)data fileName:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
return [data writeToFile:appFile atomically:YES];
}
//Image - Retrieve from Application Directory
+(NSData *)readFromFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
if (myData) {
return myData;
}
return nil;
}
I am trying to play a music file with eternal loop.
My code is:
NSString *path = [[NSBundle mainBundle] pathForResource:#"theme" ofType:#"mp3" inDirectory:#"assets/audios" ];
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
audioPlayer.volume = 1.0;
if (audioPlayer == nil){
NSLog(#"Error creating player: %#", error);
}
[audioPlayer prepareToPlay];
[audioPlayer play];
But it does nothing. Nothing is played and any message log is printed.
The code below works great:
NSString *path = [[NSBundle mainBundle] pathForResource:#"theme" ofType:#"mp3" inDirectory:#"assets/audios" ];
NSURL *url = [NSURL fileURLWithPath:path];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)CFBridgingRetain(url), &soundID);
AudioServicesPlaySystemSound (soundID);
But I need to control the volume and the loop.
I dont know what Im doing wrong.
Can anyone help me?
i'm downloading my pdf file in document directory and then trying to show that pdf file
from that path to load in uiwebview but its not opening just a blank white page, i dont
understand why its not loading in uiwebview.
below is my code ,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathExtension:#".pdf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
and my filepath is:
NSLog(#"Filepath------->> %#", filePath);
/Users/zee/Library/Application Support/iPhone Simulator/6.0/Applications/BD79829B-95FF-4B91-94AD-830C6B1CD31D/Documents/myPDF.pdf
Please tell me why its not working and what should be the best solution
Thanks
Zeeshan Shaikh
Try this,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathExtension:#"sample.pdf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSString *urlString = [url absoluteString];
NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *webURL = [NSURL URLWithString:encodedString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
Im trying to save and load an image picked from camera roll within my app. I get an error when I press either the load or save button
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView CGImage]: unrecognized selector sent to instance 0xe637640'
any thoughts?
AppDelagate .m
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory i nDomains:NSUserDomainMask] lastObject];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
.m of class im trying to save/load image
- (IBAction)save:(id)sender {
UIImage *myImage = imageView;
NSData *data = UIImagePNGRepresentation(myImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
}
- (IBAction)load:(id)sender {
UIImage *myImage = imageView;
NSData *data = UIImagePNGRepresentation(myImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
UIImage* thumImage = [UIImage imageWithContentsOfFile: [NSString stringWithFormat:#"%#/%#.png", appDocsDirectory, #"myNewFile"]];
}
this error means that message CGImage is sent to object of type UIImageView, but UIImageView does not response to it. The problem is seemed to be in
UIImage *myImage = imageView;
You do not write properly implementation of imageView, so I can not say exactly, but I assume that imageView is UIImageView, which is subclass of UIView, and you are assigning it to UIImage. Try
UIImage *myImage = [imageView image];
instead. I think that should works))
by the way, why do you need first and second strings of code in -load? They are unnecessary:
- (IBAction)load:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
UIImage* thumImage = [UIImage imageWithContentsOfFile: [NSString stringWithFormat:#"%#/%#.png", appDocsDirectory, #"myNewFile"]];
}
I'm making an app where I need to copy certain file from the app bundle to the user's Application Support folder. This is the code I'm using actually:
NSBundle *thisBundle = [NSBundle mainBundle];
NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleExecutable"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *userpath = [paths objectAtIndex:0];
userpath = [userpath stringByAppendingPathComponent:executableName]; // The file will go in this directory
NSString *saveFilePath = [userpath stringByAppendingPathComponent:#"db.sqlite"];
NSFileManager *fileManager = [[NSFileManager alloc] init];
[fileManager copyItemAtPath:[thisBundle pathForResource:#"db" ofType:#"sqlite"] toPath:saveFilePath error:NULL];
But this doesn't copies anything, BTW this is a Mac app.
Finally got it working with adding
if ([fileManager fileExistsAtPath:userpath] == NO)
[fileManager createDirectoryAtPath:userpath withIntermediateDirectories:YES attributes:nil error:nil];
And the resulting code is:
NSBundle *thisBundle = [NSBundle mainBundle];
NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleExecutable"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *userpath = [paths objectAtIndex:0];
userpath = [userpath stringByAppendingPathComponent:executableName]; // The file will go in this directory
NSString *saveFilePath = [userpath stringByAppendingPathComponent:#"db.sqlite"];
NSFileManager *fileManager = [[NSFileManager alloc] init];
if ([fileManager fileExistsAtPath:userpath] == NO)
[fileManager createDirectoryAtPath:userpath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager copyItemAtPath:[thisBundle pathForResource:#"db" ofType:#"sqlite"] toPath:saveFilePath error:NULL];
You might want to double-check your file paths:
NSLog(#"src: %#", [thisBundle pathForResource:#"db" ofType:#"sqlite"]);
NSLog(#"dst: %#", saveFilePath);