简体   繁体   中英

audio is not playing on button click

I want to play a audio on Button click. I have tried this code but audio is not playing:-

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *audioPlayer  = [[AVAudioPlayer alloc] initWithData:soundFileURL error:nil];
audioPlayer.delegate = self;
audioPlayer.volume=1.0;
[audioPlayer setNumberOfLoops:1];
[audioPlayer play];

First, it appears you are using initWithData to access an audio file pointed to by a URL.

You should instead use initWithContentsOfU‌RL .


Another issue could be due to a common error in implementing the audio player.

If the AVAudioPlayer you are creating is inside a function, it will be released, or deallocated, at the end of the function. This is why the audio does not play.

To fix this problem, make the AVAudioPlayer a property or instance variable in your class so that it does not get immediately deallocated.

For example, a property is declared in your header as

@property(strong, nonatomic) AVAudioPlayer *audioPlayer;

and initialized in your implementation with

self.audioPlayer = [[AVAudioPlayer alloc] initWithData:soundFileURL error:nil];

If this does not solve your problem, then the issue is likely to do with the file pointed to by your URL.

you haven't created a player item

//declare in .h file

AVPlayerItem *playerItem;

//then in .m file

player = [AVPlayer playerWithPlayerItem:playerItem];

after that play it

   [player play];
NSString *soundFilePath = [NSString stringWithFormat:@"%@/test.mp3", 
   [[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL  error:nil];
player.numberOfLoops = -1; //Infinite

[player play];

print "soundFileURL" . check whether it is null(in case soundFileURL contains spaces it results/prints null ) and if it not a null value check the file name is spelled exactly correct or not(ie, here your file name is " sound ")

var asyncaAudioPlayer: AVAudioPlayer!

let audioFilePath = NSBundle.mainBundle().pathForResource(fileName, ofType: "mp3")

let audioFileUrl = NSURL.fileURLWithPath(audioFilePath!)

asyncaAudioPlayer = AVAudioPlayer(contentsOfURL: audioFileUrl, fileTypeHint: nil)

audioPlayerArray.append(asyncaAudioPlayer)

asyncaAudioPlayer.prepareToPlay()


asyncaAudioPlayer.play()

Your problem is you set NSURL for initWithData -> Wrong. in case it sound play multi time, you should prepare your sound Data and keep it as strong ref.

and you should use CGD and global queue instead of main queue.

-(void)onTouchUpInside{
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),   ^{
        AVAudioPlayer *audioPlayer  = [[AVAudioPlayer alloc] initWithData:self.soundFileData error:nil];
        audioPlayer.delegate = self;
        audioPlayer.volume=1.0;
        [audioPlayer setNumberOfLoops:1];
        if ([audioPlayer prepareToPlay] && [audioPlayer play])
        {
            NSLog(@"play Success!");
        }
        else
        {
            NSLog(@"Failed to play the audio file.");
        }

    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM