iOS mp3播放效果 iOS实现mp3播放效果

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

iOS mp3播放效果 iOS实现mp3播放效果

弦外雨   2021-03-24 我要评论
想了解iOS实现mp3播放效果的相关内容吗,弦外雨在本文为您仔细讲解iOS mp3播放效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:iOS,mp3,播放,下面大家一起来学习吧。

一、工程图

二、代码

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];
  
  
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们