【问题标题】:How to play m3u audio stream in iOS app如何在 iOS 应用中播放 m3u 音频流
【发布时间】:2012-11-29 19:13:56
【问题描述】:

我正在尝试使用 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];
    }

【问题讨论】:

  • 有人可以帮我看看上面的代码有什么问题吗?谢谢

标签: ios xcode audio avfoundation mpmovieplayer


【解决方案1】:

有两种方法可以实现。

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

【讨论】:

  • 谢谢你,你能告诉我上面的代码有什么问题吗?
  • 拜托,同样的问题,但在手表应用程序上?有例子吗?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    相关资源
    最近更新 更多