溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

iOS實現mp3播放效果

發布時間:2020-09-04 12:12:05 來源:腳本之家 閱讀:171 作者:弦外雨 欄目:移動開發

一、工程圖

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女