【问题标题】:AVAudioPlayer not playing iPad2AVAudioPlayer 不播放 iPad2
【发布时间】:2012-02-25 00:57:52
【问题描述】:

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

一直在浏览这个精美的资源殿堂,但根据此处的反馈,我所尝试的一切都没有产生任何东西,只是让 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];
}

【问题讨论】:

    标签: objective-c ios ipad xcode4 avaudioplayer


    【解决方案1】:

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

    【讨论】:

    • 是的,文件绝对是 mp3,文件包中的文件,最大设备音量,但是尽管在 IB 中正确链接了开始按钮,但在 didstartplaying 或 didfinishplaying 上都无法返回日志?
    • 尝试重启模拟器/设备。有时当音频发生变化时,例如在使用 Boom 之类的应用时,可能会发生这种情况。
    • 那么上面的东西是什么?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多