xcode5 - playing sound after pushing button is not working - ios

OBJECTIVE :
I am trying to get a sound to play when a button is clicked. This sound will be preformed by an action called "play". The sound is in my resource folder and is called SoftClick.mp3
PROBLEM:
There is no sound at all being played in either simulator or the real device. The code does not give any errors.
CONNECTIONS:
The button, that needs to give sound, is connected to the action play and is used as a Modal Segue to show another view controller. Both viewcontrollers use the same .h and .m script.
CODE FOR .H
#import <UIKit/UIKit.h>
#import<AVFoundation/AVAudioPlayer.h>
#interface ViewController : UIViewController
{
AVAudioPlayer *audioPlayer;
}
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
- (IBAction) play;
#end
Action and audioplayer are defined as i thought was necessary (?)
THE CODE FOR .M
#import "ViewController.h"
#import<AVFoundation/AVAudioPlayer.h>
#interface ViewController ()
#end
#implementation ViewController
#synthesize audioPlayer;
-(IBAction)play
{
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"SoftClick"ofType:#"mp3"]] error:NULL];
[theAudio play];
}
Can someone please help me with correcting my coding so this could work in xcode5?
Ive spend some hours trying to figure out why this will not work an tried combinations like without segue etc. I do not want to steal the code from someone else and am trying to build this on my own.
Thanks in advanced,
Lien

The issue, as I'm assuming you're using ARC, is that you aren't retaining the AVAudioPlayer object. You create the object, call play, but then it is instantly released and dealloc'd. You need to add self.audioPlayer = theAudio;

Related

xcode 7.3.1 app on iPhone6+ won't play MP3 file anymore

