MPMoviePlayerViewController not playing a downloaded file - iphone

I've downloaded a file from a webserver, and saved it in my Documents Directory.
I try and pass the url of the file to MPMoviePlayerViewController using
initWithContentURL:url];
Where the url is created with
[NSURL URLWithString:#"/var/mobile/Applications/7A21A941-A54C-4116-857D-A34EDEE2F2E8/Documents/lesson.m4v"];
However whenever I try to play the video, the MoviePlayer appears for a second, with "loading" then dismisses itself.
Am I doing something wrong with the local file URL?
(The video plays fine when streamed from the webserver)
Thanks

I don't know why it is dismissing itself, but you actually are using web url instead of fileUrl. You need to initiate it like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"lesson.m4v"];
NSURL *url = [NSURL fileURLWithPath:path];
and pass this url to your MPMovieController

For local files must use:
[NSURL fileURLWithPath: somethingPath];
For server files can use:
[NSURL URLWithString: somethingPath];

Related

iPhone app with local HTML file doesn't support link

I want to create simple iPhone app with local HTML file.
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[waWebView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:#""]];
I made one webView and make it show index.html file in same directory with .m file.
Then make an another HTML file; index2.html, in same directory
I want to see index2.html through a link in index.html
So, in index.html, I put
View html 2
index.html is successfully shown, but above link doesn't work.
What do I need more? Help meT.T
In additional to this problem, this app doesn't support javascript code too...
the link is relativ.. you need to set the baseURL of your webView to the path containing the index html file
NSString *file = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *base = [NSURL fileURLWithPath:file.stringByRemovingLastPathComponent];
[webview loadHTMLString:text baseURL:base];
It is done by creating a NSURL using its fileURLWithPath and then a NSURLRequest and loading that rather loading the html itself:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[waWebView loadRequest:request];
You may need to add javascript to it for it to work. If you want to make a simple iPhone app just use app mobi(http://www.appmobi.com/) and they let you put css html and js files into a builder and it does the magic for you. Try that.
Hope I Helped
LucaSpeedStack

How to save audio data recorded using AVAudioRecorder for further use?

Hi i am creating application which can record sound. I can have that recorded sound file and i can play it immediately. I do not know how can I save it on iPhone so that I can play it after reopening application.
Is there any way by which this file will be stored as application save data?
Thanks in advance.
Your can do it like this to record it into your documents directory :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"audioFile.ext"]; // Where ext is the audio format extension you have recorded your sound
[yourAudioPlayer.data writeToFile:filePath atomically:YES];

How to add a ringtone from an application to ringtones of iphone?

I have created an application included a ringtone, but how can i add it to ringtones of iphone?
Use iTunes file sharing in your app and copy the ringtone file to the app's documents directory.
Set "Application supports iTunes file sharing" to YES in your info.plist
Wherever appropriate in your app copy out the file with the code below:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"MyRingtone" ofType:#"m4r"];
NSData *mainBundleFile = [NSData dataWithContentsOfFile:filePath];
[[NSFileManager defaultManager] createFileAtPath:[documentsDirectory stringByAppendingPathComponent:#"MyRingtone.m4r"]
contents:mainBundleFile
attributes:nil];
The user can now access the ringtone via itunes and add it to their device's ringtones.
You cannot. Apple doesn't release an API to export/write ringtones to the operating system.

Downloading music files to the iphone

I am trying to develop an application which downloads online music data (music files) and makes them available when I am offline.
Can anyone help me? What is the best way to do that?
I like ASIHTTPRequest framework for downloading. You may also use standard NSURLRequest, but as for me it's not as easy-to-use as ASIHTTPRequest.
Update:
To save file, you may use Documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [paths objectAtIndex:0];
To save loaded data into a file, use
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
To load:
+ (id)dataWithContentsOfFile:(NSString *)path

Read Text file Programmatically using Objective-C

I am new to iPhone Programming
I want to read the content of text file which is in my Resourse folder. i did a lot of googling but failed to get proper way for doing this task.
Please suggest
The files in your "Resource folder" are actually the contents of your application bundle. So first you need to get the path of the file within your application bundle.
NSString* path = [[NSBundle mainBundle] pathForResource:#"filename"
ofType:#"txt"];
Then loading the content into a NSString is even easier.
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
As a bonus you can have different localized versions of filename.txt and this code will fetch the file of the currently selected language correctly.
The first thing you want to do is get the path for the text file you want to read. Since it's in your Resources folder, I'm assuming you're copying it into the main bundle of the application. You can get the path for a file in your main bundle using NSBundle's pathForResource:ofType:
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:#"filename" ofType:#"txt"];
Then you can read the file at that path into an NSString directly using initWithContentsOfFile:usedEncoding:error:
NSStringEncoding encoding;
NSError *error;
NSString *fileContents = [[[NSString alloc] initWithContentsOfFile:filePath
usedEncoding:&encoding
error:&error]
autorelease];