2012-12-12 160 views
3

我正在嘗試使用Xcode 4.5.2創建iOS/iPhone廣播應用程序。如何在iOS應用程序中播放m3u音頻流

我想播放@「http://xx.xxxxxxx.com/8111/radio.m3u」播放,暫停,音量控制,並能夠在後臺功能/多任務處理上播放。

我已經添加AVFoundation,MediaplayerAudioToolBox到目前爲止的框架。我添加了play,暫停和滑塊對象到xib。

ViewController.h 

@interface ViewController : UIViewController 

@property (strong,nonatomic) MPMoviePlayerController *myPlayer; 

@property (weak, nonatomic) IBOutlet UISlider *myslider; 

- (IBAction)playButtonPressed; 

- (IBAction)myslider:(id)sender; 

@end 

ViewController.m 
#import "ViewController.h" 
#import <MediaPlayer/MediaPlayer.h> 

@interface ViewController() 
{ 
    UISlider *volumeSlider; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
    UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid; 
    newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL]; 
} 

- (IBAction)playButtonPressed; 
{ 
    NSString *urlAddress = @"http://xxxxxxx.com/8111/listen.m3u"; 
    NSURL *url = [NSURL URLWithString:urlAddress]; 
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc]initWithContentURL:url]; 
    player.movieSourceType = MPMovieSourceTypeStreaming; 
    [player prepareToPlay]; 
    self.myPlayer = player; 
    [self.view addSubview:self.myPlayer.view]; 
    [self.myPlayer play]; 
} 

- (IBAction)stopButtonPressed; 
{ 
    [self.myPlayer stop]; 
} 
- (IBAction)myslider:(id)sender 
{ 
    MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame: CGRectMake(10, 10, 200, 40)]; 
    [volumeSlider addSubview:volumeView]; 
    [volumeView sizeToFit]; 
    } 
+0

能有人請與上面的代碼幫助我,什麼是錯的?謝謝 – user1896794

回答

1

有兩種方法可以實現這一點。

  1. 你可以直接加載你的URL在UIWebView中,它會正確。
  2. 您也可以使用MPMoviePlayerController。
+0

謝謝,能否請你告訴我上面的代碼有什麼問題? – user1896794

+0

請問,同樣的問題,但在手錶應用程序?有沒有例子? – Markus

1

在你的ViewController中創建一個「MPMoviePlayerController * player」作爲強對象。

所以,你的代碼將類似於下面:

@interface ViewController() 
{ 
    UISlider *volumeSlider; 
    MPMoviePlayerController *player; 
} 

@end 


- (IBAction)playButtonPressed; 
{ 
    NSString *urlAddress = @"http://xxxxxxx.com/8111/listen.m3u"; 
    NSURL *url = [NSURL URLWithString:urlAddress]; 

    if(nil != player) 
    { 
     player = nil; // Alternatively you can stop and restart with the different stream. 
    } 

    player = [[MPMoviePlayerController alloc]initWithContentURL:url]; 
    player.movieSourceType = MPMovieSourceTypeStreaming; 
    [player prepareToPlay]; 
    self.myPlayer = player; 
    [self.view addSubview:self.myPlayer.view]; 
    [self.myPlayer play]; 
} 
相關問題