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

3
我正在尝试使用Xcode 4.5.2创建一个iOS/iPhone收音机应用程序。
我想要通过播放、暂停、音量控制并具有背景播放/多任务处理功能来流式传输@"http://xx.xxxxxxx.com/8111/radio.m3u"。
到目前为止,我已经添加了AVFoundation, Mediaplayer, 和AudioToolBox 框架,并在 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];
    }

有人能帮我看一下上面的代码,指出问题在哪里吗?谢谢。 - user1896794
2个回答

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];
}

1

有两种方法可以实现这个。

  1. 您可以直接在 UIWebView 中加载 URL,它会正常工作。
  2. 您也可以使用 MPMoviePlayerController。

谢谢,您能告诉我上面的代码有什么问题吗? - user1896794
请问,在手表应用程序上是否有相同的问题?有没有示例? - Markus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接