xcode 如何在 iOS 应用中播放 m3u 音频流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13833731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to play m3u audio stream in iOS app
提问by user1896794
I'm trying to create an iOS/iPhone radio app using Xcode 4.5.2.
我正在尝试使用 Xcode 4.5.2 创建一个 iOS/iPhone 广播应用程序。
I wanted to stream @"http://xx.xxxxxxx.com/8111/radio.m3u" with play, pause, volume control and able to play on background feature/multitasking.
我想通过播放、暂停、音量控制和能够在后台功能/多任务处理中播放@"http://xx.xxxxxxxx.com/8111/radio.m3u"。
I've added AVFoundation
, Mediaplayer
and AudioToolBox
frameworks thus far. I've added play, pause and slider objects to xib.
到目前为止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];
}
回答by Yashesh
There are Two way to achieve this.
有两种方法可以实现这一点。
- You can directly load you URL in UIWebView and it will properly.
- You can also use MPMoviePlayerController.
- 您可以直接在 UIWebView 中加载您的 URL,它会正确地加载。
- 您也可以使用 MPMoviePlayerController。
回答by Vebz
Create a "MPMoviePlayerController *player" as a strong object in your ViewController.
在 ViewController 中创建一个“MPMoviePlayerController *player”作为强对象。
So you code would look something like below:
所以你的代码看起来像下面这样:
@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];
}