2012-06-06 13 views
5

MediaPlayerでオーディオファイルを再生しようとしています。 MediaPlayerでバイト配列を再生したい。これどうやってするの?私はチェックしましたthisMediaPlayerでバイト配列を再生する - Android

public void writeSamples(byte[] samples, int length) 
{ 
    // track.write(samples, 0, length); 
    File tempMp3; 
    try { 
    tempMp3 = File.createTempFile("kurchina", ".mp3"); 
     tempMp3.deleteOnExit(); 
     FileOutputStream fos = new FileOutputStream(tempMp3); 
     fos.write(samples); 
     fos.close(); 
     // Tried reusing instance of media player 
     // but that resulted in system crashes... 
     MediaPlayer mediaPlayer = new MediaPlayer(); 

     // Tried passing path directly, but kept getting 
     // "Prepare failed.: status=0x1" 
     // so using file descriptor instead 
     FileInputStream fis = new FileInputStream(tempMp3); 
     mediaPlayer.setDataSource(fis.getFD()); 
     mediaPlayer.prepare(); 
     mediaPlayer.start(); 
     } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 

しかし、そのオーディオが再生されていません。それは、SDカードに多くのファイルを生成しているだけです。そして、このエラーを与えること:

06-06 11:02:59.191: E/MediaPlayer(1831): Unable to to create media player 
06-06 11:02:59.191: W/System.err(1831): java.io.IOException: setDataSourceFD failed.:  status=0x80000000 
06-06 11:02:59.201: W/System.err(1831): at android.media.MediaPlayer.setDataSource(Native Method) 
06-06 11:02:59.201: W/System.err(1831): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:749) 
06-06 11:02:59.201: W/System.err(1831): at org.vinuxproject.sonic.SonicTest$1.run(SonicTest.java:178) 
06-06 11:02:59.201: W/System.err(1831): at java.lang.Thread.run(Thread.java:1096) 

私を助けてください。どんな種類の助けにも感謝します。あなたのコメントに基づいて

おかげ

+0

あなたのコードを追加します。スニペットと誰かが助けることができるでしょう。 –

+0

FDに加えてオフセットと長さをとるsetDataSourceバリアントを試してみることをおすすめします。生成されたmp3ファイルは音楽アプリケーションで再生されますか?また、deleteOnExitはAndroid/Dalvikでは便利ではありません。 – dagalpin

+0

アプリケーションで再生されているファイルがありません。どうかお手伝いください –

答えて

6

、あなたはストリーミング用のWAVファイルを使用しています。従来のWAV has a headerはファイルの先頭にあり、残りのファイルはの純粋なデータです。ヘッダ部分を含まない場合は、データを解釈できるようにするためのパラメータをプレーヤに提供する必要があります(チャネル数、1秒あたりのサンプル数、ブロックサイズなど)。結果として、WAVファイルだけではストリーミングできないため、パラメータをプレーヤに供給する必要があります。一方、ストリーミングが可能であると指定されているのは、

MPEG-4です。これには、単独で再生することが可能な識別可能なセグメントが含まれているため、データチャンクにヘッダーが含まれていれば、再生可能なデータにすることができます。良い圧縮率に加えて、これは多くのインターネットラジオがそれを使用する理由の1つです。

MediaPlayerは高レベルのコンポーネントであり、WAVチャンクの再生に必要な低レベルのパラメータにはアクセスできません。ソースをWAVにする必要があり、他のstreamable formatsを使用できない場合は、クラスまたはOpenSL ESを試してみてください。 AudioTrackについては これは非常に良いチュートリアルです:ファイルのバイトを書き込みした後http://audioprograming.wordpress.com/2012/10/18/a-simple-synth-in-android-step-by-step-guide-using-the-java-sdk/

+0

うーん... audioTrackは行く方法のように聞こえるかもしれません。 – EGHDK

+0

私が助けることができるかどうか教えてください。 – allprog

+0

ブログは現在プライベートになっていますのでワードプレスリンクは "死んでいます" –

0

: あなたは、この関数から再生することができます:

void playSound(int resid) { 
     MediaPlayer eSound = MediaPlayer.create(context, resid); 
     Resources res = context.getResources(); 
     AssetFileDescriptor afd = res.openRawResourceFd(resid); 
     eSound.reset(); 
     eSound.setAudioStreamType(AudioManager.STREAM_SYSTEM); 
     try { 
      eSound.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), 
        afd.getLength()); 
     } catch (IllegalArgumentException e) { 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     try { 
      eSound.prepare(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     eSound.start(); 
    } 

そして、あなたはここからファイル情報を取得することができた:

byte[] getFileInformation(String filepath) { 
     MediaPlayer eSound = MediaPlayer.create(context, Uri.parse(filepath)); 
     eSound.reset(); 
     eSound.setAudioStreamType(AudioManager.STREAM_SYSTEM); 
     try { 
      eSound.setDataSource(filepath); 
     } catch (IllegalArgumentException e) { 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     try { 
      eSound.prepare(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     int duration = eSound.getDuration()/1000; 

     int height = 480; 
     int width = 640; 
     height = eSound.getVideoHeight(); 
     width = eSound.getVideoWidth(); 

     eSound.release(); 
     File f = new File(filepath); 
     int size = (int) f.length(); 
     byte[] b = new byte[16]; 
     System.arraycopy(convertIntToByte(size), 0, b, 0, 4); 
     System.arraycopy(convertIntToByte(duration), 0, b, 4, 4); 
     System.arraycopy(convertIntToByte(width), 0, b, 8, 4); 
     System.arraycopy(convertIntToByte(height), 0, b, 12, 4); 
     return b; 
    } 
0

これを試してみてください:

private void playMp3(byte[] mp3SoundByteArray) 
{ 
    try 
    { 

     File path=new File(getCacheDir()+"/musicfile.3gp"); 

     FileOutputStream fos = new FileOutputStream(path); 
     fos.write(mp3SoundByteArray); 
     fos.close(); 

     MediaPlayer mediaPlayer = new MediaPlayer(); 

     FileInputStream fis = new FileInputStream(path); 
     mediaPlayer.setDataSource(getCacheDir()+"/musicfile.3gp"); 

     mediaPlayer.prepare(); 
     mediaPlayer.start(); 
    } 
    catch (IOException ex) 
    { 
     String s = ex.toString(); 
     ex.printStackTrace(); 
    } 
} 
関連する問題