2013-02-05 27 views
0

我正在實現一個用於播放音頻文件的應用程序(從文本到語音輸出並存儲在mnt/sdcard/audiofiles/audio.mp3中)。當我嘗試使用MediaPlayer播放時,出現錯誤。請看看我的代碼,並告訴我。如何在模擬器中播放本地保存的文件?

代碼:

MediaPlayer mMediaPlayer = new MediaPlayer(); 
try { 
    mMediaPlayer.setDataSource("/mnt/sdcard/audiofiles/audio01.mp3"); 
    mMediaPlayer.prepare(); 
} catch (IllegalArgumentException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IllegalStateException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
mMediaPlayer.start(); 

錯誤:

02-05 13:11:40.859: I/SynthProxy(286): setting pitch to 100<br> 
    02-05 13:11:40.898: E/MediaPlayer(15651): error (1, -2147483648)<br> 
    02-05 13:11:40.898: W/System.err(15651): java.io.IOException: Prepare failed.: status=0x1<br> 
    02-05 13:11:40.898: W/System.err(15651): at android.media.MediaPlayer.prepare(Native Method)<br> 
    02-05 13:11:40.898: W/System.err(15651): at com.example.testmedai1.MainActivity$MySpeech.onInit(MainActivity.java:93)<br> 
    02-05 13:11:40.898: W/System.err(15651): at android.speech.tts.TextToSpeech$1.onServiceConnected(TextToSpeech.java:451)<br> 
    02-05 13:11:40.898: W/System.err(15651): at android.app.ActivityThread$PackageInfo$ServiceDispatcher.doConnected(ActivityThread.java:1247)<br> 
    02-05 13:11:40.898: W/System.err(15651): at android.app.ActivityThread$PackageInfo$ServiceDispatcher$RunConnection.run(ActivityThread.java:1264)<br> 
    02-05 13:11:40.898: W/System.err(15651): at android.os.Handler.handleCallback(Handler.java:587)<br> 
    02-05 13:11:40.898: W/System.err(15651): at android.os.Handler.dispatchMessage(Handler.java:92)<br> 
    02-05 13:11:40.898: W/System.err(15651): at android.os.Looper.loop(Looper.java:123)<br> 
    02-05 13:11:40.898: W/System.err(15651): at android.app.ActivityThread.main(ActivityThread.java:4627)<br> 
    02-05 13:11:40.898: W/System.err(15651): at java.lang.reflect.Method.invokeNative(Native Method)<br> 
    02-05 13:11:40.898: W/System.err(15651): at java.lang.reflect.Method.invoke(Method.java:521)<br> 
    02-05 13:11:40.898: W/System.err(15651): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)<br> 
    02-05 13:11:40.898: W/System.err(15651): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)<br> 
    02-05 13:11:40.898: W/System.err(15651): at dalvik.system.NativeStart.main(Native Method)<br> 
    02-05 13:11:40.898: E/MediaPlayer(15651): start called in state 0<br> 
    02-05 13:11:40.898: E/MediaPlayer(15651): error (-38, 0)<br> 
    02-05 13:11:40.918: E/MediaPlayer(15651): Error (-38,0)<br>           

編輯1:

這是我想,當按鈕1點擊我創建的音頻文件的總碼。我使用DDMS-> File Exploring檢查了該文件,並將其保存到我的桌面並播放。它工作正常。但是,當我嘗試以實用的方式訪問它並嘗試播放時,我得到了錯誤。

package com.example.testmedai1; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.HashMap; 


import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.os.Environment; 
import android.app.Activity; 
import android.speech.tts.TextToSpeech; 
import android.speech.tts.TextToSpeech.OnInitListener; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

Button store, play; 
EditText input; 
String speakTextTxt; 
TextToSpeech mTts; 
private String path; 
HashMap<String, String> myHashRender = new HashMap<String, String>(); 
String tempDestFile ; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    store = (Button) findViewById(R.id.button1); 
    play = (Button) findViewById(R.id.button2); 
    input = (EditText) findViewById(R.id.editText1); 
    store.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 


      speakTextTxt = "Hello world Hello world Hello world Hello world Hello world"; 
      HashMap<String, String> myHashRender = new HashMap<String, String>(); 
      myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, speakTextTxt); 

      String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath(); 
      Log.d("MainActivity", "exStoragePath : "+exStoragePath); 
      File appTmpPath = new File(exStoragePath + "/audiofiles/"); 
      boolean isDirectoryCreated = appTmpPath.mkdirs(); 
      Log.d("MainActivity", "directory "+appTmpPath+" is created : "+isDirectoryCreated); 
      String tempFilename = "audio01.mp3"; 
      tempDestFile = appTmpPath.getAbsolutePath() + File.separator + tempFilename; 
      Log.d("MainActivity", "tempDestFile : "+tempDestFile); 
      new MySpeech(speakTextTxt); 
     } 
    }); 
} 

class MySpeech implements OnInitListener 
{ 

      String tts; 

    public MySpeech(String tts) 
    { 
     this.tts = tts; 
     mTts = new TextToSpeech(MainActivity.this, this); 
    } 

    public void onInit(int status) 
    { 
     Log.d("MainActivity", "onInit() called"); 

     int i = mTts.synthesizeToFile(speakTextTxt, myHashRender, tempDestFile); 
     if(i == TextToSpeech.SUCCESS) 
     { 

      Toast toast = Toast.makeText(MainActivity.this, "Saved "+i, Toast.LENGTH_SHORT); 
      toast.show(); 

     path = "/mnt/sdcard/audiofiles/audio01.mp3"; 

     //FileInputStream fis; 


     MediaPlayer mMediaPlayer = new MediaPlayer(); 
     try { 
      //fis = new FileInputStream(); 
      mMediaPlayer.setDataSource("/mnt/sdcard/audiofiles/audio01.mp3"); 
      mMediaPlayer.prepare(); 
     } catch (IllegalArgumentException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     mMediaPlayer.start(); 
     } 
     else { 
      Log.v("MainActivity", "error saving wav file : "+i); 
     } 
    } 
    } 

}

