zoukankan      html  css  js  c++  java
  • 播放音乐

    播放音乐

    1.
    2.#import "ViewController.h"
    3.#import <AVFoundation/AVFoundation.h>
    4.
    5.@interface ViewController () <AVAudioPlayerDelegate> {
    6. AVAudioPlayer *myAudioPlayer;
    7.}
    8.@property (weak, nonatomic) IBOutlet UILabel *artistLabel;
    9.@property (weak, nonatomic) IBOutlet UILabel *albumLabel;
    10.@property (weak, nonatomic) IBOutlet UIImageView *artworkImageView;
    11.
    12.@end
    13.
    14.@implementation ViewController
    15.
    16.- (void)viewDidLoad {
    17. [super viewDidLoad];
    18.
    19. // 获得音频会话的单例对象
    20. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    21. // 激活音频会话
    22. [audioSession setActive:YES error:nil];
    23. // 设置支持后台播放
    24. [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
    25. // 设置应用程序可以接受远程控制事件
    26. [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    27.
    28. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"后会无期" ofType:@"mp3"];
    29. NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
    30.
    31.
    32. // 创建一个AVURLAsset对象,该对象保存了音频文件的元数据
    33. AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:fileUrl options:nil];
    34. // 获得元数据格式
    35. NSString *format = [[asset availableMetadataFormats] firstObject];
    36.
    37. // 根据指定的格式获得元数据
    38. // 对获得的元数据做一个循环取到其中的每一项
    39. for (AVMetadataItem *item in [asset metadataForFormat:format]) {
    40. if ([item.commonKey isEqualToString:@"artist"]) { // 艺术家
    41. self.artistLabel.text = (id)item.value;
    42. }
    43. else if ([item.commonKey isEqualToString:@"albumName"]) { // 专辑
    44. self.albumLabel.text = (id)item.value;
    45. }
    46. else if ([item.commonKey isEqualToString:@"artwork"]) { // 封面
    47. // 如果是封面的话item.value是封面图片的二进制数据
    48. // 需要转换成UIImage对象
    49. NSData *data = (id)item.value;
    50. self.artworkImageView.image = [UIImage imageWithData:data];
    51. }
    52. else if ([item.commonKey isEqualToString:@"title"]) { // 歌名
    53.
    54. }
    55. }
    56.
    57. myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
    58. // 音量(0-1) 1以后软放大
    59. myAudioPlayer.volume = 1;
    60. // 缓冲
    61. [myAudioPlayer prepareToPlay];
    62. // 开始播放
    63. [myAudioPlayer play];
    64. // 暂停播放
    65. // [myAudioPlayer pause];
    66.
    67. myAudioPlayer.delegate = self;
    68.
    69.// myAudioPlayer.enableRate = YES;
    70.// myAudioPlayer.rate = 8;
    71.}
    72.
    73.- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    74. // 当播放完成时要回调的方法(可以在此处添加代码实现播放下一首)
    75.
    76.}
    77.
    78.@end
    79.
    80.
     
  • 相关阅读:
    PTA 7-5 有趣的最近公共祖先问题 (30分)
    平衡二叉树的旋转类型及代码实现
    Ubuntu搭建青岛大学开源OJ
    见过猪跑现在开始吃猪肉了
    工作4年的老腊肉的总结
    服务器日志的目录
    Jacoco配置的问题
    一次述职之后的反省
    Python+Webdriver+Phantomjs,设置不同的User-Agent,获得的url不一致
    Eclipse+Pydev 找不到对应的module not in Pythonpath
  • 原文地址:https://www.cnblogs.com/buakaw/p/5211358.html
Copyright © 2011-2022 走看看