Ok solved the problem by myself:

Qt Code:
  1. void MainWindow::try_with_ffmpeg()
  2. {
  3. av_register_all();
  4.  
  5. int audioStream = -1;
  6. AVCodecContext *codecContext;
  7. AVCodec *codec;
  8. AVPacket packet;
  9.  
  10. av_open_input_file(&formatContext, WMV, NULL,0,NULL);
  11. av_find_stream_info(formatContext);
  12. av_dump_format(formatContext, 0, WMV,0);
  13.  
  14. for( unsigned int i=0; i < formatContext->nb_streams; i++ )
  15. {
  16. if( formatContext->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO )
  17. {
  18. audioStream = i;
  19. break;
  20. }
  21. }
  22. if( audioStream == -1 )
  23. {
  24. qWarning() << "Didnt find audio stream";
  25. return;
  26. }
  27.  
  28. codecContext = formatContext->streams[audioStream]->codec;
  29.  
  30. codec = avcodec_find_decoder(codecContext->codec_id);
  31. avcodec_open(codecContext,codec);
  32.  
  33. med_buffer = (uint8_t*)malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  34. buffer.open(QBuffer::WriteOnly);
  35.  
  36. while( av_read_frame(formatContext,&packet) >= 0 )
  37. {
  38. qint64 written=0;
  39. if( packet.stream_index == audioStream )
  40. {
  41. qDebug() << "size= " << packet.size;
  42. while(packet.size > 0)
  43. {
  44. int len, data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
  45. //decode
  46. len = avcodec_decode_audio3(codecContext, (int16_t*)med_buffer, &data_size, &packet);
  47. if( len <= 0 )
  48. qWarning() << "no data read to buffer";
  49. if( data_size > 0 )
  50. written = buffer.write((const char*)med_buffer, data_size);
  51.  
  52. packet.size -= len;
  53. packet.data += len;
  54.  
  55. if( buffer.size() > 10000000 )
  56. {
  57. qDebug() << "buffer.size()=" << buffer.size();
  58. break;
  59. }
  60. }
  61.  
  62. }
  63.  
  64. }
  65.  
  66. QAudioFormat format;
  67. format.setFrequency(codecContext->sample_rate);
  68. format.setChannels(2);
  69. format.setSampleSize(16);
  70. format.setCodec("audio/pcm");
  71. format.setByteOrder(QAudioFormat::LittleEndian);
  72. format.setSampleType(QAudioFormat::SignedInt);
  73.  
  74. QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
  75.  
  76. if (!info.isFormatSupported(format)) {
  77. qWarning()<<"raw audio format not supported by backend, cannot play audio.";
  78. format = info.nearestFormat(format);
  79. }
  80.  
  81. audio = new QAudioOutput(format, this);
  82.  
  83. connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(stateChanged(QAudio::State)));
  84.  
  85. if( !buffer.open(QBuffer::ReadWrite) )
  86. qWarning() << "Couldnt open Buffer";
  87.  
  88. qDebug() << "buffer.size()=" << buffer.size();
  89.  
  90. audio->start(&buffer);
  91. }
To copy to clipboard, switch view to plain text mode