/// <summary> /// 初始化BassEngine /// </summary> private void Initialize(DeviceInfo? device = null) { positionTimer.Interval = TimeSpan.FromMilliseconds(50); positionTimer.Tick += positionTimer_Tick; IsPlaying = false; IntPtr handle = IntPtr.Zero; if (Application.Current.MainWindow != null) { handle = new WindowInteropHelper(Application.Current.MainWindow).EnsureHandle(); } int deviceNO = FindDevice(device, true); if (!Un4seen.Bass.Bass.BASS_Init(deviceNO, sampleFrequency, Un4seen.Bass.BASSInit.BASS_DEVICE_DEFAULT, handle)) { var error = Un4seen.Bass.Bass.BASS_ErrorGetCode(); int count = Un4seen.Bass.Bass.BASS_GetDeviceCount(); for (deviceNO = -1; deviceNO < count; ++deviceNO) { if (deviceNO != 0 && Un4seen.Bass.Bass.BASS_Init(deviceNO, sampleFrequency, Un4seen.Bass.BASSInit.BASS_DEVICE_DEFAULT, handle)) { break; } } if (deviceNO == count) { throw new BassInitializationFailureException(error); } } if (device == null && deviceNO == FindDefaultDevice()) { Device = null; } else { var info = Un4seen.Bass.Bass.BASS_GetDeviceInfo(Un4seen.Bass.Bass.BASS_GetDevice()); Device = new DeviceInfo { Driver = info.driver, Name = info.name, ID = info.id }; } Un4seen.Bass.Bass.BASS_SetConfig(Un4seen.Bass.BASSConfig.BASS_CONFIG_NET_TIMEOUT, 15000); }
/// <summary> /// 查找设备的序号 /// </summary> /// <param name="device">要查找的设备</param> /// <param name="returnDefault">当找不到设备时,是否返回默认设备的序号</param> /// <returns></returns> private static int FindDevice(DeviceInfo? device, bool returnDefault = false) { if (device.HasValue) { int deviceNO = -1; var devices = Un4seen.Bass.Bass.BASS_GetDeviceInfos(); var filteredDevices = from d in devices where d.id != null && d.id == device.Value.ID select Array.IndexOf(devices, d); if (filteredDevices.Count() == 1) { deviceNO = filteredDevices.First(); } if (deviceNO == -1) { filteredDevices = from d in devices where d.name == device.Value.Name select Array.IndexOf(devices, d); if (filteredDevices.Count() == 1) { deviceNO = filteredDevices.First(); } } if (deviceNO == -1) { filteredDevices = from d in devices where d.driver == device.Value.Driver select Array.IndexOf(devices, d); if (filteredDevices.Count() == 1) { deviceNO = filteredDevices.First(); } } if (deviceNO == -1 && returnDefault) { return FindDefaultDevice(); } else if (deviceNO != -1) { return deviceNO; } else { throw new Exception("找不到此设备:" + device.Value.Name); } } else { return FindDefaultDevice(); } }
/// <summary> /// 更换设备 /// </summary> public void ChangeDevice(DeviceInfo? device) { int deviceNO = FindDevice(device); int oldDeviceNO = Un4seen.Bass.Bass.BASS_GetDevice(); if (oldDeviceNO != deviceNO) { if (!Un4seen.Bass.Bass.BASS_GetDeviceInfo(deviceNO).IsInitialized) { IntPtr handle = IntPtr.Zero; if (Application.Current.MainWindow != null) { handle = new WindowInteropHelper(Application.Current.MainWindow).EnsureHandle(); } if (!Un4seen.Bass.Bass.BASS_Init(deviceNO, sampleFrequency, Un4seen.Bass.BASSInit.BASS_DEVICE_DEFAULT, handle)) { throw new Exception(Un4seen.Bass.Bass.BASS_ErrorGetCode().ToString()); } } if (activeStreamHandle != 0) { if (!Un4seen.Bass.Bass.BASS_ChannelSetDevice(activeStreamHandle, deviceNO)) { throw new Exception(Un4seen.Bass.Bass.BASS_ErrorGetCode().ToString()); } } if (!Un4seen.Bass.Bass.BASS_SetDevice(oldDeviceNO)) { throw new Exception(Un4seen.Bass.Bass.BASS_ErrorGetCode().ToString()); } if (!Un4seen.Bass.Bass.BASS_Free()) { throw new Exception(Un4seen.Bass.Bass.BASS_ErrorGetCode().ToString()); } if (!Un4seen.Bass.Bass.BASS_SetDevice(deviceNO)) { throw new Exception(Un4seen.Bass.Bass.BASS_ErrorGetCode().ToString()); } } Device = device; }
/// <summary> /// 显式初始化 /// </summary> public static void ExplicitInitialize(DeviceInfo? deviceInfo = null) { if (instance == null) instance = new BassEngine(deviceInfo); }
private BassEngine(DeviceInfo? deviceInfo = null) { Initialize(deviceInfo); //设置播放结束的回调 endTrackSyncProc = EndTrack; }
/// <summary> /// 初始化BassEngine /// </summary> private void Initialize(DeviceInfo? device = null) { positionTimer.Interval = TimeSpan.FromSeconds(double.Parse(ConfigurationManager.AppSettings["Bass.ChannelPositionRefreshRate"])); positionTimer.Tick += positionTimer_Tick; IsPlaying = false; IntPtr handle = IntPtr.Zero; if (Application.Current.MainWindow != null) { handle = new WindowInteropHelper(Application.Current.MainWindow).EnsureHandle(); } int deviceNO = FindDevice(device, true); if (!Un4seen.Bass.Bass.BASS_Init(deviceNO, sampleFrequency, initFlags, handle)) { var error = Un4seen.Bass.Bass.BASS_ErrorGetCode(); int count = Un4seen.Bass.Bass.BASS_GetDeviceCount(); for (deviceNO = -1; deviceNO < count; ++deviceNO) { if (deviceNO != 0 && Un4seen.Bass.Bass.BASS_Init(deviceNO, sampleFrequency, initFlags, handle)) { break; } } if (deviceNO == count) { throw new BassInitializationFailureException(error); } } if (device == null && deviceNO == FindDefaultDevice()) { Device = null; } else { var info = Un4seen.Bass.Bass.BASS_GetDeviceInfo(Un4seen.Bass.Bass.BASS_GetDevice()); Device = new DeviceInfo { Driver = info.driver, Name = info.name, ID = info.id }; } }