I put a button on a table view cell that play sound when they are pressed but - iphone

I put a button on a table view cell that play sound when they are pressed, when I press the button it stop the sound. But pressing another button on another cell it doesn't plays another sound while the other still playing. I hope that when another button is pressed in another table cell it will stop the playing sound.
Here is my code
.h file
I enter this on #interface bracket AVAudioPlayer *talkSound;
.m
NSString *myExamplePath = [[NSBundle mainBundle] pathForResource:#"kumusta" ofType:#"m4a"];
if (talkSound.playing) {
[talkSound stop];
[talkSound release];
}
talkSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:myExamplePath] error:NULL];
talkSound.delegate = self; /*with this here it says theres no delegate so i go an add AVAudioPlayerDelegate and i get tons of errors of connot find protocol declaration*/
[talkSound play];

I don't know if this is the problem, but you don't need to release and reallocate the AVAudioPlayer every time. Just stopping and starting it should work fine, like:
if (talkSound.playing) {
[talkSound stop];
}
[talkSound play];
Then you can just alloc/init the AVAudioPlayer once, perhaps in viewDidLoad.

Related

MPMoviePlayerViewController won't play larger mp4 files

What I have are 2 links to 2 versions of the same movie, one in lower definition the other in higher, both .mp4 format.
It plays the low-def just fine, but when I click the link for the HD version the movie player pops up like it should then dismisses itself back to the original view controller. If I click it again, it works, but the first try usually fails. Like something with threading is going on and succeeding or not. From what I've read it's an issue with ARC but I can't seem to get it right.
I want to use the MPMoviePlayerViewController because I like how it by default acts as a modal popup. I tried with a MPMoviePlayerController but you have to set the view then handle rotations it seems.
I'm not getting any crashes or any warnings in the log inside Xcode either.
Here's my code:
#interface VideoInfoViewController ()
#property (nonatomic, strong) MPMoviePlayerController *player;
#end
- (IBAction)playLowVideo:(id)sender {
[self playVideoFromURL:[NSURL URLWithString:_video.video_url_low]];
}
- (IBAction)playHDVideo:(id)sender {
[self playVideoFromURL:[NSURL URLWithString:_video.video_url_HD]];
}
- (void)playVideoFromURL:(NSURL *)url {
MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
_player = playerViewController.moviePlayer;
_player.scalingMode = MPMovieScalingModeFill;
[_player prepareToPlay];
[self presentMoviePlayerViewControllerAnimated:playerViewController];
}
ETA:
found this tutorial: http://josebolanos.wordpress.com/2011/09/28/dismissing-mpmovieplayerviewcontroller-the-right-way/
- (void)playVideoFromURL:(NSURL *)url {
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentModalViewController:player animated:YES];
[player.moviePlayer prepareToPlay];
[player.moviePlayer play];
}
still doesn't work.

How to play AVAudioPlayer local to method in ios5?

I think it has to do with ARC in iOS 5, but when I declare an AVAudioPlayer in a method and play it, it won't play. I think it gets released automatically at the end of the method because I don't remember this happening prior to iOS5. If I make it a global variable it works fine. Any way to use a local instance?
Make it a retained property, and make your class the delegate, so you can clean up when it's done playing. In your header:
#interface MyClass : NSObject <AVAudioPlayerDelegate>
#property(strong)AVAudioPlayer *player;
Then, when you use it:
self.player = [AVAudioPlayer alloc] initWithContentsOfURL:URL error:&err];
[player setDelegate:self];
and clean it up in the delegate method:
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)playedSuccessfully {
self.player = nil;
}
One nice thing about having it as a property is that you can interrupt it if you need to:
-(void)someInterruptionOccurred {
[self.player stop];
}

How can I update music volume on iPhone, through an UISlider?