Xcode app doesn't play MP3 music file on iphone6+ or simulator.
Just trying to play a simple MP3 file when app starts.
Was working. Tried previous working version, and it won't play.
MP3 filename is d.mp3
It plays if I double-click it in Xcode project.
Xcode top project groups:
ParticleSDK
...
myApp
d.mp3
all my .h and .c
Pods
I selected: TARGETS > myApp
Build Phases > Link Binary With Libraries:
AVFoundation
CoreGraphics
UIKit
Foundation
// .h:.............................
#import <UIKit/UIKit.h>
#interface SparkViewController : UIViewController
#end
// .m:........................................
#import "SparkViewController.h"
#import "Spark-SDK.h"
#import <UIKit/UIKit.h>
#import "AVFoundation/AVAudioPlayer.h"
#interface SparkViewController () <AVAudioPlayerDelegate>
{
AVAudioPlayer *AV_audio_player; // strong reference
}
#property (weak, nonatomic) IBOutlet UILabel *loggedInLabel;
#property (nonatomic, strong) id eventListenerID_A;
#property (nonatomic, strong) id eventListenerID_B;
#end
#implementation SparkViewController
- (void)viewDidLoad
{
[super viewDidLoad];
////////////////////////// P L A Y M P 3 F I L E ///////////////////////////
NSURL *url = [[NSBundle mainBundle] URLForResource:#"d" withExtension:#"mp3"];
printf("\n url = 0x%x \n", (int)url );
NSError* the_error;
AV_audio_player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error: &the_error];
NSLog(#" Error is: %#", the_error );
AV_audio_player.delegate = self;
[AV_audio_player play];
printf( "\n %s AV_audio_player playing.", __FUNCTION__ );
printf("\n AV_audio_player = 0x%x", (int)AV_audio_player );
}
OUTPUT LOG...................................
-[SparkViewController viewDidLoad] ************* v 2016-09-19 X1 ************
url = 0x34e5db80
2016-09-19 12:18:41.805 AudMan1 x1[385:60539] Error is: (null)
-[SparkViewController viewDidLoad] AV_audio_player playing.
AV_audio_player = 0x34e51790
All the evidence given in your question would seem to be that the audio player is playing, or at least is starting to play. Perhaps some other code then comes along and stop it, or sets AV_audio_player and thus kills the current audio player — but that would be other code which you have not shown, so it would be impossible for us to know about it. Only you can find it. When you do, the problem will be solved.
(Of course, you might also have turned on the Silent switch on your iPhone, or turned the volume all the way down. So do also look for a hardware reason why the sound might not be audible.)

When adding UIView instantiated with an .xib to the Storyboard buttons don't work and background color can't be changed

I am working on an iOS project and wanted to include a UIView that is reused on multiple screens in the application (appearing at the bottom of different UIViews). I am using a storyboard for the UI work so far but created an .xib file to be used by the reusable view (playerView in code below).
The view gets added but the button I have added to the View is unresponsive, also my background color cannot be changed on the view. I have tried to set background color programmatically and in the .xib with no luck. Very weird symptoms and I tried to instantiate the view from #5 of this article and probably did something wrong. I dont fully understand everything in the article which makes me nervous - for instance my showSubclassedView method returns IBAction but I just call the function name in code and dont use a button (I did hook up the buttons in the view as the article described though).
Here is the code:
EventViewController.m (where I trry and add PlayerView)
#import "EventViewController.h"
#import "PlayerView.h"
#interface EventViewController ()
-(IBAction)showSubclassedView;
#end
#implementation EventViewController
-(IBAction)showSubclassedView
{
[PlayerView presentInViewController:self];
}
PlayerView.h (.h for reusable view)
#import <UIKit/UIKit.h>
#import "Utils.h"
#class PlayerView;
#protocol PlayerViewDelegate
-(void)playerViewTouchedUp:(PlayerView*) playerView;
-(void)playerViewDidDismiss:(PlayerView*) playerView;
#end
#interface PlayerView : UIView
+(void)presentInViewController:(UIViewController<PlayerViewDelegate>*) playerView;
-(IBAction)viewTouchedUp;
-(IBAction)dismiss;
#end
PlayerView.m (.m for reusable view)
#import "PlayerView.h"
#interface PlayerViewOwner : NSObject
#property(nonatomic,weak) IBOutlet PlayerView *playerView;
#end
#implementation PlayerViewOwner
#end
#interface PlayerView ()
#property (nonatomic, weak) UIViewController <PlayerViewDelegate> *delegateViewController;
#end
#implementation PlayerView
+(void)presentInViewController:(UIViewController<PlayerViewDelegate> *)viewController
{
PlayerViewOwner *owner = [PlayerViewOwner new];
[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:owner options:nil];
owner.playerView.delegateViewController = viewController;
[viewController.view addSubview:owner.playerView];
}
-(IBAction)viewTouchedUp
{
//forward to delegate
NSLog(#"you clicked a button");
[self.delegateViewController playerViewTouchedUp:self];
}
-(IBAction)dismiss
{
[self removeFromSuperview];
// Forward to delegate
[self.delegateViewController playerViewDidDismiss:self];
}
#end
PlayerView.xib has a UIbutton on it that connects it to viewTouchedUp method in PLayerView.m
Is there anything I did wrong in the code above? Is this the best way to do a reusable view to display on other views?
Thank you!
Here's a workaround I found after not being able to solve this.
New implementation is from this article
I reverted my changes from my initial question and implemented this. It lacks the nice protocol separation for talking back and forth and I might need something like this later but I think now I can still implement a protocol to solve this.
At least with this solution I have usable items in my view and a background that changes!

Why can't I access my superclass methods?

I'm using LBYouTubePlayer to play videos in my app.
In the framework LBYouTubePlayer, it's subclassed as mpmovieplayercontroller, declared as
#interface LBYouTubePlayerViewController : MPMoviePlayerViewController
In my own view controller, I do
#property (nonatomic, strong) LBYouTubePlayerViewController* videoPlayerController;
and video plays successfully with
self.videoPlayerController = [[LBYouTubePlayerViewController alloc] initWithYouTubeID:stringID quality:LBYouTubeVideoQualitySmall];
self.videoPlayerController.delegate = self;
[self.viewForPlayer addSubview:self.videoPlayerController.view];
but when I try
[self.videoPlayerController setControlStyle: MPMovieControlStyleDefault ];
the method isn't recognized? even when I do self.videoPlayerController, autocomplete pops up but does not show any of the superclass' methods?
There is no in setControlStyle method in MPMoviePlayerViewController.

iOS: Control AudioPlayer of another View(controller)

I am programming a Quiz-App. In the Menu-ViewController I added Music to the project. The musicPlayer is running well and as long as the Menu-ViewController is in front, I can also controle it (play, pause, stop, ect.). When I display another ViewController, the Music runs in the Background, like I'd like to. But if try to call the play/pause Method of the first ViewController being in the secondViewController, the music nether paused nor stoped. I don't know why! If I add other Instructions to this Method, everything's going fine. (I tried the exit(0); Method. Here is my Code:
Controller 1 .h :
#implementation MenuViewController : <....> {
... }
#property (retain) AVAudioPlayer *backgroundPlayer;
- (void) playPauseMethod;
Controller 1 .m :
#interface ...
#end
#implementation MenuViewController
# synthesize
- (void) soundChanger {
if (hintergrundPlayer.isPlaying) {
[backgroundPlayer pause];}
else if (!backgroundPlayer.isPlaying) {
[backgroundPlayer play];}}
Controller 2 .h :
#import "MenuViewController.h"
#interface QuizViewController : UIViewController{}
Controller 2 .m :
#interface ...
#end
#implementation MenuViewController
# synthesize ...
//..... musicPlayer is playing music.
- (IBAction)myMusic:(id)sender {
//first try:
[[[MenuViewController alloc] init].backgroundPlayer pause];
//second try:
[[[MenuViewController alloc] init] soundChanger];}
I'd like to control the music in every ViewController. I'm looking forward to your help.
You're creating a completely new MenuViewController in Controller2 with
[[MenuViewController alloc] init]
The best way to handle it would be to set up a protocol in controller 2 with something like
#protocol <Controller2Delegate>
-(void) playButtonPressed:(id)self;
#end
Then set up a delegate property (still in controller 2) like this:
#property (weak) id <Controller2Delegate> delegate;
Then, back in Controller 1, when you create the Controller 2, set it's delegate property, like this:
QuizViewController *controller2 = [[QuizViewController alloc] init]];
controller2.delegate = self;
And then create the playButtonPressed method somewhere in controller 1. From Controller 2, you'll do something like:
[self.delegate playButtonPressed:self];
And that will call the method in Controller 1, where you can pause the background player.

