简单的歌曲播放

来源:互联网 发布:软件开发人员绩效考核 编辑:程序博客网 时间:2024/04/28 16:00
简单的歌曲播放



#import
#import
@interface ViewController : UIViewController
{
    AVAudioPlayer *audioplayer;
    NSURL*audiopath;
    
    
}

-(void)startplayer;
-(void)stopplayer;
@end


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
-(void)startplayer{
   
//      audioplayer=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURLfileURLWithPath:[[NSBundle mainBundle]pathForResource:@"宜璇 -离开你是我的错" ofType:@"mp3" ]]error:nil];
   audiopath=[[NSURL alloc]initFileURLWithPath:[[NSBundlemainBundle]pathForResource:@"宜璇 - 离开你是我的错" ofType:@"mp3"] ];
    NSError*error=nil;
   audioplayer=[[AVAudioPlayer alloc]initWithContentsOfURL:audiopatherror:&error];
   audioplayer.delegate=self;
    if(error!=nil) {
       NSLog(@"播放错误!%@",[error description]);
       
       return;
     
    [audioplayerplay];
}
-(void)stopplayer{
    [audioplayerstop];
}
- (void)viewDidLoad
{
    [superviewDidLoad];
    // Do anyadditional setup after loading the view, typically from anib.
   
    UIView*view=[[UIView alloc]initWithFrame:[UIScreenmainScreen].applicationFrame];
   view.backgroundColor=[UIColor whiteColor];
    [self.viewaddSubview:view];

    UIButton*button01=[UIButton buttonWithType:UIButtonTypeRoundedRect];
   button01.frame=CGRectMake(90,100,160,40);
    [button01setTitle:@"播放按钮" forState:UIControlStateNormal];
    [button01addTarget:self action:@selector(startplayer)forControlEvents:UIControlEventTouchUpInside];
    [self.viewaddSubview:button01];

    UIButton*button02=[UIButton buttonWithType:UIButtonTypeRoundedRect];
   button02.frame=CGRectMake(90,200,160,40);
    [button02setTitle:@"暂停按钮" forState:UIControlStateNormal];
    [button02addTarget:self action:@selector(stopplayer)forControlEvents:UIControlEventTouchUpInside];
    [self.viewaddSubview:button02];
   
}

- (void)didReceiveMemoryWarning
{
    [superdidReceiveMemoryWarning];
    // Disposeof any resources that can be recreated.
}

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)playersuccessfully:(BOOL)flag
{
   NSLog(@"本次播放歌曲完成!");
}

-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)playererror:(NSError *)error
{
   NSLog(@"解码错误!");
}
@end

原创粉丝点击