GPUImage and AVAssetWriter writes files to where? - ios

I am trying to use Brad Larson's GPUImage library to record a video file. So far, the issue I am running into is the following piece of code:
NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Movie.m4v"];
unlink([pathToMovie UTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie
NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];
movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(480.0, 640.0)];
This is picked up directly from his example from his GitHub page
https://github.com/BradLarson/GPUImage
Now, I looked at the source code and I see he does the usual AVFoundation stuff - creates AVAssetWriter instance and adds inputs to it. MY question here is that where is #"Documents/Movie.m4v" located? How can I play this video back? Or how do I even access it? I know it is located in an arbitrary location where the app bundle is copied during run-time. My goal is to be able to write the video into the Gallery or atleast an album in Gallery that is created by my app. Does anyone know where the file is being written to?

Maybe I'm misunderstanding, but the movie is at the path pathToMovie (or URL movieURL.
You can play it back with something like this:
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
// later
AVPlayerViewController *avc = [[AVPlayerViewController alloc] init];
avc.player = [[AVPlayer alloc] initWithURL:movieURL];
[myViewController presentViewController:avc animated:YES completion:nil];
If you want to download the file to your computer, you can select your connect device in Xcode > Devices, then select your app and download the package. Your movie will be in the Documents directory.
To save it to the gallery, do
// TODO: use completion arguments for a better user experience
UISaveVideoAtPathToSavedPhotosAlbum(pathToMovie,nil,nil,nil);

Related

Can I upload an MP3 file to my server from iOS?

I was googling this question but nothing useful or current came up. I'd like to know if (and if, how) you can select an MP3 file (from itunes?) and upload the contents to one of my own servers on iOS (iphone & ipad app).
One of my clients is asking me if it's possible to do this, and I havn't found the answer yet.
Thanks in advance!
The short answer would be YES.
Here is a working solution for me. But you need to use a third party library. Then this is what you need to do:
Create a temp folder either in the NSDocuments directory or a temp directory.
Use MPMediaQuery to load the music files.
The object that you will get from the MPMediaQuery is an MPMediaItem. With this you can get the asset URL of the media item.
Code:
NSString *assetURL = [mediaItem valueForProperty:MPMediaItemPropertyAssetURL];
get the extension of with the asset URL
NSString *extension = [TSLibraryImport extensionForAssetURL:assetURL];
set a location URL (This will be the location where the mp3 music data will be imported).
NSString *locationURL = [[NSURL fileURLWithPath:[path stringByAppendingPathComponent:musicTitleYouWant]] URLByAppendingPathExtension:extension];
Now you can import the contents of the mp3 from to the directory you set earlier.
TSLibraryImport *libraryImport = [[TSLibraryImport alloc] init];
[libraryImport importAsset:assetURL toURL:locationURL completionBlock:^(TSLibraryImport *libraryImport)
{
if(libraryImport.status == AVAssetExportSessionStatusCompleted)
{
//Once complete the file will be store on the location URL you set earlier. Now you can get the file from that location and convert it to NSData and send it to the server. There are plenty of ways to do that.
}
else
{
//Here means import failed. :(
}
}];
Hope this helps. :)

Local saved videos don't play again after app closes

