2011-12-19 44 views
1

我有這個應該播放聲音的類。MediaPlayer.create異常

public class AudioUtil { 
    public static void play(Context ctx, File file) { 
     MediaPlayer player = MediaPlayer.create(ctx, Uri.fromFile(file)); 
     try { 
      player.start(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

但運行提供以下錯誤:

12-19 14:02:56.070: I/MediaPlayer(4085): prepareAsync called in state 4 
12-19 14:02:56.090: E/MediaPlayer(4085): error (1, -2147483648) 
12-19 14:02:56.090: W/System.err(4085): java.io.IOException: Prepare failed.: status=0x1 
12-19 14:02:56.090: W/System.err(4085):  at android.media.MediaPlayer.prepare(Native Method) 
12-19 14:02:56.090: W/System.err(4085):  at br.com.digitalpages.renderer.util.AudioUtil.play(AudioUtil.java:13) 
12-19 14:02:56.090: W/System.err(4085):  at br.com.digitalpages.renderer.view.infl.ViewInflater$3.execute(ViewInflater.java:146) 

回答

1
MediaPlayer player = new MediaPlayer(); 
player.setAudioStreamType(AudioManager.STREAM_MUSIC); 
try { 
    player.setDataSource(ctx, Uri.fromFile(file)); 
    player.prepare(); 
} catch (Exception e) { 
    e.printStackTrace(); 
}  
player.start();