AVAudioPlayer不玩iPad2

时间:2012-02-25 00:57:53

标签: objective-c ios ipad xcode4 avaudioplayer

IOS开发新手,我正在测试AVAudioplayer在iPad2上播放声音(Xcode 4.2项目,ARC /故事板启用)。声音在模拟器中播放正常,没有错误。设备上没有错误但没有声音。

一直在浏览这个精美的资源寺庙,但我根据这里的反馈所做的一切都没有产生任何震耳欲聋的iPad沉默。有人可以帮忙吗?我的.h:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController : UIViewController
<AVAudioPlayerDelegate>
{
    AVAudioPlayer *audioPlayer;
    UISlider *volumeControl;
    UILabel *timerLabel;
    NSTimer *playbackTimer;
}
@property (nonatomic, retain) IBOutlet UISlider *volumeControl;
@property (nonatomic, retain) IBOutlet UILabel *timerLabel;
@property (nonatomic, retain) NSTimer *playbackTimer;
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
-(IBAction) playAudio;
-(IBAction) stopAudio;
-(IBAction) adjustVolume;
@end

我的.m:

#import "ViewController.h"

@implementation ViewController
@synthesize volumeControl, timerLabel, playbackTimer, audioPlayer;

-(void)playAudio
{
    playbackTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                     target:self
                                                   selector:@selector(updateTime)
                                                   userInfo:nil
                                                    repeats:YES];
    [audioPlayer play];
}

-(void)stopAudio
{
    [playbackTimer invalidate];
    [audioPlayer stop];
}
-(void)adjustVolume
{
    if (audioPlayer != nil)
    {
        audioPlayer.volume = volumeControl.value;
    }
} 

-(void)updateTime
{
    float minutes = floor(audioPlayer.currentTime/60);
    float seconds = audioPlayer.currentTime - (minutes * 60);

    float duration_minutes = floor(audioPlayer.duration/60);
    float duration_seconds = 
    audioPlayer.duration - (duration_minutes * 60);

    NSString *timeInfoString = [[NSString alloc] 
                                initWithFormat:@"%0.0f.%0.0f / %0.0f.%0.0f",
                                minutes, seconds, 
                                duration_minutes, duration_seconds];

    timerLabel.text = timeInfoString;
}


-(void)audioPlayerDidFinishPlaying:
(AVAudioPlayer *)player successfully:(BOOL)flag
{
}
-(void)audioPlayerDecodeErrorDidOccur:
(AVAudioPlayer *)player error:(NSError *)error
{
}
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
}
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
}

我的viewDidLoad:

 - (void)viewDidLoad {
        [super viewDidLoad];

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

        NSError *error;
        audioPlayer = [[AVAudioPlayer alloc]
                       initWithContentsOfURL:url
                       error:&error];
        if (error)
        {
            NSLog(@"Error in audioPlayer: %@", 
                  [error localizedDescription]);
        } else {
            audioPlayer.delegate = self;
            [audioPlayer prepareToPlay];
        }

    [super viewDidLoad];
}

2 个答案:

答案 0 :(得分:0)

确保该文件确实是mp3格式。确保将文件复制到捆绑包中,而不是在桌面上播放本地路径。检查设备音量。检查播放通话中的返回BOOL。所有这些都是可能的解释。

答案 1 :(得分:0)

根本不播放声音吗?即使插入耳机也没声音?如果通过内置扬声器没有声音,但通过耳机发出声音,请确保设备响铃/声音音量没有静音。检查侧面的拨动开关(如果您设置为静音与方向锁定)。钟声不应该被划掉。仅仅因为按下音量增大按钮并不意味着它从扬声器中取消静音。您是否测试过YouTube视频或音乐文件以确保您的iPad没有硬件问题?