문제

I'm following in a tutorial on how to play mp3 sounds when pressing a button.

I created a button (playSound).

I added it to the view controller interface:

- (IBACTION)playSound:(id)sender;

in the implementation I declared the needed header files and I wrote the following:

#import "AudioToolbox/AudioToolbox.h"
#import "AVFoundation/AVfoundation.h"

- (IBAction)playSound:(id)sender {

    //NSLog(@"this button works");
    AVAudioPlayer *audioPlayer;
    NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"audio" ofType:@"mp3"];
    //NSLog(@"%@", audioPath);
    NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
    //NSLog(@"%@", audioURL);
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
    [audioPlayer play];

}

I'm not getting any errors. the NSlogs are logging the URL fine, and now I have no clue where to look further. I also checked if my MP3 sound was maybe damaged, but that was also not the case. all i hear is a little crackling noise for 1 second. then it stops.

도움이 되었습니까?

해결책

You declared a local variable audioPlayer to hold a pointer to the player. As soon as your button handler returns the player is being released before it has a chance to play your sound file. Declare a property and use it instead of the local variable.

In YourViewController.m file

@interface YourViewController ()
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@end

or in YourViewController.h file

@interface YourViewController : UIViewController
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@end

Then replace audioPlayer with self.audioPlayer in your code.

다른 팁

try this its working for me

in .h

AVAudioPlayer * _backgroundMusicPlayer;

in .m

NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:@"Theme" ofType:@"mp3"];
    NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
    NSError *error;
    _backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
    [_backgroundMusicPlayer setDelegate:self];  // We need this so we can restart after interruptions
    [_backgroundMusicPlayer setNumberOfLoops:-1];
    [_backgroundMusicPlayer play];

Edited

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Name of your audio file" 
                                                              ofType:@"type of your audio file example: mp3"];
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
    audioPlayer.numberOfLoops = -1;
    audioPlayer.delegate=self;
    [audioPlayer play];

Try this and let me know. Make sure you set the delegate for audioPlayer.

Firstly re add your music file in your project , then try this code With the log you can see error.

NSError *error;

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"bg_sound" ofType:@"mp3"]];

AVAudioPlayer   *audioPlayer = [[AVAudioPlayer alloc]
                              initWithContentsOfURL:url
                              error:&error];
if (error){
//Print Error
NSLog(@"Error in audioPlayer: %@",
          [error localizedDescription]);
} else {
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer setNumberOfLoops: -1];
[audioPlayer play];
audioPlayer.volume=1.0;

}

Make sure your music files are properly added in project

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top