【问题标题】:Play a song in iOS在 iOS 中播放歌曲
【发布时间】:2014-03-13 16:03:32
【问题描述】:

我试图在 iOS 中播放一首歌曲,但它给了我一条错误消息。

头文件.h

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

@interface PRPViewController : UIViewController{
    AVAudioPlayer *audioPlayer;
    IBOutlet UIButton *start;
}

-(IBAction)play;

@end

实现文件.m

NSURL *url = [NSURL fileURLWithPath:
                 [NSString stringWithFormat:@"%@/bobmarley.mp3",          
                     [[NSBundle mainBundle] resourcePath]]];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsofURL:url error:&error];
audioPlayer.numberOfLoops = 0;

[audioPlayer play];

但它说

AVAudioPlayer 没有可见的@interface 声明选择器“initWithContentsofUrl:error:”

我该怎么办?

【问题讨论】:

    标签: ios objective-c avaudioplayer audio-player


    【解决方案1】:

    Of 中的“O”应大写。在 Objective-C 中,拼写很重要,包括大小写。 initWithContentsofURLinitWithContentsOfURL 是两个不同的东西。

    (顺便说一句,这是尽可能多地使用自动完成的一个很好的理由。自动完成机制比你更清楚如何拼写声明的方法的名称!)

    【讨论】:

      【解决方案2】:

      您应该使用initWithContentsOfURL 方法检查您的系统上的文件是否可用,您的写错了。否则应用程序可能会崩溃。我创建了一个为我处理所有事情的类:

      @implementation AudioPlayer{
      
          AVAudioPlayer *_sound;
          NSURL *_soundURL;
          NSString *_receivedValue;
      
          float _volumeSpecific;
      }
      
      - (id)initWithAudioFile:(NSString *)fileName andExtension:(NSString *)extension{
      
          self = [super init];
          if( self ){
              _receivedValue = fileName;
      
              _soundURL = [NSURL fileURLWithPath:
                           [[NSBundle mainBundle] pathForResource:fileName
                                                           ofType:extension]];
              if([[NSFileManager defaultManager] fileExistsAtPath:[_soundURL path]]){
                  _sound = [[AVAudioPlayer alloc] initWithContentsOfURL:_soundURL 
                                                                  error:nil];
              }
          }
      
          return self;
      }
      
      - (void)playEndless{
      
          if( [[NSUserDefaults standardUserDefaults] boolForKey:kSound] ){
              _sound.numberOfLoops = -1;
              [_sound play];
          }
      }
      
      - (void)setVolume:(float)myVolume{
      
          _volumeSpecific = myVolume;
          [_sound setVolume:myVolume];
      }
      
      - (void)play{
          if( _sound == nil ){
              NSLog(@"No AudioPlayer available %@", self);
          }
          if( [[NSUserDefaults standardUserDefaults] boolForKey:kSound] ){
      
              if( _volumeSpecific ){
                  [_sound setVolume:_volumeSpecific];
              }
              [_sound play];
          }
      }
      
      - (NSString *)description{        
          return [NSString stringWithFormat:@"Received: %@, Player: %@, URL: %@",
                  _receivedValue, _sound, _soundURL];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多