2012-07-21 2 views
0

클래스 본문 내에서 mp3 파일을 성공적으로 재생했습니다. 그러나 기능을 별도의 클래스로 이동하면 실패합니다.별도의 클래스로 이동하면 IOS - 재생 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 

구현 : =는 별도의 클래스에서 실행될 때 성공적 플레이어를 만들지 않습니다, 그러나

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

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

하고 "오디오 플레이어로 이동 : 여기

#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 

는 호출 코드의

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.)" 
= 닐 "분기 여기서

출력의

URL은 "file : //localhost/A.mp3"

내가 뭘 잘못하고있어? 어떻게 든 분리 된 메소드로 리팩토링 할 때 나는 항상 문제가있다. 그것은 실망 스럽다.

답변

1

당신은 URL에 오류가 있고, 여기에이 라인

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

을 변경 : 내가 생각

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

가장 먼저 나오는 것은 "play"클래스에서는 재생되지 않는 반면에 오디오 플레이어를 즉시 릴리스한다는 것입니다.

이 문제를 해결하기위한 좋은 디자인은 싱글 톤 클래스 내에서 오디오 플레이어를 한 번 인스턴스화하는 것입니다. 이 클래스는 전체 응용 프로그램에서 오디오를 재생해야하며 모든 클래스의 모든 요청을 관리해야합니다. 이렇게하면 메모리를 올바르게 관리하고 AVFoundation 프레임 워크가 한 곳에서만 사용된다는 것을 알게됩니다. 또한

, 당신의 필수 경로를 얻기 위해, 사용

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

당신 말이 맞아요, 석방이 문제를 일으키고 있어요. 그러나 NSUrl에 대한 다른 제안 된 솔루션은 잘못된 문자열 형식의 문제를 해결 한 솔루션이었습니다. –

+0

문제가 없습니다. 어쨌든 stringByAppendingPathComponent를 사용하는 것이 좋습니다. 귀하의 요구에 더 적합하며이 문제에있어보다 일반적인 접근 방법입니다. – Stavash

관련 문제