iOS实现mp3播放效果
程序员文章站
2023-12-22 08:40:52
一、工程图
二、代码
rootviewcontroller.h
#import
//加入avfoundati...
一、工程图
二、代码
rootviewcontroller.h
#import <uikit/uikit.h> //加入avfoundation.framework头文件 #import <avfoundation/avfoundation.h> @interface rootviewcontroller : uiviewcontroller <avaudioplayerdelegate> { //播放器player avaudioplayer *avaudioplayer; } @end
rootviewcontroller.m
#import "rootviewcontroller.h" //导入avfoundation.framework #import <avfoundation/avfoundation.h> @interface rootviewcontroller () @end @implementation rootviewcontroller - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { // custom initialization } return self; } - (void)viewdidload { [super viewdidload]; // do any additional setup after loading the view. self.title=@"mp3的播放"; } -(void)initplay { //从budle路径下读取音频文件 nsstring *string = [[nsbundle mainbundle] pathforresource:@"母亲" oftype:@"mp3"]; //把音频文件转换成url格式 nsurl *url = [nsurl fileurlwithpath:string]; //初始化音频类 并且添加播放文件 avaudioplayer = [[avaudioplayer alloc] initwithcontentsofurl:url error:nil]; //开始进行播放 [avaudioplayer play]; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。