AVAudioPlayer没有播放Mp3文件我试过但没有运气

时间:2016-03-29 04:54:56

标签: ios objective-c avaudioplayer

您好我使用下面的代码来播放我的本地mp3文件。但没有错误来了。但是mp3没有播放。

protected $primaryKey = "item_id";

1 个答案:

答案 0 :(得分:0)

检查以下步骤。

  1. 您的AppDelegate.m

    #import <AVFoundation/AVAudioSession.h>`<AVFoundation/AVAudioSession.h>
    
  2. 您的application:didFinishLaunchingWithOptions:方法

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor];
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    }
    
  3. 在ViewController.h中

    @property (nonatomic,retain) AVAudioPlayer  *audioPlayer ;
    
  4. 在您的ViewController.m

    @synthesize audioPlayer;
    
  5. 然后

    NSString *sound_file;
    if ((sound_file = [[NSBundle mainBundle] pathForResource:@"mpthreetest" ofType:@"mp3"])){
    
        NSURL *url = [[NSURL alloc] initFileURLWithPath:sound_file];
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
        audioPlayer.delegate = self;
    
        [audioPlayer prepareToPlay];
        [audioPlayer play];    
    }
    

    你犯的错误是你没有设置属性。