I have a problem i have been trying to solve for a day and i tired of beating my head aaginst the wall. Anyone know how to do this?
Number 1 Works Great
Works Great
- (void) viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://techslides.com/demos/sample-videos/small.mp4"];
AVPlayer *avPlayer = [[AVPlayer alloc] initWithURL:url];
self.player = avPlayer;
[self.player play];
}
Does not work
- (void) viewDidLoad {
[super viewDidLoad];
CKAsset *asset = record[#"VideoFile"];
AVPlayer *avPlayer = [[AVPlayer alloc] initWithURL:asset.fileURL];
self.player = avPlayer;
[self.player play];
}
Does not work
- (void) viewDidLoad {
[super viewDidLoad];
CKAsset *asset = record[#"VideoFile"];
NSString *fileName = record[#"videoFileKey"];
NSData *data = [NSData dataWithContentsOfURL:asset.fileURL];
NSString *cachesDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [cachesDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.mp4", fileName]];
[data writeToFile:path atomically:YES];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSURL *url = [NSURL URLWithString:path];
AVPlayer *avPlayer = [[AVPlayer alloc] initWithURL:url];
self.player = avPlayer;
[self.player play];
}
}
Related
I'm using this code the load the local sqlite database.
- (id)init
{
self = [super init];
if (self) {
NSString *path = [[NSBundle mainBundle] pathForResource:#"db" ofType:#"sqlite3"];
_db = [[MDDatabase alloc] initWithPath:path];
_HTMLRenderer = [[MDHTMLRenderer alloc] init];
}
return self;
}
I would like to put the database online and let the app download the database instead. I changed the code to:
NSData *dbFile = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.someurl.com/DatabaseName.sqlite"]];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"Database.sqlite"];
[dbFile writeToFile:filePath atomically:YES];
_db = [[MDDatabase alloc] initWithPath:filePath];
_HTMLRenderer = [[MDHTMLRenderer alloc] init];
Editted:
I changed my code to follow but it's crashed.
- (id)init
{
self = [super init];
if (self) {
[self performSelectorOnMainThread:#selector(downalod) withObject:nil waitUntilDone:YES];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"Database.sqlite"];
_db = [[MDDatabase alloc] initWithPath:filePath];
_HTMLRenderer = [[MDHTMLRenderer alloc] init];
}
return self;
}
-(void)download
{
NSData *dbFile = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.someurl.com/DatabaseName.sqlite"]];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"Database.sqlite"];
[dbFile writeToFile:filePath atomically:YES];
}
You have problem in your file. Check with correct url. Its an encoding issue. Try with this (It will work) : http://spaceflight.nasa.gov/gallery/images/apollo/apollo17/hires/s72-55482.jpg
Add App Transport Security Settings (Allow Arbitrary Loads YES) in your plist.
Enable background mode.
Declare this macro in .m
#define DocumentsDirectory [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
Now call and you will have the file in your local.
Call like:
[self performSelectorOnMainThread:#selector(downalod) withObject:nil waitUntilDone:NO];
_db = [[MDDatabase alloc] initWithPath:filePath];
_HTMLRenderer = [[MDHTMLRenderer alloc] init];
-(void)download
{
NSString *stringURL =[NSString stringWithFormat: #"url"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#",[stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSString *filePath = [DocumentsDirectory stringByAppendingPathComponent:[url lastPathComponent]];
NSLog(#"success--222,%#", filePath);
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSLog(#"success--222,%#", filePath);
if (error)
{
NSLog(#"success--not---Error,%#", [error localizedDescription]);
}
else
{
NSLog(#"success--yes... %#", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
[data writeToFile:filePath atomically:YES];
}
}];
}
After long time search, finally I solved it myself:
- (id)init
{
self = [super init];
if (self) {
NSData *fetchedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.someurl.com/db.sqlite3"]];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"db.sqlite3"];
[fetchedData writeToFile:filePath atomically:YES];
_db = [[MDDatabase alloc] initWithPath:filePath];
_HTMLRenderer = [[MDHTMLRenderer alloc] init];
}
return self;
}
I'm using AVPlayer to play videos. I couldn't play .mp4 videos.I'm getting this black screen. here is my code.
`NSString *filePath = [self.videoArray objectAtIndex:index];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:filePath ofType: #"mp4];
url = [[NSURL alloc] initFileURLWithPath: soundFilePath];
playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.videoGravity = AVLayerVideoGravityResize;
playerViewController.view.frame = self.view.bounds;
playerViewController.showsPlaybackControls = NO;
video=[AVPlayer playerWithURL:url];
playerViewController.player = _video;
playerViewController.view.userInteractionEnabled = false;
[video play];
[self.view addSubview:playerViewController.view];`
Try this code I think it would be working for you, because It seems your URL is not for bundle its from web.
// your filepath which is may be "http" type
NSString *filePath = [self.video_Array objectAtIndex:index];
// http to NSURL using string filepath
NSURL *url= [[NSURL alloc] initWithString:filePath];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.videoGravity = AVLayerVideoGravityResize;
playerViewController.view.frame = self.view.bounds;
playerViewController.showsPlaybackControls = NO;
video=[AVPlayer playerWithURL:url];
playerViewController.player = _video;
playerViewController.view.userInteractionEnabled = false;
[video play];
[self.view addSubview:playerViewController.view];
I'm looking for a solution to record an audio stream to a file. I can get audio to play but I'm struggling to figure out how to record/save to file what is playing.
A nudge in the right direction would be greatly appreciated.
Player code thus far:
#interface ViewControllerPlayer ()
#end
#implementation ViewControllerPlayer
#synthesize receivedStreamReferenceNumber;
- (void)convertData:(NSData *) data {
NSString *urlString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:urlString];
[self loadPlayer:url];
}
- (void) loadPlayer:(NSURL *) url {
audioPlayer = [AVPlayer playerWithURL:url];
[audioPlayer play];
}
- (void) start {
NSLog(#"%#", receivedStreamReferenceNumber);
NSString *urlHalf = #"http://getstreamurl.php?KeyRef=";
NSMutableString *mutableUrlString = [NSMutableString stringWithFormat:#"%#%#", urlHalf, receivedStreamReferenceNumber];
NSURL *url = [NSURL URLWithString: mutableUrlString];
NSData *data = [NSData dataWithContentsOfURL:url];
[self convertData:data];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self start];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
EDIT:
I've incorporated the following code... but i'm struggling to tie it all together. The following should create a file that the recorded stream audio is saved to. I suppose i've got to tell the AVRecorder to listen to the AVPlayer some how? Again -- help will be greatly appreciated:
- (void)viewDidLoad
{
[super viewDidLoad];
[stopButton setEnabled:YES];
[playButton setEnabled:YES];
// Set the audio file
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
#"xxx.mp3",
nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
}
I have a class that provides a Music Player, compiled under ARC. This is the init code:
- (id) init{
self = [super init];
runningVolumeNotification = FALSE;
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
systemVolume = musicPlayer.volume;
NSString *myExamplePath = [[NSBundle mainBundle] pathForResource:#"servo" ofType:#"mp3"];
AVAudioPlayer* p = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:myExamplePath] error:NULL];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
[p prepareToPlay];
[p stop];
return self;
}
Instruments reports a 100% leak on the line:
AVAudioPlayer* p = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:myExamplePath] error:NULL];
I can't figure out where the problem is!
- (void)viewDidLoad{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"VidName" ofType:#"mov"];
NSURL *url = [NSURL URLWithString:path];
AVPlayer *av = [[AVPlayer alloc] initWithURL:url];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:av];
[layer setFrame:self.view.frame];
[self.view.layer addSublayer:layer];
[av play];
NSLog(#"error: %#", av.error);
}
The error being logged is NULL.
instead of
NSURL *url = [NSURL URLWithString:path];
use following line.
NSURL *url=[[NSURL alloc]initFileURLWithPath:path];