首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用Obj-C在mp3中播放iOS

如何用Obj-C在mp3中播放iOS
EN

Stack Overflow用户
提问于 2015-07-28 02:36:43
回答 3查看 6.6K关注 0票数 2

我在目标C中有一个问题,我正在开发一个iOS应用程序,它应该播放一个特定的mp3文件集--242个mp3文件--那么有人能告诉我什么是最好的方法来播放它们,我的意思是应该把它们放在服务器上,让它们都是本地的还是什么的?以及我应该如何在Xcode中编码它们。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-07-28 05:36:02

苹果提供了许多在iPhone上播放音频的方法--系统声音服务、AVAudioPlayer、音频队列服务和OpenAL。没有外部支持库,到目前为止最简单的两种方法是Services和AVAudioPlayer。

  1. 系统声音服务 它是有用的音频警报和简单的游戏声音。 类型:@“caf”;NSURL *pewPewURL = NSURL :pewPewPath;AudioServicesCreateSystemSoundID(__bridge CFURLRef)pewPewURL,&self.pewPewSound

重要的是将pewPewSound定义为iVar或属性,而不是局部变量,以便以后可以在dealloc中处理它。它被声明为SystemSoundID。如果你在AudioServicesPlaySystemSound(self.pewPewSound),之后立即把它处理掉,那么声音就不会播放了。 2. AVAudioPlayer

它允许一次播放多个声音(对每个声音使用不同的AVAudioPlayer ),即使应用程序在background..The AVAudioPlayer类中是AVFoundation的一部分,也可以播放声音,您需要将AVFoundation @导入到项目中。

代码语言:javascript
运行
复制
 NSError *error;
self.backgroundMusicPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:backgroundMusicURL error:&error];
[self.backgroundMusicPlayer prepareToPlay];
[self.backgroundMusicPlayer play];

ATBViewController.h

代码语言:javascript
运行
复制
#import <UIKit/UIKit.h>

@interface ATBViewController : UIViewController

@end

ATBViewController.m

代码语言:javascript
运行
复制
#import "ATBViewController.h"
#import "AudioController.h"

@interface ATBViewController ()

@property (strong, nonatomic) AudioController *audioController;

@end

@implementation ATBViewController

#pragma mark - Lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

    self.audioController = [[AudioController alloc] init];
    [self.audioController tryPlayMusic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - IBAction

- (IBAction)spaceshipTapped:(id)sender {
    //The call below uses AudioServicesPlaySystemSound to play
    //the short pew-pew sound.
    [self.audioController playSystemSound];
    [self fireBullet];
}

- (void)fireBullet {
    // In IB, the button to top layout guide constraint is set to 229, so
    // the bullets appear in the correct place, on both 3.5" and 4" screens
    UIImageView *bullets = [[UIImageView alloc] initWithFrame:CGRectMake(84, 256, 147, 29)];
    bullets.image = [UIImage imageNamed:@"bullets.png"];
    [self.view addSubview:bullets];
    [self.view sendSubviewToBack:bullets];
    [UIView beginAnimations:@"shoot" context:(__bridge void *)(bullets)];
    CGRect frame = bullets.frame;
    frame.origin.y = -29;
    bullets.frame = frame;
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView commitAnimations];
}

- (void) animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    UIImageView *bullets = (__bridge UIImageView *)context;
    [bullets removeFromSuperview];
}

@end

AudioController.h

代码语言:javascript
运行
复制
#import <Foundation/Foundation.h>

@interface AudioController : NSObject

- (instancetype)init;
- (void)tryPlayMusic;
- (void)playSystemSound;

@end

AudioController.m

代码语言:javascript
运行
复制
#import "AudioController.h"
@import AVFoundation;

@interface AudioController () <AVAudioPlayerDelegate>

@property (strong, nonatomic) AVAudioSession *audioSession;
@property (strong, nonatomic) AVAudioPlayer *backgroundMusicPlayer;
@property (assign) BOOL backgroundMusicPlaying;
@property (assign) BOOL backgroundMusicInterrupted;
@property (assign) SystemSoundID pewPewSound;

@end

@implementation AudioController

#pragma mark - Public

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self configureAudioSession];
        [self configureAudioPlayer];
        [self configureSystemSound];
    }
    return self;
}

- (void)tryPlayMusic {
    // If background music or other music is already playing, nothing more to do here
    if (self.backgroundMusicPlaying || [self.audioSession isOtherAudioPlaying]) {
        return;
    }

       [self.backgroundMusicPlayer prepareToPlay];
    [self.backgroundMusicPlayer play];
     self.backgroundMusicPlaying = YES;
}

- (void)playSystemSound {
    AudioServicesPlaySystemSound(self.pewPewSound);
}

#pragma mark - Private

- (void) configureAudioSession {
    // Implicit initialization of audio session
    self.audioSession = [AVAudioSession sharedInstance];


    NSError *setCategoryError = nil;
    if ([self.audioSession isOtherAudioPlaying]) { // mix sound effects with music already playing
        [self.audioSession setCategory:AVAudioSessionCategorySoloAmbient error:&setCategoryError];
        self.backgroundMusicPlaying = NO;
    } else {
        [self.audioSession setCategory:AVAudioSessionCategoryAmbient error:&setCategoryError];
    }
    if (setCategoryError) {
        NSLog(@"Error setting category! %ld", (long)[setCategoryError code]);
    }
}

- (void)configureAudioPlayer {
    // Create audio player with background music
    NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:@"background-music-aac" ofType:@"caf"];
    NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
    self.backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:nil];
    self.backgroundMusicPlayer.delegate = self;  // We need this so we can restart after interruptions
    self.backgroundMusicPlayer.numberOfLoops = -1;  // Negative number means loop forever
}

- (void)configureSystemSound {

    NSString *pewPewPath = [[NSBundle mainBundle] pathForResource:@"pew-pew-lei" ofType:@"caf"];
    NSURL *pewPewURL = [NSURL fileURLWithPath:pewPewPath];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)pewPewURL, &_pewPewSound);
}

#pragma mark - AVAudioPlayerDelegate methods

- (void) audioPlayerBeginInterruption: (AVAudioPlayer *) player {
        self.backgroundMusicInterrupted = YES;
    self.backgroundMusicPlaying = NO;
}

- (void) audioPlayerEndInterruption: (AVAudioPlayer *) player withOptions:(NSUInteger) flags{

      [self tryPlayMusic];
      self.backgroundMusicInterrupted = NO;
}

@end
票数 5
EN

Stack Overflow用户

发布于 2015-07-28 03:36:16

苹果公司提供AVAudioPlayer来处理音频媒体文件。您可以使用的另一个库是EZAudio,它与核心音频一起工作

票数 1
EN

Stack Overflow用户

发布于 2015-07-28 02:43:43

你可以这样做:

代码语言:javascript
运行
复制
NSUrl *videoUrl = [NSUrl urlWithString:@"url"];
MPMoviePlayerViewController *playerViewController = 
    [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString: videoUrl]];
[self presentMoviePlayerViewControllerAnimated:playerViewController];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31666475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档