This is strange, but basically I download and save a video locally, and the store the url path to provide to an AVPlayer to play.
This works fine the first time I do it. I download a file, and then I can play it to my hearts content as many times UNTIL I exit the app. When I launch the app a second time, I now get a black screen when I try to play the same exact video using the same exact path.
Because I am using the Simulator I can verify that the videos and pictures indeed very much still exist in the same folder I saved them to, and I can still play them if I click on them from the Finder.
Maybe it's a caching issue? If it matters, I've saved them straight to the Library directory as I test this.
Relevant Code:
NSString *outputFile = [NSString stringWithFormat:#"video_%#.mp4", guid];
NSString *outputDirectory = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *tempPath = [outputDirectory stringByAppendingPathComponent:outputFile];
NSURL *fileURL = [NSURL fileURLWithPath:tempPath];
// save the video to the URL
Then I "persist" it using an NSString [fileURL path] (The way I've built this out, assume the solution requires an NSString to NSURL conversion).
Later I create an AVPlayerItem:
NSURL *url = [NSURL fileURLWithPath:persistedObject.contentURL];
NSLog(#"url: %#", url); // prints a valid location**
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
** for example this is a sample url location
url: file:///Users/gabriel/Library/Developer/CoreSimulator/Devices/CE1FC933-808C-4003-9BE4-DEC59B787FF7/data/Containers/Data/Application/FAD072B4-B5B0-4487-8A76-57B047324A00/Library/picture_D8DEAFA5-0843-4AA3-BB32-C61E32D13579.mp4
It's been suggested I use URLForDirectory:inDomain:appropriateForURL:create:error: and URLByAppendingPathComponent: instead, which I will look into. But still confused as to why it would play when I first download it, but not after app exits when it's the same exact file.
You've made a classic mistake. You are persisting the full path. But the full path changes. Never persist a full path. Only persist the part of the path relative to the value obtained from NSSearchPathForDirectoriesInDomains.
Given what you are doing, you should only persist the base filename (outputFile). Then when the app starts, you rebuild the full path again like you did originally but use the persisted filename to append it to the dynamically obtained path to the application support folder.

How to stream mp3 files (iOS)

I have found many related questions on SO, but or because I am very new, or because it doesn't exactly fit my problem, I can't find a way to succeed.
So, all I would like to do is playing an audio file stored on an exernal server directly on the iOS device (iPhone in my case)
I have tried differents solution on the net, and this is what I get until now.
NSString* resourcePath = #"http://www.chiptape.com/chiptape/sounds/medium/laugh.mp3"; //your url
NSURL *url = [[NSURL alloc]initWithString:resourcePath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithURL:url error:&error];
audioPlayer.delegate = self;
[url release];
[audioPlayer prepareToPlay];
[audioPlayer play];
The compiler show me this error:
No visible #interface for 'AVAudioPlayer' declares the selector
'initWithURL:error:'
I am not sure at all about my code, and would like your guys to know I have started iOS coding yesterday, so it's not impossible I'm missing something so obvious that tutorials/posts don't mention it.
Also, I have imported the library AVFoundation and imported it on my .h file.
I have also declared this on my .h file
AVAudioPlayer *audioPlayer;
I hope there is an easy way to stream audio file on iOS as provide android/java
Thank you and if you need more details comment and I'll update my post.
Try this:
NSString* resourcePath = #"http://www.chiptape.com/chiptape/sounds/medium/laugh.mp3"; //your url
NSURL *audioURL = [NSURL URLWithString:resourcePath];
AVAudioPlayer *audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL: audioURL error:&error]autorelease];
[audioPlayer play];
I'm working with it recently.
AVAudioPlayer can't work with audio stream. In fact, there isn't an easy way to do that.
You need to learn some Core Audio things, get the raw data of media, play them in audioQueue (audioToolBox framework). Real-time audio processing is not a simple work, it's full of c-level api and so many dirty work, don't rush, have fun :)
Check out the book Learning Core Audio and this open-source audio streamer.
Let me know if you need more help.

MPMoviePlayerController stills play after delete mp3 source file?

I'm writing a app to play media file.I use MPMoviePlayerController.
My source:
NSURL * mp3URL = [NSURL fileURLWithPath:mp3Path];
MPMoviePlayerController *mpv = [[MPMoviePlayerController alloc] init];
[mpv setContentURL:mp3URL];
But when i delete mp3Path,mpv stills play.
In this case,MPMoviePlayer creates a tmp file,load data from mp3Path to tmp path and play this tmp file?
Can anyone explain to me?Thanks so much.
yes it creates a tmp file, thats why you have the notifications for didloadfinish and didpreloadfinish if the file did load completely it is gonna play the whole song...

iPhone SDK: Play a single WAV from a button

I am currently trying out this code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"dream" ofType:#"m4a"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
However, the SDK says that the ViewController does not implement the AVAudioPlayer Delegate.
Any body any ideas on how to play a WAV (or M4a) using the 2.2 SDK?
You need to add <AVAudioPlayerDelegate> to the end of your controller's class declaration, along these lines:
#interface MyViewController : UIViewController <AVAudioPlayerDelegate>
This'll probably generate some more warnings about delegate methods you need to implement - follow those and you'll be good to go.
Interesting. Try NSURL -URLWithString: - that's what I use in combination with the bundle method you're using. I don't see anything that could be causing a problem other than that. You're testing it on the device, right?
The answer I posted here should definitely help you. Just use audioservices for small wave sounds, unless you need concurrency, it's faster.
iPhoneToot.com has its own itootSound class and a VIDEO to show you how to play a wav file in 2 lines of code. Check them out at iPhoneToot.com. it will save you hours if not days of time!!

Resources