EDIT2:

我試着用下面的代碼,還是我收到同樣的錯誤。

class MySpeech implements OnInitListener 
{ 

      String tts; 

    public MySpeech(String tts) 
    { 
     this.tts = tts; 
     mTts = new TextToSpeech(MainActivity.this, this); 
    } 

    public void onInit(int status) 
    { 
     Log.d("MainActivity", "onInit() called"); 

     int i = mTts.synthesizeToFile(speakTextTxt, myHashRender, tempDestFile); 
     if(i == TextToSpeech.SUCCESS) 
     { 

      Toast toast = Toast.makeText(MainActivity.this, "Saved "+i, Toast.LENGTH_SHORT); 
      toast.show(); 

     try 
     { 
      audioPlayer("mnt/sdcard/audiofiles" , "audio01.mp3"); // your file location 
     } 
     catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 



     } 
     else { 
      Log.v("MainActivity", "error saving wav file : "+i); 
     } 
    } 
    } 

public void audioPlayer(String path, String fileName) throws IOException 
{   
     if (mp != null) 
     { System.out.println("before mp.reset() "); 
       mp.reset();  
       System.out.println("after mp.reset() "); 
      } 

     try 
     { 
      System.out.println("after path fileName "); 
      mp.setDataSource(path+"/"+fileName); 
      System.out.println("after path fileName "); 
     } 
     catch (IllegalArgumentException e) 
     { 
      // TODO Auto-generated catch block 
      //mp.reset(); 
      System.out.println("IllegalArgumentException "); 
      e.printStackTrace(); 
     } 
     catch (IllegalStateException e) 
     { 
      // TODO Auto-generated catch block 
      System.out.println("IllegalStateException "); 
      e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
      // TODO Auto-generated catch block 
      System.out.println("IOException "); 
      e.printStackTrace(); 
     } 
     try 
     { 
      mp.prepare(); 
     } catch (IllegalStateException e) 
     { 
      // TODO Auto-generated catch block 
      System.out.println("On prepare "); 
      e.printStackTrace(); 
     } 
     System.out.println("Before starting"); 
     mp.start(); 


} 

回答

0

試試用下面的方法玩你的文件。

public class MainActivity extends Activity { 
    Button store, play; 
    EditText input; 
    String speakTextTxt; 
    TextToSpeech mTts; 
    private String path; 
    HashMap<String, String> myHashRender = new HashMap<String, String>(); 
    String tempDestFile; 
    MediaPlayer mMediaPlayer; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    store = (Button) findViewById(R.id.button1); 
    play = (Button) findViewById(R.id.button2); 
    input = (EditText) findViewById(R.id.editText1); 
    mMediaPlayer = new MediaPlayer(); 
    store.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      speakTextTxt = "Hello world Hello world Hello world Hello world Hello world"; 
      HashMap<String, String> myHashRender = new HashMap<String, String>(); 
      myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, 
        speakTextTxt); 
      String exStoragePath = Environment .getExternalStorageDirectory().getAbsolutePath(); 
      Log.d("MainActivity", "exStoragePath : " + exStoragePath); 
      File appTmpPath = new File(exStoragePath + "/audiofiles/"); 
      boolean isDirectoryCreated = appTmpPath.mkdirs(); 
      Log.d("MainActivity", "directory " + appTmpPath 
        + " is created : " + isDirectoryCreated); 
      String tempFilename = "audio01.mp3"; 
      tempDestFile = appTmpPath.getAbsolutePath() + File.separator 
        + tempFilename; 
      Log.d("MainActivity", "tempDestFile : " + tempDestFile); 
      try { 
        audioPlayer(exStoragePath ,tempFilename); // your file location 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } 
     } 
    }); 
} 
public void audioPlayer(String path, String fileName) throws IOException { 
    if (mMediaPlayer != null) { 
     mMediaPlayer.reset(); 
    } 
    try { 
     mMediaPlayer.setDataSource(path + "/" + fileName); 
    } catch (IllegalArgumentException e) { 
     // TODO Auto-generated catch block 
     // mp.reset(); 
     e.printStackTrace(); 
    } catch (IllegalStateException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
} 

編輯: 檢查鏈接HERE它會指導你如何實現文本到語音 - 執行暫停使用的MediaPlayer在TTS恢復。

我希望它能幫到你..

謝謝。

+0

我加入這行到我的清單 – ChandraSekhar

+0

是現在或仍在工作的問題? – GrIsHu

+0

仍然我geting相同的錯誤,我發佈我的總代碼在Edit1:請看看。 – ChandraSekhar

0

試試這個:

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<Button id="@+id/cmd_play" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Play the music !!!" 
    /> 
</LinearLayout> 

MusicPlayer.java

public class MusicPlayer extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 
    // Find the Button from the xml-file. 
    Button cmd_play = (Button)this.findViewById(R.id.cmd_play); 
    cmd_play.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View arg0) { 
     MediaPlayer mp = MediaPlayer.create(MusicPlayer.this, R.raw.everlast); 
     mp.prepare(); 
     mp.start(); 
     // ie. react on the end of the music-file: 
     mp.setOnCompletionListener(new OnCompletionListener(){ 
      // @Override 
      public void onCompletion(MediaPlayer arg0) { 
      // File has ended !!! <img src="http://www.anddev.org/images/smilies/wink.png" alt=";)" title="Wink" /> 
      } 
     }); 
     } 
    }); 
    } 
} 
相關問題