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?
Related
I have retrieved movies file from my iPhone Photo Library, and saved them into my App`s Documents directory. When I click one movie file, I want to use MPMoviePlayerController to play it. Unfortunately, It fail to play the movie file with such error:
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
I have searched for a long time to resolve this problem. Somebody say the format maybe is unsupport. The movies file is retrieved from Photo Library, so the format should be OK. Also, I copy the file to NSBundle and MPMoviePlayerController can play it. So the file is OK. Following is my codes:
NSString *newstr = [mFilePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
DLog(#"the raw is %# the newstr is %#", mFilePath, newstr);
NSURL *url = [NSURL URLWithString:newstr];
DLog(#"the url is %#", url);
#if 0
NSBundle *bundle = [NSBundle mainBundle];
if (bundle) {
NSString *path = [bundle pathForResource:#"test" ofType:#"MOV"];
if (path) {
url = [NSURL fileURLWithPath:path];
DLog(#"the new url is %#", url);
}
}
#endif
mPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[mPlayer setMovieSourceType:MPMovieSourceTypeFile];
[mPlayer prepareToPlay];
[mPlayer.view setFrame:self.view.bounds];
[mPlayer play];
[self.view addSubview:mPlayer.view];
Changing:
NSURL *url = [NSURL URLWithString:path];
to
NSURL *url = [NSURL fileURLWithPath:path];
fixed it for me, then:
mPlayer.contentURL = url;
[mPlayer prepareToPlay];
but you already do this correctly. I create the mPlayer once and then just shift the contentURL about (instead of alloc + initWithContentURL) for different videos. Have you tried that?
I'm trying to add a video in xcode but it crashes: [NSURL initFileURLWithPath:]: nil string
(IBAction)Play:(id)sender {
/////
NSString *videoUrl=[[NSBundle mainBundle]pathForResource:#"tnween" ofType:#"mov"];
MPMoviePlayerController *player=[[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:videoUrl]];
[player play];
}
Maybe if you try:
NSString *videoUrl=[[NSBundle mainBundle]pathForResource:#"tnween" ofType:#"mov"];
NSURL *url = [NSURL urlWithString:videoUrl];
MPMoviePlayerController *player=[[MPMoviePlayerController alloc]initWithContentURL:url];
I am new to iOS and trying to add sound to my app. I have followed several tutorials about adding a simple sound file to my project, but ALL with the same result. I get no errors, but no sound plays. Any ideas on what I am doing wrong? Any help would be appreciated!
- (IBAction)playSound:(id)sender {
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSLog(soundFilePath);
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
player.numberOfLoops = 1;
[player play];
}
**UPDATE***
So I never had any luck with the above code which is very discouraging, but I did end up getting it to work using the AudioToolbox Framework instead, using this:
SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle]
pathForResource:#"test" ofType:#"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)
[NSURL fileURLWithPath:soundFile], & soundID);
AudioServicesPlaySystemSound(soundID);
Can anyone explain to me the difference, why this one works and the other does not?? I am curious more than anything. Would love some clarification.
Finally figured it out. I needed to create a property for AVAudioPlayer in my header file in order to get it to work. Here is what I did:
// .h file
#interface ThirdViewController : UIViewController {
AVAudioPlayer *audioPlayer;
}
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
- (void) playSound //method in .m file
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"test"
ofType:#"mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[audioPlayer play];
}
Simplify your code by doing the follow. Also drop your test.mp3 to the root of your project directory in xcode. Your mp3 placement and location is very important. Also ensure volume is up, and vibrate is off of your device, and it's unmuted.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"test"
ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
[audioPlayer play];
Make sure you install quartz.h, or a specific framework that supports audio players. Just double check.
Had the same problem a while back and the framework must be installed properly.
I have two local .html files in the Resources folder. I'm trying to load them the following way, but only the final page loads. What am I doing wrong?
File = please_wait.html
This one does not work.
NSError *error;
NSString* path = [[NSBundle mainBundle] pathForResource:#"please_wait" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
//Big "do-while" loop here. It works fine so I omitted it.
File = update_graph.html
This one does not work
path = [[NSBundle mainBundle] pathForResource:#"update_graph" ofType:#"html"];
htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
//Lots of code removed. All works correctly and doesn't touch webview
This last one works perfectly. Google displays.
string = #"http://google.com";
NSURL *url = [NSURL URLWithString: string];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
It appears from your comment that your UIWebView loads just fine, but it does not get a chance to refresh itself on the screen until you exit your method. It is not enough to set a break point inside the method and wait for the view to load: you must exit the method before iOS realizes that it needs to call UIWebView's drawRect method.
To fix this, split your method in three parts, A B and C, and set UIWebView's delegate in A to invoke B on webViewDidFinishLoad:, and the delegate in B to call C.
Here is how to implement this: start with a delegate that can call a selector when the loading has completed:
#interface GoToNext : NSObject <UIWebViewDelegate> {
id __weak target;
SEL next;
}
-(id)initWithTarget:(id)target andNext:(SEL)next;
-(void)webViewDidFinishLoad:(UIWebView *)webView;
#end
#implementation GoNext
-(id)initWithTarget:(id)_target andNext:(SEL)_next {
self = [super init];
if (self) {
target = _target;
next = _next;
}
return self;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[target performSelector:next];
#pragma clang diagnostic pop
}
#end
Now split your method into three parts - loading the first page, loading the second page, and loading the third page:
-(void)loadPleaseWait {
NSError *error;
NSString* path = [[NSBundle mainBundle] pathForResource:#"please_wait" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
webView.delegate = [[GoToNext alloc] initWithTarget:self andNext:#selector(loadUpdateGraph)];
[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
// big do-while loop
}
-(void)loadUpdateGraph {
NSError *error;
NSString* path = [[NSBundle mainBundle] pathForResource:#"update_graph" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
webView.delegate = [[GoToNext alloc] initWithTarget:self andNext:#selector(loadGoogle)];
[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
// Lots of code removed
}
-(void)loadGoogle {
string = #"http://google.com";
NSURL *url = [NSURL URLWithString: string];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
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];