I've tried to make an UISlider that controls background music volume, and whatever I set the value to should be the volume of the music. The trouble is, when I run the app in simulator, and move the slider, the volume stays that of the original volume, it doesn't update.
I am new to programming so I still need help with the basics.
Here is my viewcontroller.h:
#interface FifthViewController : UIViewController <AVAudioPlayerDelegate> {
IBOutlet UISlider *slider;
IBOutlet UIAlertView *alert;
AVAudioPlayer *theAudio;
}
-(IBAction)alertbutton;
-(IBAction)changeVolume;
#end;
and here is the viewcontroller.m:
- (void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"background music" ofType:#"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
theAudio.numberOfLoops = -1;
theAudio.volume=slider.value;
[super viewDidLoad];
}
I have connected "slider" to a horizontal slider and the sound plays and loops. And if I set the value of the slider higher or lower in Xcode, when I run the app, the volume adjusts accordingly. But if in the app I change the value of the slider, the volume remains the same . Can anyone look at my code and tell me what I'm doing wrong and how to fix it?
Assuming you have an instance of AVAudioPlayer setup in your controller.
Set the slider to run from 0.0 to 1.0 and connect it to an action.
-(void)volumeValueChange:(UISlider *)sender
{
(AVAudioPlayer *)myaudioplayer.volume = sender.value;
}
//.m implementation where audioPlayer is an an object of AVAudioPlayer
-(void)volumeValueChange:(UISlider *)sender
{
audioPlayer.volume = sender.value;
}

how to play different audio files with same media player in a separate view- iphone sdk?

I've been trying and researching for countless hours but for some reason I just cant get my head around it and I know its a simple answer. Please Help!
I have a view with different buttons, -(ScrollViewController1)
the different buttons push in one view, -(b1ViewController)
this view contains the media player,
In a nutshell, I cant figure out how to get the media player to play a different audio file depending on which button in the first view was pressed.
I'm trying not to make a different view with a different media player for every button.
Could someone demonstrate it please so that I can understand it? Thanks a million.
Heres the relevant code-
(ps- theres a reason for the tags, I didnt want to get too much into for the sake of asking a clear question and keeping it straight forward.)
#implementation ScrollViewController1
- (IBAction)pressedb1view {
if (thebutton.tag==101) {
[self performSelector:#selector(dismissb1pop:) withObject:nil afterDelay:0.1];
b1view.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:b1view animated:YES];
}
if (thebutton.tag==102) {
[self performSelector:#selector(dismissb1pop:) withObject:nil afterDelay:0.1];
b1view.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:b1view animated:YES];
}
if (thebutton.tag==103) {
[self performSelector:#selector(dismissb1pop:) withObject:nil afterDelay:0.1];
b1view.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:b1view animated:YES];
}
}
etc, etc, etc....
in the next view which contains the mediaplayer I have-
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface b1ViewController : UIViewController {
MPMoviePlayerController *moviePlayer;
NSURL *movieURL;
}
in the .m -
-(void)viewWillAppear:(BOOL)animated {
[[self navigationController] setNavigationBarHidden:NO animated:YES];
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"music1" ofType:#"mp3"];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, 320, 40);
[moviePlayer play];
}
EDIT:
Im passing text from the first view to a uilabel on the second view using the following in the first view-
[b1view changeSiteText:#"im stuck here"];
in the second view Im doing this in the .h-
- (IBAction) changeSiteText:(NSString *)str;
and in the .m im doing this-
- (IBAction) changeSiteText:(NSString *)str{
lblSiteTxt.text = str;
}
Is there a way to implement a similar method for the mediaplayer,??? maybe doing
something like
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"%#" ofType:#"mp3", str];
Im stuck!!
Help!
Do you push the view or present it?
Add a method in the player class which when passed various arguments it plays the appropriate track or whatever. Then the different IBActions will invoke this method.

How to Unload a TabBar View when New TabBar view is loaded?

I am playing an audio file in a viewcontroller inside an UITabBar using AVAudioPlayer. So when user clicks a different tabbar item the music is not stopped. I tried adding stop method in viewwilldisappear. But it's not responding. How can I stop my audio when user clicks a new tab bar item.
ente
AVAudioPlayer *audioPlayer;
This is my code in view will disappear
[audioPlayer stop];
[sliderTimer invalidate];
audioPlayer = nil;
self.URL = nil;
self.musicTitle = nil;
Thanks,
Nikil
This worked for me.
(void)viewWillDisappear:(BOOL)animated {
[self.audioPlayer stop];
}