コード例 #1
0
ファイル: mp3-player.go プロジェクト: enku2015/go-mp3-player
func SinglePlayProcess(fpath string, loop *C.GMainLoop) {
	// fmt.Printf("filename[%s]\n", fpath)
	var pipeline *C.GstElement // 定义组件
	var bus *C.GstBus

	switch t := filepath.Ext(fpath); t {
	case ".mp3":
	case ".wav":
	case ".ogg":
	case ".wma":
	case ".rmvb":
	default:
		fmt.Printf("不支持此文件格式[%s]\n", t)
		return
	}

	v0 := GString("playbin")
	v1 := GString("play")
	pipeline = C.gst_element_factory_make(v0, v1)
	GFree(unsafe.Pointer(v0))
	GFree(unsafe.Pointer(v1))
	v2 := GString(fpath)
	C.set_path(unsafe.Pointer(pipeline), v2)
	GFree(unsafe.Pointer(v2))

	// 得到 管道的消息总线
	bus = C.pipeline_get_bus(unsafe.Pointer(pipeline))
	if bus == (*C.GstBus)(nil) {
		fmt.Println("GstBus element could not be created.Exiting.")
		return
	}
	C.bus_add_watch(unsafe.Pointer(bus), unsafe.Pointer(loop))

	C.media_ready(unsafe.Pointer(pipeline))
	C.media_play(unsafe.Pointer(pipeline))

	// 开始循环
	C.g_main_loop_run(loop)
	C.media_stop(unsafe.Pointer(pipeline))
	C.object_unref(unsafe.Pointer(pipeline))
}
コード例 #2
0
ファイル: mp3-player.go プロジェクト: enku2015/go-mp3-player
func PlayProcess(cs chan byte, loop *C.GMainLoop) {
	var pipeline *C.GstElement // 定义组件
	var bus *C.GstBus

	wg := new(sync.WaitGroup)
	sig_out := make(chan bool)

	g_wg.Add(1)
	defer close(sig_out)
	defer g_wg.Done()
	if g_isOutOfOrder {
		outOfOrder(g_list)
		debug.FreeOSMemory()
	}

	start := g_list.Front()
	end := g_list.Back()
	e := g_list.Front()

	v0 := GString("playbin")
	v1 := GString("play")
	pipeline = C.gst_element_factory_make(v0, v1)
	GFree(unsafe.Pointer(v0))
	GFree(unsafe.Pointer(v1))
	// 得到 管道的消息总线
	bus = C.pipeline_get_bus(unsafe.Pointer(pipeline))
	if bus == (*C.GstBus)(nil) {
		fmt.Println("GstBus element could not be created.Exiting.")
		return
	}
	C.bus_add_watch(unsafe.Pointer(bus), unsafe.Pointer(loop))
	// 开始循环

	go func(sig_quit chan bool) {
		wg.Add(1)
		i := 0
	LOOP_RUN:
		for !g_isQuit {
			if i != 0 {
				C.media_ready(unsafe.Pointer(pipeline))
				C.media_play(unsafe.Pointer(pipeline))
			}
			C.g_main_loop_run(loop)
			C.media_stop(unsafe.Pointer(pipeline))
			switch g_play_style {
			case PLAY_STYLE_SINGLE:
				sig_quit <- true
				break LOOP_RUN

			case PLAY_STYLE_ORDER:
				if e != end {
					e = e.Next()
				} else {
					break LOOP_RUN
				}

			case PLAY_STYLE_SHUFFLE:
				if e != end {
					e = e.Next()
				} else {
					break LOOP_RUN
				}

			case PLAY_STYLE_SLOOP:

			case PLAY_STYLE_ALOOP:
				if e != end {
					e = e.Next()
				} else {
					e = start
				}

			}
			fpath, ok := e.Value.(string)
			if ok {
				v2 := GString(fpath)
				C.set_path(unsafe.Pointer(pipeline), v2)
				GFree(unsafe.Pointer(v2))

			} else {
				break
			}
			i++
		}

		C.object_unref(unsafe.Pointer(pipeline))
		wg.Done()

	}(sig_out)

	fpath, ok := e.Value.(string)
	if ok {
		// fmt.Printf("filename[%s]\n", fpath)
		v2 := GString(fpath)
		C.set_path(unsafe.Pointer(pipeline), v2)
		GFree(unsafe.Pointer(v2))

		C.media_ready(unsafe.Pointer(pipeline))
		C.media_play(unsafe.Pointer(pipeline))
		//C.set_mute(unsafe.Pointer(pipeline))

		lb := true
		for lb {
			select {
			case op := <-cs:
				switch op {
				case 's':
					C.media_pause(unsafe.Pointer(pipeline))
				case 'r':
					C.media_play(unsafe.Pointer(pipeline))
				case 'n':
					switch g_play_style {
					case PLAY_STYLE_SINGLE:
						lb = false
						g_isQuit = true
					case PLAY_STYLE_ORDER:
						fallthrough
					case PLAY_STYLE_SHUFFLE:

						C.media_stop(unsafe.Pointer(pipeline))
						if e != end {
							e = e.Next()
						} else {
							lb = false
							g_isQuit = true
						}
					case PLAY_STYLE_SLOOP:
						C.media_stop(unsafe.Pointer(pipeline))

					case PLAY_STYLE_ALOOP:
						if e != end {
							e = e.Next()
						} else {
							e = start
						}

					}
					if !lb {
						fpath, ok := e.Value.(string)
						if ok {
							v2 := GString(fpath)
							C.set_path(unsafe.Pointer(pipeline), v2)
							GFree(unsafe.Pointer(v2))
							C.media_ready(unsafe.Pointer(pipeline))
							C.media_play(unsafe.Pointer(pipeline))
						} else {
							lb = false
							g_isQuit = true
						}
					}
					//C.g_main_loop_quit(loop)
				case 'p':
					switch g_play_style {
					case PLAY_STYLE_SINGLE:
						// do nothing ???
					case PLAY_STYLE_ORDER:
						fallthrough
					case PLAY_STYLE_SHUFFLE:

						C.media_stop(unsafe.Pointer(pipeline))
						if e != start {
							e = e.Prev()
							fpath, ok := e.Value.(string)
							if ok {
								v2 := GString(fpath)
								C.set_path(unsafe.Pointer(pipeline), v2)
								GFree(unsafe.Pointer(v2))
								C.media_ready(unsafe.Pointer(pipeline))
								C.media_play(unsafe.Pointer(pipeline))
							} else {
								lb = false
								g_isQuit = true
							}
						} else {
							lb = false
							g_isQuit = true
						}
					case PLAY_STYLE_SLOOP:
						C.media_stop(unsafe.Pointer(pipeline))
						fpath, ok := e.Value.(string)
						if ok {
							v2 := GString(fpath)
							C.set_path(unsafe.Pointer(pipeline), v2)
							GFree(unsafe.Pointer(v2))
							C.media_ready(unsafe.Pointer(pipeline))
							C.media_play(unsafe.Pointer(pipeline))
						}
					case PLAY_STYLE_ALOOP:
						C.media_stop(unsafe.Pointer(pipeline))
						if e != start {
							e = e.Prev()
						} else {
							e = end
						}
						fpath, ok := e.Value.(string)
						if ok {
							v2 := GString(fpath)
							C.set_path(unsafe.Pointer(pipeline), v2)
							GFree(unsafe.Pointer(v2))
							C.media_ready(unsafe.Pointer(pipeline))
							C.media_play(unsafe.Pointer(pipeline))
						}
					}

				case 'q':
					lb = false
					g_isQuit = true
				case '+':
					g_volume_size++
					C.set_volume(unsafe.Pointer(pipeline), C.int(g_volume_size))
				case '-':
					g_volume_size--
					if g_volume_size < 0 {
						g_volume_size = 0
					}
					C.set_volume(unsafe.Pointer(pipeline), C.int(g_volume_size))
				case 't':
					C.media_seek(unsafe.Pointer(pipeline), C.gint64(5))

				}
			case vv0 := <-sig_out:
				if vv0 {
					C.g_main_loop_quit(loop)
					wg.Wait()
					g_wg.Done()
					g_wg.Wait()
					close(sig_out)
					os.Exit(0)
				}
			}
		}

	} else {
		// 路径非法
		return
	}

	C.g_main_loop_quit(loop)
	wg.Wait()

}