简体   繁体   中英

No sound when button clicked

I am trying to play a sound when I click a button, the sound clip is 10 sec, the application runs fine and when I click the button nothing happens there are no errors or nothing, this is my code

.m file
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
- (IBAction)Play:(id)sender {

    AVAudioPlayer *audioPlayer;
    NSString *audioPath = [[NSBundle mainBundle]pathForResource:@"Blesgaes" ofType:@"mp3"];
    NSURL *audioURL =[NSURL fileURLWithPath:audioPath];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
    [audioPlayer play];
}
.h file
#import <AVFoundation/AVFoundation.h>


@interface ViewController : UIViewController
- (IBAction)Play:(id)sender;

I have added AVFoundation framework and I have followed these steps

http://looksok.wordpress.com/2013/01/19/ios-tutorial-play-sound/ http://www.techotopia.com/index.php/Playing_Audio_on_an_iPhone_using_AVAudioPlayer_(iOS_6) http://code-and-coffee.blogspot.in/2012/09/how-to-play-audio-in-ios.html

Any ideas what I am doing wrong.

Below is the simple code:-

  - (IBAction)playAudio:(id)sender {
   AVAudioPlayer *audioPlayer;
   NSString *audioPath = [[NSBundle 
    mainBundle] pathForResource:@"Woof" 
    ofType:@"mp3"];
  NSURL *audioURL = [NSURL 
  fileURLWithPath:audioPath];
NSError *audioError = [[NSError alloc] init];
audioPlayer = [[AVAudioPlayer alloc]    
 initWithContentsOfURL:audioURL 
 error:&audioError];

   if (!audioError) {
    [audioPlayer play];
    NSLog(@"Woof!");
}
else {
    NSLog(@"Error!");
}
   }

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