如何在iOS应用程序中播放m3u音频流

时间:2012-12-12 06:00:20

标签: ios xcode audio avfoundation mpmovieplayer

我正在尝试使用Xcode 4.5.2创建iOS / iPhone广播应用。

我想通过播放,暂停,音量控制来播放@“http://xx.xxxxxxx.com/8111/radio.m3u”并能够播放背景功能/多任务处理。

到目前为止,我已添加了AVFoundationMediaplayerAudioToolBox框架。我已经为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];
    }

2 个答案:

答案 0 :(得分:1)

实现这一目标有两种方法。

  1. 你可以直接在UIWebView中加载你的URL,它会正确。
  2. 您也可以使用MPMoviePlayerController。

答案 1 :(得分: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];
}