object-c之音频视频的播放

1.播放视频。首先要引入关于音频和视频的框架

#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>

2.初始化播放器准备播放

//初始化
    self.player = [[AVPlayerViewController alloc]init];
    //设置播放内容
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"宣传资料.mp4" ofType:nil];
    NSURL *url = [NSURL fileURLWithPath:filePath];
    self.player.player = [AVPlayer playerWithURL:url];
//打开后自动播放
    [self.player.player play];

3.音乐的播放,这里写了一个播放音乐的方法。然后只需要把要播放的歌曲名字传过来然后就播放就绪了。

//播放音乐方法
- (void) musicPlayWithName:(NSString *)musicName
{
    NSString *filePath = [[NSBundle mainBundle]pathForResource:musicName ofType:@"mp3"];
    NSURL *url = [NSURL fileURLWithPath:filePath];
    if(musicName==nil || url==nil || musicName.length==0)
    {
        return;
    }
    self.txtMusicName.text = musicName;
    //初始化播放对象
    AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    self.player = player;
    [player prepareToPlay];
    self.player.delegate = self;
}
//调用方法之后
[self.player play];//就可以播放了
//暂停
[self.player pause];
4.音乐播放有关播放进度的。需要加一个UISlider来显示播放进度然后配合上UITimer定时器来使用

//定时器的简单实例化
- (NSTimer *)timer
{
    if(!_timer)
    {
        //默认添加到运行循环
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(refrishScreen) userInfo:nil repeats:YES];
    }
    return _timer;
}
5.跟新播放进度

//定时器调用方法
- (void) refrishScreen
{
    //获得已经播放的时间
    NSTimeInterval current = self.player.currentTime;
    //计算分钟数
    NSInteger minute = current/60;
    //获取秒
    NSInteger s = (NSInteger)current%60;
    self.txtPlayTime.text = [NSString stringWithFormat:@"%02ld:%02ld",minute,s];
    
    NSTimeInterval endcurrent = self.player.duration;
    //计算分钟数
    NSInteger endminute = endcurrent/60;
    //获取秒
    NSInteger ends = (NSInteger)endcurrent%60;
    //获得歌曲总时间
    self.txtEndPlayTime.text = [NSString stringWithFormat:@"%02ld:%02ld",endminute,ends];
    //设置进度条时间
    self.slider.maximumValue = endcurrent;
    self.slider.value = current;
}

6.音乐播放还有一个代理方法是要用到的。就是当播放完成的时候自定播放下一曲

#pragma mark --代理
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    [self btnNextMusic:nil];//这里直接调用下一曲的方法就行了。
    [self.player play];
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的使用 gstreamer C 语言实现音视频同步的代码示例: ``` #include <gst/gst.h> static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data) { GMainLoop *loop = (GMainLoop *)data; switch (GST_MESSAGE_TYPE(msg)) { case GST_MESSAGE_EOS: g_print("End of stream\n"); g_main_loop_quit(loop); break; case GST_MESSAGE_ERROR: { gchar *debug; GError *error; gst_message_parse_error(msg, &error, &debug); g_free(debug); g_printerr("Error: %s\n", error->message); g_error_free(error); g_main_loop_quit(loop); break; } default: break; } return TRUE; } int main(int argc, char *argv[]) { GMainLoop *loop; GstElement *pipeline, *src, *sink, *audio_sink, *video_sink, *convert; GstBus *bus; guint bus_watch_id; GstPad *video_pad, *audio_pad; /* Initialize GStreamer */ gst_init(&argc, &argv); /* Create the elements */ src = gst_element_factory_make("filesrc", "source"); convert = gst_element_factory_make("decodebin", "convert"); audio_sink = gst_element_factory_make("autoaudiosink", "audio_sink"); video_sink = gst_element_factory_make("autovideosink", "video_sink"); /* Create the empty pipeline */ pipeline = gst_pipeline_new("test-pipeline"); if (!pipeline || !src || !convert || !audio_sink || !video_sink) { g_printerr("Not all elements could be created.\n"); return -1; } /* Build the pipeline */ gst_bin_add_many(GST_BIN(pipeline), src, convert, audio_sink, video_sink, NULL); if (gst_element_link(src, convert) != TRUE || gst_element_link(convert, audio_sink) != TRUE || gst_element_link(convert, video_sink) != TRUE) { g_printerr("Elements could not be linked.\n"); gst_object_unref(pipeline); return -1; } /* Set the URI to play */ g_object_set(src, "location", argv[1], NULL); /* Connect to the bus */ loop = g_main_loop_new(NULL, FALSE); bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); bus_watch_id = gst_bus_add_watch(bus, bus_call, loop); gst_object_unref(bus); /* Start playing */ gst_element_set_state(pipeline, GST_STATE_PLAYING); /* Wait for the playback to finish */ g_main_loop_run(loop); /* Clean up */ gst_element_set_state(pipeline, GST_STATE_NULL); gst_object_unref(GST_OBJECT(pipeline)); g_source_remove(bus_watch_id); g_main_loop_unref(loop); return 0; } ``` 这个示例代码使用了 GStreamer 库来实现音视频播放,首先需要创建各个元素(例如`filesrc`、`decodebin`、`autoaudiosink`、`autovideosink`),然后将它们添加到管道中,并且使用`gst_element_link`函数将它们连接起来。接着,设置要播放的音视频文件的 URI,并将管道设置为播放状态。最后,使用`g_main_loop_run`函数等待播放结束。在播放过程中,使用`bus_call`函数来处理各种 GStreamer 消息,例如错误消息和结束消息。 在音视频同步方面,需要使用`gst_pad_set_offset`函数来设置音频视频之间的时间偏移值,以确保它们能够同步播放。在管道中连接`decodebin`元素后,可以使用`gst_element_get_static_pad`函数获取音频视频源的输入端口,并使用`gst_pad_set_offset`函数设置它们之间的时间偏移值。 由于每个音视频文件的编码和格式都不同,因此需要根据实际情况进行修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值