【问题标题】:IOS - playing mp3 fails when moved to a separate classIOS - 移动到单独的班级时播放 mp3 失败
【发布时间】:2012-07-21 11:52:14
【问题描述】:

我成功地在课程主体中播放了一个 mp3 文件。但是,当我将功能移到单独的类时,它会失败。

这是工作代码:

标题:

#import <AVFoundation/AVFoundation.h>

@interface QuestionController : UIViewController 
<UITableViewDataSource, UITableViewDelegate, UISplitViewControllerDelegate>
{
    AVAudioPlayer *audioPlayer;

}

工作代码:

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/A.mp3", [[NSBundle mainBundle] resourcePath]]];

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = 0;

    if (audioPlayer == nil)
        NSLog(@"audio errror: %@",[error description]);             
    else 
        [audioPlayer play];

这是新课程:

标题:

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>


    @interface AudioPlayer : NSObject {

    }

    - (void *) playAudioFile:(NSString *) mp3File;

    @end

实现:

  #import "AudioPlayer.h"

    @implementation AudioPlayer

    - (void *) playAudioFile:(NSString *) mp3File {

        NSLog(@"mp3file to play: %@", mp3File ); 

        NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@", mp3File, [[NSBundle mainBundle] resourcePath]]];

        NSError *error;

        AVAudioPlayer *audioPlayer;
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        audioPlayer.numberOfLoops = 0;

        if (audioPlayer == nil) {
            NSLog(@"audio errror: %@",[error description]);
        }
        else {
            [audioPlayer play];
        }
        [audioPlayer release];

        return 0; 
    }   

    @end

这是调用代码:

   AudioPlayer *audioPlayer = [[AudioPlayer alloc] init];

   [audioPlayer playAudioFile:@"/A.mp3"];

但是,当它在单独的类中运行时,它没有成功创建播放器并转到“audio player == nil”分支

这是输出:

012-07-21 07:14:54.480 MyQuiz[6655:207] mp3file to play: /A.mp3
2012-07-21 07:15:40.827 MyQuiz[6655:207] audio errror: Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)"

网址是“file://localhost/A.mp3”

任何想法我做错了什么?当我重构为单独的方法时,我总是遇到麻烦。这令人沮丧。

【问题讨论】:

    标签: ios audio refactoring mp3


    【解决方案1】:

    您的网址有误,请更改此行

    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@", mp3File, [[NSBundle mainBundle] resourcePath]]];
    

    到这里:

    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], mp3File]];
    

    【讨论】:

      【解决方案2】:

      弹出的第一件事是您在调用 play 后立即释放音频播放器,而在“工作”类中不会发生这种情况。

      解决这个问题的一个好设计是在 Singleton 类中实例化音频播放器一次。这个类应该负责在整个应用程序中播放音频,并且应该管理来自所有类的任何请求。这样您就知道您正在正确地管理内存并且 AVFoundation 框架仅在一个地方使用。

      另外,要获取所需的路径,请使用:

      NSURL *url = [NSURL fileURLWithPath:[[NSString stringWithFormat:@"%@", mp3File] stringByAppendingPathComponent:[[NSBundle mainBundle] resourcePath]]];
      

      【讨论】:

      • 我认为你是对的,发布导致了问题。但 NSUrl 的另一个建议解决方案是解决无效字符串格式问题的解决方案。
      • 没有问题。无论如何,我建议使用 stringByAppendingPathComponent,它更适合您的需求,并且在这方面是一种更通用的方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多