Stopping sounds and looping them in xcode - ios

Ok so im pretty new to Xcode and am trying to make a simple app. i need my sound to keep looping and stop when i press the stop button. Please help and thanks in advance. here is my code so far.
as you can see the sound plays once then stops .
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize Images;
-(IBAction)start:(id)sender;{
AudioServicesPlaySystemSound(PlaySoundID);
Images.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Light.png"],
[UIImage imageNamed:#"Light1.png"],
[UIImage imageNamed:#"Light2.png"],
[UIImage imageNamed:#"Light3.png"], nil];
Images.animationRepeatCount = 99999999;
Images.animationDuration = .2;
[Images startAnimating];
[self.view addSubview:Images];
}
-(IBAction)stop:(id)sender;{
[Images stopAnimating];
}
- (void)viewDidLoad
{
NSURL *SoundURL = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:#"Siren" ofType:#"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)SoundURL, &PlaySoundID);
[super viewDidLoad];
Images.image = [UIImage imageNamed:#"Light.png"];
[self.view addSubview:Images];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

NSURL *soundURL = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:#"Siren"
ofType:#"wav"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL
error:nil];
for "infinite" number of loops:
audioPlayer.numberOfLoops = -1;

Related

Add a gif video file in UIWebview

I'm adding a GIF image in UIWbeview in Objective-C. The problem is that the image size doesn't fit in the screen and the contents above the gif video is not showing. How can I adjust the image size and show the buttons above it.
My code is:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"back" ofType:#"gif"];
NSData *gif = [NSData dataWithContentsOfFile:filePath];
UIWebView *webViewBG = [[UIWebView alloc] initWithFrame:self.view.frame];
[webViewBG loadData:gif MIMEType:#"gif" textEncodingName:NULL baseURL:NULL];
webViewBG.userInteractionEnabled = NO;
webViewBG.scalesPageToFit=YES;
[self.view addSubview:webViewBG];
// Do any additional setup after loading the view.
}
You can move your code to viewDidAppear
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"back" ofType:#"gif"];
NSData *gif = [NSData dataWithContentsOfFile:filePath];
UIWebView *webViewBG = [[UIWebView alloc] initWithFrame:self.view.frame];
[webViewBG loadData:gif MIMEType:#"image/gif" textEncodingName:#"UTF-8" baseURL:NULL];
webViewBG.userInteractionEnabled = NO;
webViewBG.scalesPageToFit=YES;
[self.view addSubview:webViewBG];
}

How to play the music continously which are stored in NSArray?

In tableview footer i have a play button to play the music, as the music starts tableview scrolls automatically according to the audio played but if the user tap on particular cell, the content in the particular cell is playing and stops but i want to continue the music from where the use taps to the end. Below is the code. Please help.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int ref =(int) indexPath.row;
NSString *filePath = [soundArray objectAtIndex:ref];
NSString *Path = [[NSBundle mainBundle] pathForResource:filePath ofType:#"mp3"];
NSURL *musicFile = [NSURL fileURLWithPath:Path];
myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
if (playing == YES) {
[playButton setBackgroundImage:[UIImage imageNamed:#"pause_White.png"] forState:UIControlStateNormal];
[myAudioPlayer play];
// playing = NO;
}
if (playing == NO) {
// [playButton setBackgroundImage:[UIImage imageNamed:#"play_white.png"] forState:UIControlStateNormal];
}
}
SoundArray contains trimmed music. How to continue the song ? Help much appreciated.
Here is my code as per my concept. You may have to manage it more as its just a sample
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
isMainMusic = NO;
// Construct URL to sound file
NSString *path = [NSString stringWithFormat:#"%#/1.mp3", [[NSBundle mainBundle] resourcePath]];
mainMusicUrl = [NSURL fileURLWithPath:path];
NSString *path1 = [NSString stringWithFormat:#"%#/2.mp3", [[NSBundle mainBundle] resourcePath]];
secondMusicUrl = [NSURL fileURLWithPath:path1];
}
-(void)playSecondaryMusic
{
if ([playMainMusic isPlaying]) {
[playMainMusic pause];
}
if (playSecondaryMusic) {
playSecondaryMusic.delegate = nil;
playSecondaryMusic = nil;
}
isMainMusic = NO;
playSecondaryMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:secondMusicUrl error:nil];
playSecondaryMusic.delegate = self;
[playSecondaryMusic play];
}
- (IBAction)playMainMusic:(UIButton *)sender {
if (playMainMusic) {
playMainMusic.delegate = nil;
playMainMusic = nil;
}
isMainMusic = YES;
playMainMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:mainMusicUrl error:nil];
playMainMusic.delegate = self;
[playMainMusic play];
}
//Delegate method
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if (isMainMusic)
{
NSLog(#"Main music");
}
else
{
NSLog(#"Second music");
[playMainMusic play];
isMainMusic = YES;
}
}

how to import songs dynamically to our app in Objective C

I tried iPhone app.In this app I need to import all songs into table-view when I click on import button.I don't know how to import songs dynamically.Please tell me what are the Frameworks to be used.
But i have played Locally saved songs like code:-
using this two Frameworks `AVFoundation.framework,MediaPlayer.framework
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#interface ViewController ()
#property (nonatomic, strong) AVAudioPlayer *audioPlayer;
- (IBAction)playSound:(id)sender;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:#"drum" ofType:#"mp3"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSError *error = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData
error:&error];
[self.audioPlayer prepareToPlay];
self.audioPlayer.numberOfLoops = -1;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)playSound:(id)sender {
[self.audioPlayer play];
UIView *wrapperView = [[UIView alloc] initWithFrame:CGRectMake(20, 120, 260, 20)];
wrapperView.backgroundColor = [UIColor clearColor];
[self.view addSubview:wrapperView];
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame: wrapperView.bounds];
[wrapperView addSubview:volumeView];
}
#end
Output for this code is songs played locally but I need when I click play button it should import 10 songs in tableView and then songs mush be played.
Please give me any idea.
Thanks in advanced.

Blend two videos using GPUImage

Using the latest code of GPUImage from GitHub I tried the following:
#import "ViewController.h"
#import "GPUImage.h"
#import <AssetsLibrary/AssetsLibrary.h>
#interface ViewController ()
{
GPUImageOutput<GPUImageInput> *filter0;
GPUImageMovieWriter *movieWriter;
GPUImageMovie *movie1, *movie2;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"started");
NSURL *url = [[NSBundle mainBundle] URLForResource:#"video1" withExtension:#"MOV"];
movie1 = [[GPUImageMovie alloc] initWithURL:url];
movie1.playAtActualSpeed = YES;
NSURL *url2 = [[NSBundle mainBundle] URLForResource:#"video2" withExtension:#"MOV"];
movie2 = [[GPUImageMovie alloc] initWithURL:url2];
movie2.playAtActualSpeed = YES;
filter0 = [[GPUImageNormalBlendFilter alloc] init];
[filter0 forceProcessingAtSize:CGSizeMake(1080, 1920)];
[movie1 addTarget:filter0];
[movie2 addTarget:filter0]; // movie2 adding Filter
GPUImageView *filterView= (GPUImageView *) self.view;
filterView.fillMode = kGPUImageFillModePreserveAspectRatio;
[filter0 addTarget:filterView];
[movie1 startProcessing];
[movie2 startProcessing]; // movie2 startprocessing
}
#end
Basically, I want to blend two videos and display it on the screen, but after running, nothing gets displayed, only black screen is being shown and the first frame of the 2nd video is shown after few seconds.
** I have added the "Header Search Path" and also given the flag -ObjC in "Other Linkers"
Now if I add a PixellateFilter to any one of the video(say movie1, commenting movie2 adding Filter and movie2 startprocessing then the result is coming.
So my question is why the blend is not happening for two videos, am I doing something wrong.
Thanks in advance
NSURL *url = [[NSBundle mainBundle] URLForResource:#"abc" withExtension:#"mp4"];
gpuIM = [[GPUImageMovie alloc] initWithURL:url];
gpuIM.playAtActualSpeed = YES;
NSURL *url2 = [[NSBundle mainBundle] URLForResource:#"def" withExtension:#"mov"];
movie2 = [[GPUImageMovie alloc] initWithURL:url2];
movie2.playAtActualSpeed = YES;
filter0 = [[GPUImageColorDodgeBlendFilter alloc] init];
[gpuIM addTarget:filter0];
[movie2 addTarget:filter0];
[filter0 addTarget:_view0];
[gpuIM startProcessing];
[movie2 startProcessing];
Another approach would be create a avasset from URL , use it in GPUImageMovie,most importantly make the GPUImageMovie instance global.

My buttons play all the same sound?

Here is my MainViewController.m
#import "MainViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
#synthesize audioPlayer;
#synthesize soundsArray;
-(void)prepareSounds
{
NSString *filepath= [[NSBundle mainBundle] pathForResource:#"Sounds" ofType:#"plist"];
self.soundsArray = [[NSArray alloc] initWithContentsOfFile:filepath];
}
- (IBAction)playSound:(id)sender {
UIButton *buttonPressed = (UIButton *)sender;
NSString *soundName = [soundsArray objectAtIndex:(buttonPressed.tag -1)];
NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:#"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *p = [[AVAudioPlayer alloc]
initWithContentsOfURL:file error:nil];
self.audioPlayer = p;
[self.audioPlayer play];
}
- (IBAction)playSound2:(id)sender {
UIButton *buttonPressed = (UIButton *)sender;
NSString *soundName = [soundsArray objectAtIndex:(buttonPressed.tag -2)];
NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:#"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *p = [[AVAudioPlayer alloc]
initWithContentsOfURL:file error:nil];
self.audioPlayer = p;
[self.audioPlayer play];
}
- (IBAction)playSound3:(id)sender {
UIButton *buttonPressed = (UIButton *)sender;
NSString *soundName = [soundsArray objectAtIndex:(buttonPressed.tag -3)];
NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:#"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *p = [[AVAudioPlayer alloc]
initWithContentsOfURL:file error:nil];
self.audioPlayer = p;
[self.audioPlayer play];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Flipside View
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)showInfo:(id)sender
{
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideViewController" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:controller animated:YES completion:nil];
}
#end
My MainViewController.h
#import <UIKit/UIKit.h>
#import "FlipsideViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
AVAudioPlayer *audioPlayer;
NSArray *soundsArray;
}
#property(nonatomic, retain) AVAudioPlayer *audioPlayer;
#property(nonatomic, retain) NSArray *soundsArray;
-(void)prepareSounds;
- (IBAction)playSound:(id)sender;
- (IBAction)playSound2:(id)sender;
- (IBAction)playSound3:(id)sender;
#end
In the 'Supporting Files' folder I have an array of strings with the name of the sound files I want to play, and in the 'Supporting Files' folder I have a folder named 'Sounds', which contains the sound files.
All of my buttons play the same sound. Can someone please provide some insight. Thanks!
I think the problem may be in this line playSound:
NSString *soundName = [soundsArray objectAtIndex:(buttonPressed.tag -1)]
which is repeated in playSound2 and playSound3 with "buttonPressed.tag -2" and "buttonPressed.tag -3".
If your buttonPressed.tags are set to 1, 2, and 3, then each time "buttonPressed.tag -X" is likely evaluating to 0, and playing the sound of the first file in the array.
You are repeating the code to do the same task.
Add all your buttons IBAction to a single method (say it as playSound)
Implement the method like:
- (IBAction)playSound:(UIButton *)sender
{
NSString *soundName = [soundsArray objectAtIndex:(sender.tag -1)];
NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:#"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *p = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
self.audioPlayer = p;
[self.audioPlayer play];
}
There is no need of writing same code for each individual button.
SOLVED: prepareSounds() was never called. Here is the working code:
#import "MainViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
#synthesize audioPlayer;
#synthesize soundsArray;
-(void)prepareSounds
{
NSString *filepath= [[NSBundle mainBundle] pathForResource:#"Sound" ofType:#"plist"];
self.soundsArray = [[NSArray alloc] initWithContentsOfFile:filepath];
}
- (void)stopAudio
{
if (audioPlayer!= nil) {
[audioPlayer stop];
//do some task for changing the Image i.e setting the default image
}
}
- (IBAction)playSound:(UIButton *)sender
{
UIButton *btn = (UIButton*)sender;
NSString *soundName = [soundsArray objectAtIndex:(btn.tag - 1)];
NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:#"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *p = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
self.audioPlayer = p;
if([audioPlayer isPlaying])
{
[self stopAudio];
}
[self.audioPlayer play];
}
- (void)viewDidLoad
{
[self prepareSounds];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Flipside View
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)showInfo:(id)sender
{
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideViewController" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:controller animated:YES completion:nil];
}
#end

Resources