2016-03-29 5 views
-1

Hallo Ich benutze unter Code, um meine lokale mp3-Datei zu spielen. aber kein Fehler kam. aber mp3 spielt nicht.AVAudioPlayer Nicht Mp3-Datei abspielen Ich habe versucht Aber kein Glück

AVAudioPlayer *audioPlayer ; 
NSString *sound_file; 
if ((sound_file = [[NSBundle mainBundle] pathForResource:@"mpthreetest" ofType:@"mp3"])){ 

    NSURL *url = [[NSURL alloc] initFileURLWithPath:sound_file]; 
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL]; 
    audioPlayer.delegate = self; 


    [audioPlayer prepareToPlay]; 
    [audioPlayer play]; 

} 
+0

NSBundle hat eine Methode namens URLForResource –

Antwort

0

Überprüfen Sie die folgenden Schritte.

  1. Ihre AppDelegate.m

    #import <AVFoundation/AVAudioSession.h>`<AVFoundation/AVAudioSession.h> 
    
  2. Ihre application:didFinishLaunchingWithOptions: Methode

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
        self.window.backgroundColor = [UIColor whiteColor]; 
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
    } 
    
  3. in Ihrem ViewController.h

    @property (nonatomic,retain) AVAudioPlayer *audioPlayer ; 
    
  4. In Yo ur ViewController.m

    @synthesize audioPlayer; 
    

Dann

NSString *sound_file; 
if ((sound_file = [[NSBundle mainBundle] pathForResource:@"mpthreetest" ofType:@"mp3"])){ 

    NSURL *url = [[NSURL alloc] initFileURLWithPath:sound_file]; 
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL]; 
    audioPlayer.delegate = self; 

    [audioPlayer prepareToPlay]; 
    [audioPlayer play];  
} 

Der Fehler, den Sie haben, ist, dass Sie Eigenschaft nicht festgelegt haben.

+0

Danke Das funktioniert für mich. – JoeSam