IOS Dev (20) uses AVAudioPlayer to play a local audio file

Source: Internet
Author: User

IOS Dev (20) uses AVAudioPlayer to play a local audio file

  • Author: CSDN laruence
  • Blog: http://blog.csdn.net/preventionstep
    • Step 1: Add AVFoundation. framework in Project-TARGETS-Project name-Build Phases-Link Binary With Libraries.
    • Step 2: Create a subclass of UIViewController PlayerViewController.
    • Step 3: add an AVAudioPlayer attribute to PlayerViewController.
    • Step 4: implement the most important code in the viewDidLoad method of PlayerViewController. Key code

      . H

      #import 
            
             #import @interface PlayViewController: UIViewController@property (strong, nonatomic) AVAudioPlayer *player;@end
            

      . M

      #import "PlayerViewController.h"@interface PlayerViewController ()@end@implementation PlayerViewController- (void) viewDidLoad{    [super viewDidLoad];    AVAudioSession *session = [AVAudioSession sharedInstance];    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];    [audioSession setActive:YES error:nil];    NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"rem" ofType:@"wav"];    NSURl *audioUrl = [NSURL fileURLWithPath:audioPath];    NSError *playerError;    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:audioUrl error:&playerError];    if (_player === NULL)    {        NSLog(@"fail to play audio :(");        return;    }    [_player setNumberOfLoops:-1];    [_player setVolume:1];    [_player prepareToPlay];    [_player play];}- (void) didReceiveMemoryWarning{    [super didReceiveMemoryWarning];}@end
      Important points:
      • You must use AVAudioSession. Otherwise, there is a sound in the wood.
      • Do not use AVAudioPlayer as a local variable (in this example, do not define it in viewDidLoad ).
      • To find the path, use mainBundle here. Do not make a mistake. Source code

        Http://download.csdn.net/detail/prevention/6816959

        -

        Reprinted please indicate from: http://blog.csdn.net/prevention

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.