我正在尝试使用 Xcode 4.5.2 创建 iOS/iPhone 广播应用程序。
我想通过播放、暂停、音量控制和能够在后台功能/多任务处理上播放@“http://xx.xxxxxxx.com/8111/radio.m3u”。
到目前为止,我已经添加了 AVFoundation
、Mediaplayer
和 AudioToolBox
框架。我已向 xib 添加了播放、暂停和 slider 对象。
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];
}
最佳答案
有两种方法可以实现这一目标。
- 您可以直接在 UIWebView 中加载您的 URL,它会正确加载。
- 您还可以使用 MPMoviePlayerController。
关于ios - 如何在 iOS 应用程序中播放 m3u 音频流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13833731/