iOS ViewControllers

ok, this may be a very basic question and I apologise if it is, but its driving me mad and I really need to get a better understanding.
I have an app that I am developing and using a new view controller and xib file for each page.
I call each page by using this code:-
HelpVC = [[[HelpViewController alloc] initWithNibName:#"HelpViewController" bundle: [NSBundle mainBundle]]autorelease];
[self presentModalViewController:HelpVC animated:YES];
Which works great.
However if I have this declared in a file and I then want to call it somewhere else I am getting errors all over the place.
is there any way to initialise all the xib files first in a class file and then call them from that file when needed??
Thanks
This is the error I get back
error: expected specifier-qualifier-list before 'UserInputViewController'
It seems to cause all of my declarations in the .h file to error with the message above.
Some sample code is:-
.h file
#import <UIKit/UIKit.h>
#import "CgePWViewController.h"
#import "LogBackInViewController.h"
#import "LoginViewController.h"
#interface SettingsViewController : UIViewController
{
CgePWViewController *cPWVC;
LogBackInViewController *LBIVC;
LoginViewController *login;
}
#property(nonatomic, retain) CgePWViewController *cPWVC;
#property(nonatomic, retain) LogBackInViewController *LBIVC;
#property(nonatomic, retain) LoginViewController *login;
so basically as soon as I have added in LoginViewController it causes everything else to error, if I take this out then the app runs perfectly.
Does your LoginViewController.h imports the SettingsViewController as well? It seems to be a "cross reference" (I don't know if this term makes sense in English as it does in Portuguese).
Before your
#interface SettingsViewController : UIViewController
put a
#class LoginViewController;
and check if it helps.
Don't initialise your view controllers to put them in properties. Instead, create them as soon as you need them.
Let's assume you want to show a view controller when somebody pushes a button. Add this method to your view controller (let's assume it's named MyFirstViewController):
- (IBAction)someButtonDidPush: (UIButton *)sender {
MySecondViewController *vc = [[MySecondViewController alloc] init];
// Do stuff and then show it. One way may be like this:
[self presentViewController: vc animated: YES completion: nil];
}
Just hook up the button with the method above. No properties needed! You're (probably) done.

Resources