objective-c 多媒体 音乐播放
2019獨角獸企業重金招聘Python工程師標準>>>
//
//? ViewController.m
//? 音樂播放
//
//? Created by DC017 on 15/12/28.
//? Copyright ? 2015年 DC017. All rights reserved.
//
#import "ViewController.h"
//引入音樂播放器框架
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVAudioPlayerDelegate>
@property(nonatomic,strong)AVAudioPlayer *audioPlayer;
@end
@implementation ViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? [self.audioPlayer play];
? ? _audioPlayer.delegate=self;
? ??
}
//get方法
-(AVAudioPlayer *)audioPlayer{
? ? NSLog(@"get 方法被調用");
? ? if (!_audioPlayer) {
? ? ? ? NSLog(@"播放器準備啟動,開始實例化");
? ? ? ? //1獲取音樂文件路徑 注意:這里獲取的時相對路經
? ? ? ? NSString * strurl=[[NSBundle mainBundle] pathForResource:@"逍遙嘆" ofType:@"mp3"];
? ? ? ? NSURL * url=[NSURL fileURLWithPath:strurl];
? ? ? ? //初始化音樂播放器
? ? ? ? NSError * error;
? ? ? ? _audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
? ? ? ? //設置播放屬性
? ? ? ? _audioPlayer.numberOfLoops=0;//0表示為不循環,負數表示為無線循環
? ? ? ? //獲取總秒數duration
? ? ? ? NSLog(@"總秒數%f",_audioPlayer.duration);
?? ? ? ? [_audioPlayer prepareToPlay];//加載音頻到緩存? ? ? ?
? ? }
? ? return _audioPlayer;
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
? ? NSLog(@"音樂播放完成");
}
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
@end
轉載于:https://my.oschina.net/u/2483781/blog/552953
總結
以上是生活随笔為你收集整理的objective-c 多媒体 音乐播放的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu -- 不输入密码执行sud
- 下一篇: 关于创建主键和索引的关系一个小小測试