No Visible interface for 'AVAudioPlayer declares the selector initWithUrl:' - ios

I am trying to play a button click sound,also that i should be able to manage click sound along with volume button.
What i have done is
Added AVFoundationFramework to project,
in .h file
#import <AVFoundation/AVAudioPlayer.h>
#import <AVFoundation/AVFoundation.h>
in .m file under button click method
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"sound1" ofType:#"mp3"];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithURL:soundUrl];
[player play];
but i get the above error.Any help would be appreciated.

It's "initWithContentsOfURL:" not "initWithURL:".
As in:
AVAudioPlayer *player = [[AVAudioPlayer alloc]
initWithContentsOfURL:soundUrl];

Related

AVPlayer doesn't play anything

I am doing something incredibly simple, just play an audio.
but it doesn't play anything. not on the simulator or device.
audio file is in bundle and preview can play the audio, I have also tried different audios nothing plays.
What could be the problem?, do I have to add delegate methods for avplayer?(if so that doesn't make sense to me)
I have also tried without avplayer item and different nsurl methods too...
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"JohnsPizza" ofType:#"aiff"];
//NSURL *assetURL = [[NSBundle mainBundle] URLForResource:#"JohnsPizza" withExtension:#"aiff"];
NSURL *url = [NSURL fileURLWithPath:path];
AVPlayer *player = [[AVPlayer alloc] init];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
player = [AVPlayer playerWithPlayerItem:playerItem];
[player play];
}
I have also tried but no luck
// SystemSoundID sound1;
// AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &sound1);
// AudioServicesPlaySystemSound(sound1);
// AudioServicesRemoveSystemSoundCompletion(sound1);
// AudioServicesDisposeSystemSoundID(sound1);
you gotta retain AVPlayer object as it may set to nil automatically
#property (strong, nonatomic) AVPlayer *player;

Sound wont play using avfoundation

