In asteroseismology, the observed time series often suffers from incomplete time coverage due to gaps. The presence of periodic gaps may generate spurious peaks in the power spectrum that limit the analysis of the data. Various methods have been developed to deal with gaps in time series data. However, it is still important to improve these methods to be able to extract all the possible information contained in the data. In this paper, we propose a new approach to handle the problem, the so-called inpainting method. This technique, based on a sparsity prior, enables to judiciously fill-in the gaps in the data, preserving the asteroseismic signal, as far as possible. The impact of the observational window function is reduced and the interpretation of the power spectrum is simplified. This method is applied both on ground and space-based data. It appears that the inpainting technique improves the oscillation modes detection and estimation. Additionally, it can be used to study very long time series of many stars because its computation is very fast. For a time series of 50 days of CoRoT-like data, it allows a speed-up factor of 1000, if compared to methods of the same accuracy.