1

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.

4

1 回答 1

1

以下是简单的代码:-

  - (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!");
}
   }
于 2013-10-26T14:24:53.883 回答