Any reason why this will not be working?
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
AVAudioPlayer *showsound;
NSString *audiopath = [[NSBundle mainBundle] pathForResource:#"mouse1" ofType:#"wav"];
NSURL *audiourl = [NSURL fileURLWithPath:audiopath];
showsound = [[AVAudioPlayer alloc]initWithContentsOfURL:audiourl error:Nil];
[showsound play];
Yes, the reason is that your audio player instance, showsound, is created as a local variable so will be deallocated as soon as the method that code is in goes out of scope. Create a strong property for show sound, and it will work properly.

MPMoviePlayerController not playing local video

My video did not start to play in simple project
So I tried some examples to incorporate in the project.
First of all I added two frameworks MediaPlayer and MobileCoreService and imported .h file
#import <MediaPlayer/MediaPlayer.h>
#import <MobileCoreServices/MobileCoreServices.h>
Then added properties
#property (copy, nonatomic) NSURL *movieURL;
#property (strong, nonatomic) MPMoviePlayerController *player;
And last step was adding to viewDidLoad next code
_movieURL = [NSURL URLWithString:#"Movie.m4v"];
_player = [[MPMoviePlayerController alloc] initWithContentURL:_movieURL];
_player.view.frame = CGRectMake(0 , 0, 400, 300);
_player.controlStyle=MPMovieControlStyleNone;
_player.scalingMode = MPMovieScalingModeAspectFill;
[self.view addSubview:_player.view];
[_player play];
Movie.m4v file in my root folder of application.
Also I'm trying to start it with help of action button, but code is working with
[_player play];
but I get a black rectangle, and if I understand right my addSubview part is working
Searching another examples but almost find same realisation.
http://photokarma.bl.ee/ios/VideoTest.zip archive with src code and file
Try This code..
NSString *path = [[NSBundle mainBundle] pathForResource:#"Movie" ofType:#"m4v" inDirectory:nil];
NSURL *movieURL = [NSURL fileURLWithPath:path];
player= [[ MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
player.view.frame = CGRectMake(184, 200, 400, 300);
[self.view addSubview:player.view];
[player play];
Source Code:http://ishare-edu.blogspot.in/2012/10/how-to-play-audio-and-video-in-iphone.html
Hope it works...
Please resolve this line in Your code.
_movieURL = [NSURL URLWithString:#"Movie.m4v"];
if this is an http url then write complete like http://www.yourserver.com/../../Movie.m4v
or you accessing this video from locally within your iOS App then provide the full path like
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Movie" ofType:#"m4v"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
then after finding the movie url you can init your player like
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
player.view.frame = CGRectMake(184, 200, 400, 300);
[self.view addSubview:player.view];
[player play];
As per your Comment do you have any idea why i get urlpath nil?
Check that:
The Movie file exists in your copy of the source code.Also review that Xcode project has a reference to that file (if it's red, it means the file isn't actually present)
In your target in Xcode, look at "Build Phases" and reveal the "Copy Bundle Resources" build phase. That file should be present. If it isn't, press the + button and select it.
As per your given code i resolved and found this bug as i also discussed.
Tick both
Add To Targets Your Project Name
Copy item into your destination group
So you need to remove the video file and add it again with these both tick.
Try in Simple steps:
Step 1: Import MediaPlayer framework #import <MediaPlayer/MediaPlayer.h>
Step 2: Set delegate in .h file UIViewController<MPMediaPlayback>
Step 3:
- (void)viewDidLoad
{
[super viewDidLoad];
NSBundle *bundle = [NSBundle mainBundle];
NSString *fileLocation= [bundle pathForResource:#"file_video" ofType:#"m4v"];
NSURL *vedioURL =[NSURL fileURLWithPath:fileLocation];
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL];
[self presentMoviePlayerViewControllerAnimated:videoPlayerView];
[videoPlayerView.moviePlayer play];
}
-(void)endSeeking
{
[self.navigationController popViewControllerAnimated:NO];
}
-(void)stop
{
[self.navigationController popViewControllerAnimated:NO];
}

Cannot play audio in Xcode project

This code works in a test app, but not in the app in which I want the audio to play.
-(void) playTutorialAudio
{
_theAudioFile = [[NSBundle mainBundle] pathForResource:#"Welcome" ofType:#"mp3"];
NSLog(#"playTutorialAudio _theAudioFile %#",_theAudioFile);
NSURL *playTutorialURL = [NSURL fileURLWithPath:_theAudioFile ];
_audioPlayAlert = [[AVAudioPlayer alloc] initWithContentsOfURL:playTutorialURL error:NULL];
[_audioPlayAlert setDelegate:self];
[_audioPlayAlert prepareToPlay];
_audioPlayAlert.volume = 1.0;
[_audioPlayAlert play];
}
The code works with other audio files.
It will work, You need to change your implementation like this. Please notice your init method.
#property (nonatomic, retain) AVAudioPlayer *cellTapSound;
may be in viewDidLoad
cellTapSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:#"ting" withExtension:#"wav"] error:nil];
[cellTapSound prepareToPlay];
Now may be on click of some button, you can give a shot like this :
if (cellTapSound.isPlaying) [cellTapSound setCurrentTime:0.0];
[cellTapSound play];
hope that helps.

AVAudioPlayer: Simple sound not playing

I'm trying to figure out why sounds aren't playing in my app, so I created what I think is as simple an implementation as possible:
NSError *error;
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"canary-trills" ofType:#"wav"];
NSLog(#"string=%#", soundFilePath);
NSURL *url = [[NSURL alloc] initFileURLWithPath:soundFilePath];
NSLog(#"URL=%#", url);
AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url error:&error];
[avPlayer prepareToPlay];
BOOL success = [avPlayer play];
NSLog(#"The sound %# play", success ? #"did" : #"didn't");
Per the console, it looks like it finds the resource, creates the URL correctly. Even the play call indicates success. However, no sound play in the simulator nor the device.
AVAudioPlayer wasn't being retained & was going out of scope.
Adding:
#property (strong, nonatomic) AVAudioPlayer *audioPlayer;
to the class & using that member solved the issue.

Resources